VereRPC 微分布式远程调用框架

1. 下载并解压  VereRPC.zip 文件

lib/dom4j-1.6.1.jar
lib/jaxen-1.1-beta-6.jar
VereRPC.jar
VereRPC.xml

2.服务器端

打开 Eclipse 新建一个项目 VereRPCServer 结构如下



新建用户模型类 com.vere.rpc.test.User 网络传输必须实现序列化接口 User implements Serializable

package com.vere.rpc.test;

import java.io.Serializable;

public class User implements Serializable{
	private String name;
	private int age;
	
	public User(String name, int age) {
	    this.name = name;
	    this.age = age;
    }
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	
}



新建服务接口 com.vere.rpc.test.UserService

package com.vere.rpc.test;

import java.util.List;

public interface UserService {
	public boolean add(User user);

	public List<User> find();
}


新建服务实现类 com.vere.rpc.test.UserServiceImpl

package com.vere.rpc.test;

import java.util.ArrayList;
import java.util.List;

public class UserServiceImpl implements UserService {

	private List<User> userList = new ArrayList<User>();

	public UserServiceImpl() {

	}

	@Override
	public boolean add(User user) {
		return userList.add(user);
	}

	@Override
	public List<User> find() {
		return userList;
	}

}


配置 src -> VereRPC.xml

<?xml version="1.0" encoding="UTF-8"?>
<VereRPC>
	<service interface="com.vere.rpc.test.UserService" class="com.vere.rpc.test.UserServiceImpl" />
</VereRPC>



新建服务启动类 com.vere.rpc.test.TestRPCServer

package com.vere.rpc.test;

import com.vere.rpc.server.RemoteServer;

public class TestRPCServer {

	public static void main(String[] args) {
		//启动 RPC 服务 监听  8080 端口
		RemoteServer.start(8080);
	}

}


运行服务启动类 com.vere.rpc.test.TestRPCServer

VereRPC Startup Successfull, Listening Port :  8080  ......



3.客户端

打开 Eclipse 新建一个项目 VereRPCClient 结构如下



将服务器端用户模型类 com.vere.rpc.test.User 拷贝到 VereRPCClient 下面

将服务器端服务接口 com.vere.rpc.test.UserService 拷贝到 VereRPCClient 下面

新建客户端测试类 com.vere.rpc.test.TestRPCClient

package com.vere.rpc.test;

import java.util.List;

import com.vere.rpc.client.RemoteClient;

public class TestClient {

	public static void main(String[] args) {

		//启动 10 个客户端线程 同时调用
		for (int i = 1; i <= 10; i++) {
			new Thread("客户" + i) {

				@Override
				public void run() {
					//实例化远程访问类
					RemoteClient remoteClient = new RemoteClient();
					try {
						//设置调用服务器ip 和 端口
						remoteClient.setRemoteAddressList("localhost:8080,localhost:8081");
						remoteClient.connect();

						//获得远程接口  调用远程方法
						UserService userService = (UserService) remoteClient.getBean(UserService.class);
						userService.add(new User(Thread.currentThread().getName(), 23));

						List<User> userList = userService.find();
						for (User user : userList) {
							System.out.println("name=" + user.getName() + " , age=" + user.getAge());
						}

					} catch (Exception e) {
						e.printStackTrace();
					} finally {
						//关闭远程连接
						remoteClient.close();
					}

				}

			}.start();
		}

	}

}



运行客户端测试类 com.vere.rpc.test.TestRPCClient  结果如下:

name=客户7 , age=23
name=客户1 , age=23
name=客户5 , age=23
name=客户4 , age=23
name=客户3 , age=23
name=客户9 , age=23
name=客户10 , age=23
name=客户8 , age=23
name=客户6 , age=23
name=客户2 , age=23


Easy Learning Java (4 Edition)


Easy Learning Design Patterns Java (3 Edition)


Easy Learning Data Structures & Algorithms Java (2 Edition)


Easy Learning Python (3 Edition)

VereRPC 微分布式远程调用框架