代码拉取完成,页面将自动刷新
当前版本:1.3.0
发布日期:20151206
发布日志参见 RELEASE.md
文档
参见 rpc-sample-api 模块
package com.xxx.rpc.sample.api;
public interface HelloService {
String hello(String name);
}
需要将 RPC 接口与 RPC 实现分别存放在不同的模块中
参见 rpc-sample-server 模块
<!-- RPC Sample API -->
<dependency>
<groupId>com.xxx.rpc</groupId>
<artifactId>rpc-sample-api</artifactId>
<version>${version.rpc}</version>
</dependency>
<!-- RPC Server -->
<dependency>
<groupId>com.xxx.rpc</groupId>
<artifactId>rpc-server</artifactId>
<version>${version.rpc}</version>
</dependency>
package com.xxx.rpc.sample.server;
import com.xxx.rpc.sample.api.HelloService;
import com.xxx.rpc.server.RpcService;
@RpcService(HelloService.class)
public class HelloServiceImpl implements HelloService {
@Override
public String hello(String name) {
return "Hello! " + name;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.xxx.rpc.sample.server"/>
<context:property-placeholder location="classpath:rpc.properties"/>
<!-- Service Registry -->
<bean id="serviceRegistry" class="com.xxx.rpc.registry.zookeeper.ZooKeeperServiceRegistry">
<constructor-arg name="zkAddress" value="${rpc.registry_address}"/>
</bean>
<!-- RPC Server -->
<bean id="rpcServer" class="com.xxx.rpc.server.RpcServer">
<constructor-arg name="serviceAddress" value="${rpc.service_address}"/>
<constructor-arg name="serviceRegistry" ref="serviceRegistry"/>
</bean>
</beans>
注册到 ZooKeeper 中的 ZNode 路径为:registry/service/address
,前 2 个节点是持久的,最后 1 个节点是临时的
rpc.service_address=127.0.0.1:8000
rpc.registry_address=127.0.0.1:2181
package com.xxx.rpc.sample.server;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class RpcBootstrap {
public static void main(String[] args) {
new ClassPathXmlApplicationContext("spring.xml");
}
}
运行 RpcBootstrap 类,将对外发布 RPC 服务,同时进行服务注册
参见 rpc-sample-client 模块
<!-- RPC Sample API -->
<dependency>
<groupId>com.xxx.rpc</groupId>
<artifactId>rpc-sample-api</artifactId>
<version>${version.rpc}</version>
</dependency>
<!-- RPC Client -->
<dependency>
<groupId>com.xxx.rpc</groupId>
<artifactId>rpc-client</artifactId>
<version>${version.rpc}</version>
</dependency>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:property-placeholder location="classpath:rpc.properties"/>
<!-- Service Discovery -->
<bean id="serviceDiscovery" class="com.xxx.rpc.registry.zookeeper.ZooKeeperServiceDiscovery">
<constructor-arg name="zkAddress" value="${rpc.registry_address}"/>
</bean>
<!-- RPC Proxy -->
<bean id="rpcProxy" class="com.xxx.rpc.client.RpcProxy">
<constructor-arg name="serviceDiscovery" ref="serviceDiscovery"/>
</bean>
</beans>
rpc.registry_address=127.0.0.1:2181
@Autowired
private RpcProxy rpcProxy; // 1
...
HelloService helloService = rpcProxy.create(HelloService.class); // 2
String result = helloService.hello("World"); // 3
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。