一个基于Nacos、Netty、SpringBoot的RPC框架
- 基于Nacos实现服务注册、发现和负载均衡
- 支持心跳机制检测TCP连接状态
- 支持不同的序列化工具,默认为ProtoStuff
- 提供注册中心和序列化工具的扩展接口
- 支持类似于OpenFeign的FeignClient接口方式的声明式调用
-
定义服务
@RpcServerService public class HelloService { public String hello(String name) { return "hello " + name; } }
-
使用注解@EnableRpcServer开启RPC服务端
@SpringBootApplication @EnableRpcServer public class RpcServerDemoApplication { public static void main(String[] args) { SpringApplication.run(RpcServerDemoApplication.class, args); } }
-
添加配置
# application.yml rpc: nacos: address: nacos.server # 你的nacos server的域名(默认8848端口),或{IP}:{Port} application: name: rpc-server-demo-service server: port: 7890
-
定义远程服务接口
方式1:接口名与远程服务的类名一致
@RpcClientService(service = "rpc-server-demo-service") public interface HelloService { String hello(String name); }
方式2:使用handler参数指定远程服务的类名
@RpcClientService(service = "rpc-server-demo-service", handler = "HelloService") public interface XxxxxService { String hello(String name); }
-
使用注解@EnableRpcClient开启客户端
@SpringBootApplication @EnableRpcClient public class RpcClientDemoApplication { public static void main(String[] args) { SpringApplication.run(RpcClientDemoApplication.class, args); } }
-
添加配置
# application.yml rpc: nacos: address: nacos.server # 你的nacos server的域名(默认8848端口),或{IP}:{Port}4
-
调用远程服务
@SpringBootTest public class TestRpcClient { @Autowired private HelloService helloService; @Test public void test() { System.out.println(helloService.hello("ZT")); } }