You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicecomb.apache.org by ni...@apache.org on 2019/05/26 04:17:49 UTC

[servicecomb-docs] branch master updated: add more descriptions about handler development

This is an automated email from the ASF dual-hosted git repository.

ningjiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/servicecomb-docs.git


The following commit(s) were added to refs/heads/master by this push:
     new 401e3be  add more descriptions about handler development
401e3be is described below

commit 401e3be97b83014d2349c1711dd347b841374bb2
Author: liubao <ba...@huawei.com>
AuthorDate: Thu May 9 14:38:36 2019 +0800

    add more descriptions about handler development
---
 .../en_US/references-handlers/intruduction.md      | 73 ++++++++++++++++++++-
 .../zh_CN/references-handlers/intruduction.md      | 76 +++++++++++++++++++++-
 2 files changed, 147 insertions(+), 2 deletions(-)

diff --git a/java-chassis-reference/en_US/references-handlers/intruduction.md b/java-chassis-reference/en_US/references-handlers/intruduction.md
index 9835e7c..0fc8110 100644
--- a/java-chassis-reference/en_US/references-handlers/intruduction.md
+++ b/java-chassis-reference/en_US/references-handlers/intruduction.md
@@ -1,9 +1,80 @@
 ## Handlers Reference
 Handlers are the core components of ServiceComb, which form the basis of service operation and control. ServiceComb handles load balancing, fuse tolerance, flow control, and more through the Handlers.
 
-## Development Handlers
+### Enable Handlers
+There are Consumer Handlers and Provider Handlers. Enable handlers in microservice.yaml:
+
+```
+servicecomb:
+  handler:
+    chain:
+      Consumer:
+        default: qps-flowcontrol-consumer,loadbalance
+      Provider: 
+        default: qps-flowcontrol-provider
+ ```
+
+We can also enable different handlers for each microservice, 
+
+```
+servicecomb:
+  handler:
+    chain:
+      Consumer:
+        default: auth,qps-flowcontrol-consumer,loadbalance
+        service:
+          authentication-server: qps-flowcontrol-consumer,loadbalance
+```
+
+Requests to authentication-server, auth handler is enabled, and others not. 
+
+### Development Handlers
 The developer's custom handlers consists of the following steps. Since the core component of ServiceComb is the handlers, developers can refer to the implementation of the handlers directory to learn more about the Handlers. Here are a few key steps to summarize:
 
 * Implement Handler interface
+
+```
+public class AuthHandler implements Handler {
+  @Override
+  public void handle(Invocation invocation, AsyncResponse asyncResponse) throws Exception {
+    String token = invocation.getContext(Constants.CONTEXT_HEADER_AUTHORIZATION);
+    if (token == null) {
+      asyncResponse.consumerFail(new InvocationException(403, "forbidden", "not authenticated"));
+      return;
+    }
+    Jwt jwt = JwtHelper.decode(token);
+    try {
+      jwt.verifySignature(BeanUtils.getBean("authSigner"));
+    } catch (InvalidSignatureException e) {
+      asyncResponse.consumerFail(new InvocationException(403, "forbidden", "not authenticated"));
+      return;
+    }
+    invocation.next(asyncResponse);
+  }
+}
+```
+
 * Add *.handler.xml file, give handler a name
+
+
+```
+<config>
+  <handler id="auth"
+    class="org.apache.servicecomb.authentication.gateway.AuthHandler" />
+</config>
+```
+
+
 * Enable the newly added Handlers in microservice.yaml
+
+
+```
+servicecomb:
+  handler:
+    chain:
+      Consumer:
+        default: auth,loadbalance
+        service:
+          authentication-server: loadbalance
+```
+
diff --git a/java-chassis-reference/zh_CN/references-handlers/intruduction.md b/java-chassis-reference/zh_CN/references-handlers/intruduction.md
index 1a3131f..73bc03b 100644
--- a/java-chassis-reference/zh_CN/references-handlers/intruduction.md
+++ b/java-chassis-reference/zh_CN/references-handlers/intruduction.md
@@ -1,9 +1,83 @@
 ## 处理链参考
+
 处理链(Handlers)是ServiceComb的核心组成部分,它们构成服务运行管控的基础。ServiceComb通过处理链来处理负载均衡、熔断容错、流量控制等。
 
-## 开发处理链
+### 处理链配置
+处理链分为Consumer和Provider,分别配置如下:
+
+```
+servicecomb:
+  handler:
+    chain:
+      Consumer:
+        default: qps-flowcontrol-consumer,loadbalance
+      Provider: 
+        default: qps-flowcontrol-provider
+ ```
+
+通过名字配置处理链,可以根据需要调整处理链的顺序,配置在前面的处理链先执行。
+
+上述配置指定目标微服务缺省处理链,开发者还可以对特定的微服务配置不同的处理链,比如:
+
+```
+servicecomb:
+  handler:
+    chain:
+      Consumer:
+        default: auth,qps-flowcontrol-consumer,loadbalance
+        service:
+          authentication-server: qps-flowcontrol-consumer,loadbalance
+```
+
+对于转发到authentication-server的请求,不经过auth处理链,转发到其他的微服务的请求,则经过auth处理链。
+
+### 开发处理链
 开发者自定义处理链包含如下几个步骤。由于ServiceComb的核心组成就是处理链,开发者可以参考handlers目录的实现详细了解处理链。下面简单总结下几个关键步骤:
 
 * 实现Handler接口
+
+```
+public class AuthHandler implements Handler {
+  @Override
+  public void handle(Invocation invocation, AsyncResponse asyncResponse) throws Exception {
+    String token = invocation.getContext(Constants.CONTEXT_HEADER_AUTHORIZATION);
+    if (token == null) {
+      asyncResponse.consumerFail(new InvocationException(403, "forbidden", "not authenticated"));
+      return;
+    }
+    Jwt jwt = JwtHelper.decode(token);
+    try {
+      jwt.verifySignature(BeanUtils.getBean("authSigner"));
+    } catch (InvalidSignatureException e) {
+      asyncResponse.consumerFail(new InvocationException(403, "forbidden", "not authenticated"));
+      return;
+    }
+    invocation.next(asyncResponse);
+  }
+}
+```
+
 * 增加*.handler.xml文件,给Handler取一个名字
+
+```
+<config>
+  <handler id="auth"
+    class="org.apache.servicecomb.authentication.gateway.AuthHandler" />
+</config>
+```
+
 * 在microservice.yaml中启用新增加的处理链
+
+```
+servicecomb:
+  handler:
+    chain:
+      Consumer:
+        default: auth,loadbalance
+        service:
+          authentication-server: loadbalance
+```
+
+
+
+