liubao68 commented on a change in pull request #13: 补充客户端示例 AsyncRestTemplate
,对其他示例进行完善
URL:
https://github.com/apache/incubator-servicecomb-docs/pull/13#discussion_r200529505
##########
File path:
java-chassis-reference/zh_CN/build-consumer/develop-consumer-using-rpc.md
##########
@@ -6,10 +6,45 @@ RPC开发方式允许用户通过在服务接口上标注注解来生成服务
只需要声明一个服务接口类型的成员,并且使用@RpcReference标注该成员,声明依赖的微服务及schemaId,即可进行服务调用,示例代码如下:
+* 透明RPC 客户端代码示例
+
+```java
+@Component
+public class PojoConsumerMain {
+
+ @RpcReference(microserviceName = "hello", schemaId = "hello")
+ private static Hello hello;
+
+ @RpcReference(microserviceName = "hello", schemaId = "codeFirstCompute")
+ public static Compute compute;
+
+ public static void main(String[] args)
+ throws Exception {
+ init();
+ System.out.println(hello.sayHi("Java Chassis"));
+ Person person = new Person();
+ person.setName("ServiceComb/Java Chassis");
+ System.out.println(hello.sayHello(person));
+ System.out.println("a=1, b=2, result=" + compute.add(1, 2));
+ }
+
+ public static void init()
+ throws Exception {
+ Log4jUtils.init();
+ BeanUtils.init();
+ }
+}
+```
+
+在以上代码中,服务消费者已经取得了服务提供者的服务接口`Hello 、Compute`,并在代码中声明一个`Hello`类型 和 Compute
类型的成员。通过在`hello 和
compute`上使用`@RPCReference`注解指明微服务名称和schemaId,ServiceComb框架可以在程序启动时从服务中心获取到对应的服务提供者实例信息,并且生成一个代理注入到hello和compute中,用户可以像调用本地类一样调用远程服务。
+
+* JAX-RS 客户端示例代码 :
Review comment:
下面这段代码仍然是RPC方式访问。可以适当的说明一下,不管provider使用JAX
RS、SPringMVC还是RPC,客户端都可以通过RPC方式访问。 只需要服务端定义的时候,声明一下RPC接口。 服务端定义一个接口是比较好的开发实践。
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services