Spring使用RMI进行远程方法调用

(1)。我新建了三个项目,SpringRmiApi(存放提供者和消费者共有的xx,例如实体类以及服务接口等等)、SpringRmiService(服务提供者)、SpringRmiProvider(服务消费者)

以下是SpringRmiApi一些代码

 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 }

暴露服务

 1 package cn.coreqi.rmi;
 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.rmi.RmiServiceExporter;
 7 
 8 @Configuration
 9 public class RmiConfig {
10     @Bean
11     public RmiServiceExporter rmiServiceExporter(UserService userService){
12         RmiServiceExporter rmiServiceExporter = new RmiServiceExporter();
13         rmiServiceExporter.setService(userService);
14         rmiServiceExporter.setServiceName("UserService");
15         rmiServiceExporter.setServiceInterface(UserService.class);
16 //        rmiServiceExporter.setRegistryHost("coreqi.cn");
17         rmiServiceExporter.setRegistryPort(1199);
18         return rmiServiceExporter;
19     }
20 }

(3)服务消费者

引用服务

 1 package cn.coreqi.rmi;
 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.rmi.RmiProxyFactoryBean;
 7 
 8 @Configuration
 9 public class RmiConfig {
10     @Bean
11     public RmiProxyFactoryBean userService(){
12         RmiProxyFactoryBean rmiProxy = new RmiProxyFactoryBean();
13         rmiProxy.setServiceUrl("rmi://localhost/UserService");
14         rmiProxy.setServiceInterface(UserService.class);
15         return rmiProxy;
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 15:32  SpringCore  阅读(368)  评论(0编辑  收藏  举报