需要安装 JDK 8 及以上 和 Maven 3 以上
<dependency>
<groupId>io.joyrpc</groupId>
<artifactId>joyrpc-all</artifactId>
<version>最新版本</version>
</dependency>
-
创建接口
/** * Demo interface */ public interface DemoService { String sayHello(String str); }
-
创建接口实现
public class DemoServiceImpl implements DemoService { public String sayHello(String name) { return "Hi " + name + ", response from provider. "; } }
-
编写服务端代码
public class ServerAPI { public static void main(String[] args) throws Exception { RegistryConfig registryConfig = new RegistryConfig(); registryConfig.setRegistry("memory");//内存注册中心 DemoService demoService = new DemoServiceImpl(); //服务提供者设置 ProviderConfig<DemoService> providerConfig = new ProviderConfig<>(); providerConfig.setServerConfig(new ServerConfig()); providerConfig.setInterfaceClazz("io.joyrpc.service.DemoService"); providerConfig.setRef(demoService); providerConfig.setAlias("joyrpc-demo"); providerConfig.setRegistry(registryConfig); providerConfig.export().whenComplete((v, t) -> {//发布服务 providerConfig.open(); }); System.in.read(); } }
-
拿到服务端接口
通常以jar的形式将接口类提供给客户端。在此,先定义全路径相同的接口做演示。
/** * Demo interface */ public interface DemoService { String sayHello(String str); }
-
编写客户端代码
public class ClientAPI { public static void main(String[] args) { RegistryConfig registryConfig = new RegistryConfig(); registryConfig.setRegistry("memory");//内存注册中心 ConsumerConfig<DemoService> consumerConfig = new ConsumerConfig<>();//consumer设置 consumerConfig.setInterfaceClazz("io.joyrpc.service.DemoService"); consumerConfig.setAlias("joyrpc-demo"); consumerConfig.setRegistry(registryConfig); try { CompletableFuture<Void> future = new CompletableFuture<Void>(); DemoService service = consumerConfig.refer(future); future.get(Integer.MAX_VALUE, TimeUnit.MILLISECONDS); String echo = service.sayHello("hello");//发起服务调用 System.in.read(); } catch (Exception e) { } } }
spring配置文件中需引入XSD文件
<?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:joyrpc="http://joyrpc.io/schema/joyrpc"
xsi:schemaLocation=
"http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://joyrpc.io/schema/joyrpc
http://joyrpc.io/schema/joyrpc/joyrpc.xsd">
</beans>
说明:上面是完整的schema描述,下面示例中采用
<joyrpc/>
标签 代表此schema。
-
编写服务端代码
public class ServerMain { private static final Logger LOGGER = LoggerFactory.getLogger(ClientMain.class); public static void main(String[] args) throws Exception { ClassPathXmlApplicationContext appContext = new ClassPathXmlApplicationContext("/joyrpc-provider.xml"); LOGGER.info("服务端启动完成!"); System.in.read(); } }
-
编写服务端配置
resources目录下定义配置文件:joyrpc-provider.xml
<beans> <!-- 实现类 --> <bean id="demoServiceImpl" class="io.joyrpc.service.impl.DemoServiceImpl"/> <!-- 注册中心 --> <joyrpc:registry id="joyRpcRegistry" address="memory://127.0.0.1" registry="memory"/> <!-- 服务端 端口默认22000 --> <joyrpc:server id="joyRpcServer"/> <!-- 发布服务 alias可修改 --> <joyrpc:provider id="demoService" interface="io.joyrpc.service.DemoService" alias="joyrpc-demo" ref="demoServiceImpl" server="joyRpcServer"></joyrpc:provider> </beans>
-
编写客户端代码
public class ClientMain { private static final Logger LOGGER = LoggerFactory.getLogger(ClientMain.class); public static void main(String[] args) throws Exception { ClassPathXmlApplicationContext appContext = new ClassPathXmlApplicationContext("/joyrpc-consumer.xml"); DemoService service = (DemoService) appContext.getBean("demoService"); try { String result = service.sayHello("hello"); LOGGER.info("response msg from server :{}", result); } catch (Exception e) { } System.in.read();//终端输入任意字符,shutdown进程 } }
-
编写客户端配置
resources目录下定义配置文件:joyrpc-consumer.xml
<beans> <!-- 注册中心 --> <joyrpc:registry id="joyprpcRegistry" address="memory://127.0.0.1" registry="memory"/> <!-- 调用者配置 --> <joyrpc:consumer id="demoService" interface="io.joyrpc.service.DemoService" alias="joyrpc-demo"></joyrpc:consumer> </beans>
@Provider(name = "provider-demoService", alias = "2.0-Boot")
public class DemoServiceImpl implements DemoService {
public String sayHello(String str){
System.out.println("Hi " + str + ", request from consumer.");
return return "Hi " + str + ", response from provider. ";
}
}
rpc.packages[0]=io.joyrpc.service.DemoService
# server
rpc.server.port=22000
# provider,可以配置额外的参数或覆盖@Provider提供的内容
#rpc.providers[0].name=provider-demoService
#rpc.providers[0].alias=2.0-Boot
# registry
rpc.registry.registry=memory
@SpringBootApplication
public class BootServer {
public static void main(String[] args) throws InterruptedException {
ConfigurableApplicationContext ctx = SpringApplication.run(BootServer.class, args);
Thread.currentThread().join();
}
}
#应用名称
spring.application.name=joyrpc-example-client
#registry
rpc.registry.registry=memory
#消费者
rpc.consumers[0].interfaceClazz=io.joyrpc.service.DemoService
rpc.consumers[0].alias=2.0-Boot
@SpringBootApplication
public class BootClient {
public static void main(String[] args) {
ConfigurableApplicationContext run = SpringApplication.run(BootClient.class, args);
DemoService consumer = run.getBean(DemoService.class);
String hello = service.sayHello("hello");
System.out.println(hello);
}
}
分别启动服务端和客户端,观察运行效果。
服务端将打印:
Hi hello, request from consumer.
客户端将打印:
Hi hello, response from provider
更多示例请参考:example