SpringMVC使用Hession发布远程服务

(1)三个项目,Api(存放提供者和消费者共有的xx,例如实体类以及服务接口等等)、Service(服务提供者)、Provider(服务消费者)

Api部分代码

 1 package cn.coreqi.entities;
 2 
 3 import java.io.Serializable;
 4 
 5 public class User implements Serializable {
 6     private Integer Id;
 7     private Integer Age;
 8     private String UserName;
 9     private String PassWord;
10     private Integer Enabled;
11 
12     public User() {
13     }
14 
15     public User(Integer id, Integer age, String userName, String passWord, Integer enabled) {
16         Id = id;
17         Age = age;
18         UserName = userName;
19         PassWord = passWord;
20         Enabled = enabled;
21     }
22 
23     public Integer getId() {
24         return Id;
25     }
26 
27     public void setId(Integer id) {
28         Id = id;
29     }
30 
31     public Integer getAge() {
32         return Age;
33     }
34 
35     public void setAge(Integer age) {
36         Age = age;
37     }
38 
39     public String getUserName() {
40         return UserName;
41     }
42 
43     public void setUserName(String userName) {
44         UserName = userName;
45     }
46 
47     public String getPassWord() {
48         return PassWord;
49     }
50 
51     public void setPassWord(String passWord) {
52         PassWord = passWord;
53     }
54 
55     public Integer getEnabled() {
56         return Enabled;
57     }
58 
59     public void setEnabled(Integer enabled) {
60         Enabled = enabled;
61     }
62 
63     @Override
64     public String toString() {
65         return "User{" +
66                 "Id=" + Id +
67                 ", Age=" + Age +
68                 ", UserName='" + UserName + '\'' +
69                 ", PassWord='" + PassWord + '\'' +
70                 ", Enabled=" + Enabled +
71                 '}';
72     }
73 }
 1 package cn.coreqi.service;
 2 
 3 import cn.coreqi.entities.User;
 4 
 5 import java.util.List;
 6 
 7 public interface UserService {
 8     public List<User> getAll();
 9     public User getById(int Id);
10     public void addUser(User user);
11     public void modifyUser(User user);
12     public void delById(int Id);
13 }

(2)服务提供者部分代码

 1 package cn.coreqi.service.impl;
 2 
 3 import cn.coreqi.entities.User;
 4 import cn.coreqi.service.UserService;
 5 import org.springframework.stereotype.Repository;
 6 
 7 import java.util.ArrayList;
 8 import java.util.List;
 9 
10 @Repository
11 public class UserServiceImpl implements UserService {
12     private static List<User> users = null;
13     static {
14         users = new ArrayList<>();
15         users.add(new User(1,24,"fanqi","123456",1));
16         users.add(new User(2,22,"gaoxing","123456",1));
17         users.add(new User(3,23,"xihuan","123456",1));
18     }
19     @Override
20     public List<User> getAll() {
21         return users;
22     }
23 
24     @Override
25     public User getById(int Id) {
26         for (User user : users){
27             if(user.getId() == Id){
28                 return user;
29             }
30         }
31         return null;
32     }
33 
34     @Override
35     public void addUser(User user) {
36         users.add(user);
37     }
38 
39     @Override
40     public void modifyUser(User user) {
41         delById(user.getId());
42         addUser(user);
43     }
44 
45     @Override
46     public void delById(int Id) {
47         for (User user : users){
48             if(user.getId() == Id){
49                 users.remove(user);
50             }
51         }
52     }
53 }

pom添加相关依赖

1     <dependency>
2       <groupId>com.caucho</groupId>
3       <artifactId>hessian</artifactId>
4       <version>4.0.51</version>
5     </dependency>

编写相应配置类

 1 package cn.coreqi.hessian;
 2 
 3 import cn.coreqi.service.UserService;
 4 import org.springframework.context.annotation.Bean;
 5 import org.springframework.context.annotation.Configuration;
 6 import org.springframework.remoting.caucho.HessianServiceExporter;
 7 import org.springframework.web.servlet.HandlerMapping;
 8 import org.springframework.web.servlet.handler.SimpleUrlHandlerMapping;
 9 
10 import java.util.Properties;
11 
12 @Configuration
13 public class HessianConfig {
14     @Bean
15     public HessianServiceExporter hessianExporterUserService(UserService userService){
16         HessianServiceExporter hessianServiceExporter = new HessianServiceExporter();
17         hessianServiceExporter.setService(userService);
18         hessianServiceExporter.setServiceInterface(UserService.class);
19         return hessianServiceExporter;
20     }
21 
22     @Bean
23     public HandlerMapping hessianMapping(){
24         SimpleUrlHandlerMapping mapping = new SimpleUrlHandlerMapping();
25         Properties mappings = new Properties();
26         mappings.setProperty("/user.service","hessianExporterUserService");
27         mapping.setMappings(mappings);
28         return mapping;
29     }
30 
31 }

*HessianServiceExporter是一个SpringMVC控制器,它接收Hessian请求,并将这些请求转换为对应的方法调用,我们需要DispatcherServlet拦截后缀为“*.service”的URL

 1 package cn.coreqi.config;
 2 
 3 import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
 4 
 5 import javax.servlet.MultipartConfigElement;
 6 import javax.servlet.ServletRegistration;
 7 import java.io.File;
 8 
 9 public class SpringWebAppInit extends AbstractAnnotationConfigDispatcherServletInitializer {
10     //Spring容器
11     @Override
12     protected Class<?>[] getRootConfigClasses() {
13         return new Class[]{RootConfig.class};
14     }
15 
16     //SpringMVC容器
17     @Override
18     protected Class<?>[] getServletConfigClasses() {
19         return new Class[]{WebConfig.class};
20     }
21 
22     //DispatcherServlet映射关系
23     @Override
24     protected String[] getServletMappings() {
25         return new String[]{"/","*.service"};   //添加对*.service的映射
26     }
27 
28 }

我们还需要配置一个URL映射来确保DispatcherServlet将请求转发到,我已经在上面的配置类完成了如下操作。

(3)服务消费者部分代码

pom添加依赖

1     <dependency>
2       <groupId>com.caucho</groupId>
3       <artifactId>hessian</artifactId>
4       <version>4.0.51</version>
5     </dependency>

编写相应配置类

 1 package cn.coreqi.hessian;
 2 
 3 import cn.coreqi.service.UserService;
 4 import org.springframework.context.annotation.Bean;
 5 import org.springframework.context.annotation.Configuration;
 6 import org.springframework.remoting.caucho.HessianProxyFactoryBean;
 7 
 8 @Configuration
 9 public class HessianConfig {
10     @Bean
11     public HessianProxyFactoryBean userService(){
12         HessianProxyFactoryBean proxy = new HessianProxyFactoryBean();
13         proxy.setServiceUrl("http://localhost:8081/SpringRmiService_war_exploded/user.service");
14         proxy.setServiceInterface(UserService.class);
15         return proxy;
16     }
17 }

调用

 1 package cn.coreqi.controller;
 2 
 3 import cn.coreqi.entities.User;
 4 import cn.coreqi.service.UserService;
 5 import org.springframework.beans.factory.annotation.Autowired;
 6 import org.springframework.stereotype.Controller;
 7 import org.springframework.web.bind.annotation.GetMapping;
 8 import org.springframework.web.bind.annotation.ResponseBody;
 9 
10 import java.util.List;
11 
12 @Controller
13 public class HomeController {
14     @Autowired
15     private UserService userService;
16 
17     @GetMapping("/home/index")
18     @ResponseBody
19     public Object index(){
20         return userService.getAll();
21     }
22 }

 

posted @ 2019-01-17 16:53  SpringCore  阅读(552)  评论(0编辑  收藏  举报