liubao68 closed pull request #23: update transparent rpc consumer
URL: https://github.com/apache/incubator-servicecomb-docs/pull/23
This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:
As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):
diff --git a/java-chassis-reference/zh_CN/build-consumer/code-first.md
b/java-chassis-reference/zh_CN/build-consumer/code-first.md
index bb6e9a2..d87d7ae 100644
--- a/java-chassis-reference/zh_CN/build-consumer/code-first.md
+++ b/java-chassis-reference/zh_CN/build-consumer/code-first.md
@@ -15,26 +15,29 @@
```java
import org.springframework.stereotype.Component;
import org.apache.servicecomb.foundation.common.utils.BeanUtils;
- import org.apache.servicecomb.foundation.common.utils.Log4jUtils;
- import org.apache.servicecomb.provider.pojo.RpcReference;
- import org.apache.servicecomb.samples.common.schema.Hello;
- import org.apache.servicecomb.samples.common.schema.models.Person;
+import org.apache.servicecomb.foundation.common.utils.Log4jUtils;
+import org.apache.servicecomb.provider.pojo.RpcReference;
+import org.apache.servicecomb.samples.common.schema.Hello;
+import org.apache.servicecomb.samples.common.schema.models.Person;
+
@Component
- public class CodeFirstConsumerMain {
-@RpcReference(microserviceName = "codefirst", schemaId = "codeFirstHello")
- private static Hello hello;
-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));
- }
-public static void init() throws Exception {
- Log4jUtils.init();
- BeanUtils.init();
- }
- }
+public class CodeFirstConsumerMain {
+ @RpcReference(microserviceName = "codefirst", schemaId = "codeFirstHello")
+ private static Hello hello;
+
+ 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));
+ }
+
+ public static void init() throws Exception {
+ Log4jUtils.init();
+ BeanUtils.init();
+ }
+}
```
在以上代码中,服务消费者已经取得了服务提供者的服务接口Hello,并在代码中声明一个Hello类型的成员。通过在hello上使用RPCReference注解指明微服务名称和schemaId,ServiceComb框架可以在程序启动时从服务中心获取到对应的服务提供者实例信息,并且生成一个代理注入到hello中,用户可以像调用本地类一样调用远程服务。
diff --git
a/java-chassis-reference/zh_CN/build-consumer/develop-consumer-using-rpc.md
b/java-chassis-reference/zh_CN/build-consumer/develop-consumer-using-rpc.md
index 0900c5f..b308390 100644
--- a/java-chassis-reference/zh_CN/build-consumer/develop-consumer-using-rpc.md
+++ b/java-chassis-reference/zh_CN/build-consumer/develop-consumer-using-rpc.md
@@ -1,67 +1,47 @@
-# 使用RPC方式开发服务消费者
+# 使用透明RPC方式开发服务消费者
## 概念阐述
-RPC开发方式允许用户通过在服务接口上标注注解来生成服务提供者代理,从而进行服务的调用。
-
-## 示例代码
-
-只需要声明一个服务接口类型的成员,并且使用@RpcReference标注该成员,声明依赖的微服务及schemaId,即可进行服务调用,示例代码如下:
-
-* 透明RPC 客户端代码示例
+透明RPC开发模式允许用户通过简单的java interface像本地调用一样进行服务调用。
+透明RPC仅仅是一种开发模式:
+* 与使用highway还是RESTful传输没有关联
+* 与producer使用透明RPC/Jax-RS还是SpringMVC模式开发没有关联
+* 也与producer代码是否实现这个interface没有关联。
+
+透明RPC开发模式与spring cloud的feign类似,不过更简单,因为不必在这个interface中增加任何RESTful annotation。
+## 在spring bean中通过@RpcReference声明
```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();
- }
+public class SomeBean {
+ ......
+
+ @RpcReference(microserviceName = "helloService", schemaId = "helloSchema")
+ private Hello hello;
+
+ ......
}
```
-
->在以上代码中,服务消费者已经取得了服务提供者的服务接口`Hello 、Compute`,并在代码中声明一个`Hello`类型 和 Compute
类型的成员。通过在`hello 和
compute`上使用`@RPCReference`注解指明微服务名称和schemaId,ServiceComb框架可以在程序启动时从服务中心获取到对应的服务提供者实例信息,并且生成一个代理注入到hello和compute中,用户可以像调用本地类一样调用远程服务。
-
-* JAX-RS 客户端示例代码 :
-
+## 脱离spring bean,直接通过api声明
```java
-@Component
-public class JaxrsConsumerMain {
-
- @RpcReference(microserviceName = "jaxrs", schemaId = "jaxrsHello")
- private static Hello hello;
+Hello hello = Invoker.createProxy("helloService", "helloSchema", Hello.class);
+```
- 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));
- }
+## reactive
+只需要使用jdk的CompletableFuture对返回值进行包装即可
+```java
+interface Hello {
+ CompletableFuture<String> sayHi(String name);
+}
+```
- public static void init() throws Exception {
- Log4jUtils.init();
- BeanUtils.init();
- }
+同一个interface中,可以同时声明同一个方法的reactive和同步原型
+因为要求方法名与契约中的operationId一一对应,而仅有返回值类型不同,在java中是非法的,所以需要修改方法名,并通过swagger
annotation来声明真正的operationId
+```java
+interface Hello {
+ String sayHi(String name);
+
+ @ApiOperation(nickname = "sayHi", value = "reactive method for sayHi")
+ CompletableFuture<String> asyncSayHi(String name);
}
```
->我们发现上述代码, `JAX-RS` 客户端也是使用 `RPC` 的方式访问的服务端方法。 其实不管 provider
-是使用 `JAX-RS` 、 `Spring mvc` 还是 `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