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

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

以下是SpringRmiApi一些代码

 package cn.coreqi.entities;

 import java.io.Serializable;

 public class User implements Serializable {
private Integer Id;
private Integer Age;
private String UserName;
private String PassWord;
private Integer Enabled; public User() {
} public User(Integer id, Integer age, String userName, String passWord, Integer enabled) {
Id = id;
Age = age;
UserName = userName;
PassWord = passWord;
Enabled = enabled;
} public Integer getId() {
return Id;
} public void setId(Integer id) {
Id = id;
} public Integer getAge() {
return Age;
} public void setAge(Integer age) {
Age = age;
} public String getUserName() {
return UserName;
} public void setUserName(String userName) {
UserName = userName;
} public String getPassWord() {
return PassWord;
} public void setPassWord(String passWord) {
PassWord = passWord;
} public Integer getEnabled() {
return Enabled;
} public void setEnabled(Integer enabled) {
Enabled = enabled;
} @Override
public String toString() {
return "User{" +
"Id=" + Id +
", Age=" + Age +
", UserName='" + UserName + '\'' +
", PassWord='" + PassWord + '\'' +
", Enabled=" + Enabled +
'}';
}
}
 package cn.coreqi.service;

 import cn.coreqi.entities.User;

 import java.util.List;

 public interface UserService {
public List<User> getAll();
public User getById(int Id);
public void addUser(User user);
public void modifyUser(User user);
public void delById(int Id);
}

(2)服务提供者代码

 package cn.coreqi.service.impl;

 import cn.coreqi.entities.User;
import cn.coreqi.service.UserService;
import org.springframework.stereotype.Repository; import java.util.ArrayList;
import java.util.List; @Repository
public class UserServiceImpl implements UserService {
private static List<User> users = null;
static {
users = new ArrayList<>();
users.add(new User(1,24,"fanqi","123456",1));
users.add(new User(2,22,"gaoxing","123456",1));
users.add(new User(3,23,"xihuan","123456",1));
}
@Override
public List<User> getAll() {
return users;
} @Override
public User getById(int Id) {
for (User user : users){
if(user.getId() == Id){
return user;
}
}
return null;
} @Override
public void addUser(User user) {
users.add(user);
} @Override
public void modifyUser(User user) {
delById(user.getId());
addUser(user);
} @Override
public void delById(int Id) {
for (User user : users){
if(user.getId() == Id){
users.remove(user);
}
}
}
}

暴露服务

 package cn.coreqi.rmi;

 import cn.coreqi.service.UserService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.remoting.rmi.RmiServiceExporter; @Configuration
public class RmiConfig {
@Bean
public RmiServiceExporter rmiServiceExporter(UserService userService){
RmiServiceExporter rmiServiceExporter = new RmiServiceExporter();
rmiServiceExporter.setService(userService);
rmiServiceExporter.setServiceName("UserService");
rmiServiceExporter.setServiceInterface(UserService.class);
// rmiServiceExporter.setRegistryHost("coreqi.cn");
rmiServiceExporter.setRegistryPort(1199);
return rmiServiceExporter;
}
}

(3)服务消费者

引用服务

 package cn.coreqi.rmi;

 import cn.coreqi.service.UserService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.remoting.rmi.RmiProxyFactoryBean; @Configuration
public class RmiConfig {
@Bean
public RmiProxyFactoryBean userService(){
RmiProxyFactoryBean rmiProxy = new RmiProxyFactoryBean();
rmiProxy.setServiceUrl("rmi://localhost/UserService");
rmiProxy.setServiceInterface(UserService.class);
return rmiProxy;
}
}

使用服务

 package cn.coreqi.controller;

 import cn.coreqi.entities.User;
import cn.coreqi.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody; import java.util.List; @Controller
public class HomeController {
@Autowired
private UserService userService; @GetMapping("/home/index")
@ResponseBody
public Object index(){
return userService.getAll();
}
}
上一篇:Eureka服务注册与发现


下一篇:Log4Cpp的使用(转)