一款轻量级的RPC框架,目前还在完善中。
@RPCService(name = "HelloService", type = HelloService.class)
public interface HelloService {
@RPCMethod(name="hello")
String hello(PersonEnttiy person);
}
@RPCService(name = "UserService", type = UserService.class)
public interface UserService {
@RPCMethod(name="hello")
PersonEnttiy getOnePerson();
}
public class PersonEnttiy implements Serializable {
private static final long serialVersionUID = 1L;
private String name;
private String sex;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
public class HelloServiceImpl implements HelloService {
@Override
public String hello(PersonEnttiy person) {
return "Hello " + person.getName();
}
}
public class PersonEnttiy implements Serializable {
private static final long serialVersionUID = 1L;
private String name;
private String sex;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
public class RpcProvider {
public static void main(String[] args) throws Exception{
//step 1. create service
HelloService helloService = new HelloServiceImpl();
UserService userService = new UserServiceImpl();
//step 2. create expoter and select protocol
Exporter exporter = ExporterFactory.getInstance().create("127.0.0.1",8080,ProtocolOption.simple);
//step 3. add service in
exporter.AddService(HelloService.class,helloService, "1.0");
exporter.AddService(UserService.class, userService, "1.0");
//step 4. provide service
exporter.export();
}
}
public class RpcConsumer {
public static void main(String[] args) throws Exception{
ServiceProxy<HelloService> helloServiceProxy = ProxyFactory.getInstance().createProxy(HelloService.class,"1.0");
try{
HelloService helloService = helloServiceProxy.refer();
PersonEnttiy person = null;
person = new PersonEnttiy();
person.setName("Cean Cheng");
person.setSex("Male");
person.setAge(10);
String result = helloService.hello(person);
System.out.println(result);
}catch(Exception e){
}finally{
helloServiceProxy.close();
}
ServiceProxy<UserService> userServiceProxy = ProxyFactory.getInstance().createProxy(UserService.class, "1.0");
try{
UserService userService = userServiceProxy.refer();
PersonEnttiy person = userService.getOnePerson();
System.out.println(person.getSex());
}catch(Exception e){
e.printStackTrace();
}
finally{
userServiceProxy.close();
}
}
}
多注册中心
zookeeper注册中心
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="serviceRegistryFactory" class="com.uoko.rpc.registry.ServiceRegistryFactory">
<property name="loadServiceRegistry">
<value>zookeeperServiceRegistry</value>
</property>
</bean>
<bean id="zookeeperServiceRegistry" class="com.uoko.rpc.registry.ZookeeperServiceRegistry">
<property name="zookeeper">
<value>127.0.0.1:2181</value>
</property>
<property name="zookeeperRootPath">
<value>/services</value>
</property>
<property name="sessionTimeout">
<value>10000</value>
</property>
</bean>
</beans>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="serviceDiscoveryFactory" class="com.uoko.rpc.discovery.ServiceDiscoveryFactory">
<property name="loadServiceDiscovery">
<value>zookeeperServiceDiscovery</value>
</property>
</bean>
<bean id="zookeeperServiceDiscovery" class="com.uoko.rpc.discovery.ZookeeperServiceDiscovery">
<property name="zookeeper">
<value>127.0.0.1:2181</value>
</property>
<property name="zookeeperRootPath">
<value>/services</value>
</property>
<property name="sessionTimeout">
<value>10000</value>
</property>
</bean>
</beans>
目前支持以下负载策略