自己写了一个RPC框架

项目只依赖asm-all.jar 和zookeeper.jar.

项目的配置文是 default.conf.properties

配置ip地址, default是本机 socket.bind.address

端口 socket.bind.port=47048

服务端注解的路径扫描 rpc.service.path.scan=org.fantasy.example

客户端端注解的路径扫描 rpc.reference.path.scan=org.fantasy.example

zookeeper路径的配置 service.registry.address=192.168.241.130:2181,192.168.241.130:2182

使用方法

服务端接口如下:

package org.fantasy.example;

import org.fantasy.bean.annotation.Consumer;
import org.fantasy.bean.annotation.Provider;
import org.fantasy.bean.annotation.RpcMethod;

@Provider(id="user", refClass="org.fantasy.example.UserServiceImpl")
@Consumer(id="user")
public interface UserService {
	@RpcMethod
	public User getUserById(int userId);
	@RpcMethod
	public void addUser(User user);
	@RpcMethod
	public boolean deleteUser(User user);
	
}

 

package org.fantasy.example;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

public class UserServiceImpl implements UserService {

	private List<User> userList = new ArrayList<User>();
	public User getUserById(int userId) {
		UserProfile profile = new UserProfile();
		profile.setSex((byte)1);
		profile.setAge((short)33);
		DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		try {
			profile.setBirthday(format.parse("1983-02-26"));
		} catch (ParseException e) {
			
		}
		User user = new User(1, "fantasy", "123456");
		user.setUserProfile(profile);
		return user;
	}

	public void addUser(User user) {
		userList.add(user);
	}

	public boolean deleteUser(User user) {
		userList.remove(user);
		return true;
	}

}

 几个实体类如下

package org.fantasy.example;

import java.io.Serializable;

public class User implements Serializable {

	private static final long serialVersionUID = -1037883824526548605L;

	private int userId;
	private String userName;
	private String password;
	private UserProfile userProfile;
	
	public User() {
		
	}
	
	public User(int userId, String userName, String password) {
		this.userName = userName;
		this.password = password;
		this.userId = userId;
	}

	public String getUserName() {
		return userName;
	}

	public String getPassword() {
		return password;
	}

	public UserProfile getUserProfile() {
		return userProfile;
	}

	public void setUserProfile(UserProfile userProfile) {
		this.userProfile = userProfile;
	}

	public int getUserId() {
		return userId;
	}

}
package org.fantasy.example;

import java.io.Serializable;
import java.util.Date;

public class UserProfile implements Serializable {
	private static final long serialVersionUID = 1L;
	private short age;
	private String phone;
	private byte sex;
	private Date birthday;
	private String address;
	public UserProfile() {
		
	}
	public short getAge() {
		return age;
	}
	public void setAge(short age) {
		this.age = age;
	}
	public String getPhone() {
		return phone;
	}
	public void setPhone(String phone) {
		this.phone = phone;
	}
	public byte getSex() {
		return sex;
	}
	public void setSex(byte sex) {
		this.sex = sex;
	}
	public Date getBirthday() {
		return birthday;
	}
	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
}

 服务端启动

package org.fantasy.example;

import java.io.IOException;

import org.fantasy.bean.bootstrap.server.ServerBootstrap;

public class ServerStarter {

	public static void main(String[] args) {
		ServerBootstrap bootstrap = new ServerBootstrap();
		bootstrap.start();
		try {
			System.in.read();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

客户端启动( 客户端需要服务端的接口定义)

package org.fantasy.example;

import java.io.IOException;

import org.fantasy.bean.bootstrap.client.ClientBootstrap;

public class ClientStarter {

	public static void main(String[] args) {
		ClientBootstrap bootstrap = new ClientBootstrap();
		bootstrap.start();
		UserService userService = (UserService)bootstrap.getBeanFactory().getBeanInstance("user");
		User user = userService.getUserById(1);
		userService.deleteUser(user);
		userService.addUser(user);
//		Foo foo = (Foo)bootstrap.getBeanFactory().getBeanInstance("foo");
//		foo.hello();
//		foo.bar("hello");
		try {
			System.in.read();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

 项目中的序列化是自己实现的,也可以使用JDK 的序列化,代码如下

package org.fantasy.net.io.unsafe;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import org.fantasy.example.User;

public class UseJdkObjectStream {

	public static void main(String[] args)  {
		User user = new User(1, "fantasy", "123456");
		UnsafeOutputStream uout = null;
		ObjectOutputStream out = null;
		UnsafeInputStream uin = null;
		ObjectInputStream in = null;
		try {
			uout = new UnsafeOutputStream();
			out = new ObjectOutputStream(uout);
			out.writeObject(user);
			uin = new UnsafeInputStream(uout.getBuffer());
			in = new ObjectInputStream(uin);
			user = (User)in.readObject();
		} catch (Exception e) {
			System.err.println(e.getMessage());
		} finally {
			try {
				if(out != null) {
					out.close();
					out = null;
				}
				if(in != null) {
					in.close();
					in = null;
				}
			} catch (IOException e) {
			}
		}
	}
}

猜你喜欢

转载自zhangyu84849467.iteye.com/blog/2295713