spring-boot-msa-rpc4j是对msa-rpc4j框架与spring boot整合,利用spring boot的自动配置特性自动配置msa-rpc4j。
maven:
<dependency>
<groupId>com.github.microcmpt</groupId>
<artifactId>spring-boot-msa-rpc4j-starter</artifactId>
<version>1.0.1</version>
</dependency>
gradle:
compile group: 'com.github.microcmpt', name: 'spring-boot-msa-rpc4j-starter', version: '1.0.1'
public interface HelloRpc4jService {
/**
* Hello string.
*
* @param str the str
* @return the string
*/
String hello(String str);
}
@RpcService(value = HelloRpc4jService.class)
public class HelloRpc4jServiceImpl implements HelloRpc4jService {
/**
* Hello string.
*
* @param str the str
* @return the string
*/
public String hello(String str) {
return "Hello, " + str;
}
}
@SpringBootApplication
@EnableRpc4jServer
public class Rpc4jServerSampleApplication {
/**
* The entry point of application.
*
* @param args the input arguments
*/
public static void main(String[] args) {
SpringApplication.run(Rpc4jServerSampleApplication.class, args);
}
}
maven:
<dependency>
<groupId>com.github.microcmpt</groupId>
<artifactId>spring-boot-msa-rpc4j-starter</artifactId>
<version>1.0.1</version>
</dependency>
gradle:
compile group: 'com.github.microcmpt', name: 'spring-boot-msa-rpc4j-starter', version: '1.0.1'
@Rpc4jClient
public interface HelloRpc4jServiceRpcClient extends HelloRpc4jService {
}
@RestController
@RequestMapping("/api")
public class HelloRpc4jController {
/**
* The Hello rpc 4 j service.
*/
@Resource
private HelloRpc4jServiceRpcClient helloServiceRpcClient;
/**
* Hello string.
*
* @param name the name
* @return the string
*/
@RequestMapping(method = RequestMethod.GET, value = "/hello/{name}")
public String hello(@PathVariable("name")String name) {
return helloServiceRpcClient.hello(name);
}
}
@SpringBootApplication
@EnableRpc4jClients
public class Rpc4jClientSampleApplication {
/**
* The entry point of application.
*
* @param args the input arguments
*/
public static void main(String[] args) {
SpringApplication.run(Rpc4jClientSampleApplication.class, args);
}
}
#服务端端口号,默认为8025,可修改
spring.rpc.server.port=8025
#服务注册中心地址,默认为localhost:2181,可修改,也可配置多个地址需以英文逗号隔开
spring.rpc.server.registry-address=localhost:2181
#服务注册中心地址,默认为localhost:2181,可修改,也可配置多个地址需以英文逗号隔开
spring.rpc.client.registry-address=localhost:2181
@EnableRpc4jServer用于开启rpc服务提供端RpcServer以及注册中心和健康监测的自动配置
@EnableRpc4jClients用于开启服务消费端(客户端)RpcClient以及服务发现自动配置
@Rpc4jClient用于标记服务消费端调用的远程服务,将其注入到spring容器并做动态代理