在前面的架构图上已经说明,rpc服务是基于spring容器启动的
spring 配置
<context:component-scan base-package="cn.myrpc.sample.server"/>
<context:property-placeholder location="classpath:rpc.properties"/>
<bean id="serviceRegistry" class="cn.myrpc.registry.ServiceRegistry">
<constructor-arg name="registryAddress" value="${registry.address}"/>
</bean>
<bean id="rpcServer" class="cn.myrpc.server.RpcServer">
<constructor-arg name="serverAddress" value="${server.address}"/>
<constructor-arg name="serviceRegistry" ref="serviceRegistry"/>
</bean
然后再cn.myrpc.server发布一个服务
`
@RpcService(HelloService.class)
public class HelloServiceImpl implements HelloService {
public String hello(String name) {
System.out.println("已经调用服务端接口实现,业务处理结果为:");
System.out.println("Hello! " + name);
return "Hello! " + name;
}
public String hello(Person person) {
System.out.println("已经调用服务端接口实现,业务处理为:");
System.out.println("Hello! " + person.getFirstName() + " " + person.getLastName());
return "Hello! " + person.getFirstName() + " " + person.getLastName();
}
}`
服务启动类
public class RpcBootstrap {
public static void main(String[] args) {
new ClassPathXmlApplicationContext("spring.xml");
}
}