content/references/java-chassis/en_US/edge/by-servicecomb-sdk/index.html (606 lines of code) (raw):
<!DOCTYPE html>
<html class="writer-html5" lang="en" >
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="shortcut icon" href="../../img/favicon.ico" />
<title>Using Edge Service - ServiceComb Java Chassis Developers Guide</title>
<link rel="stylesheet" href="../../css/theme.css" />
<link rel="stylesheet" href="../../css/theme_extra.css" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.5.0/styles/github.min.css" />
<script>
// Current page data
var mkdocs_page_name = "Using Edge Service";
var mkdocs_page_input_path = "edge/by-servicecomb-sdk.md";
var mkdocs_page_url = null;
</script>
<script src="../../js/jquery-3.6.0.min.js" defer></script>
<!--[if lt IE 9]>
<script src="../../js/html5shiv.min.js"></script>
<![endif]-->
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.5.0/highlight.min.js"></script>
<script>hljs.initHighlightingOnLoad();</script>
</head>
<body class="wy-body-for-nav" role="document">
<div class="wy-grid-for-nav">
<nav data-toggle="wy-nav-shift" class="wy-nav-side stickynav">
<div class="wy-side-scroll">
<div class="wy-side-nav-search">
<a href="../.." class="icon icon-home"> ServiceComb Java Chassis Developers Guide
</a><div role="search">
<form id ="rtd-search-form" class="wy-form" action="../../search.html" method="get">
<input type="text" name="q" placeholder="Search docs" title="Type search term here" />
</form>
</div>
</div>
<div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="Navigation menu">
<ul>
<li class="toctree-l1"><a class="reference internal" href="../..">Introduction</a>
</li>
</ul>
<p class="caption"><span class="caption-text">Getting Started</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../start/terminology/">Glossary</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../start/architecture/">Architecture</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../start/development-environment/">Development environment</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../start/first-sample/">Develop the first microservice</a>
</li>
</ul>
<p class="caption"><span class="caption-text">Development Service Provider</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../build-provider/definition/service-definition/">Service definition</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../build-provider/define-contract/">Service contract definition</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../build-provider/code-first/">Implicit API definition</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../build-provider/swagger-annotation/">Use Swagger annotations</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../build-provider/springmvc/">Develop with SpringMVC</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../build-provider/jaxrs/">Develop with JAX-RS</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../build-provider/transparent-rpc/">Develop with Transparent RPC</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../build-provider/interface-constraints/">Interface definition and data type</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../build-provider/listen-address-and-publish-address/">Service listening address and publishing address</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../build-provider/thread-pool/">Thread pool</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="#">Service Configuration</a>
<ul>
<li class="toctree-l2"><a class="reference internal" href="../../build-provider/configuration/ratelimite-strategy/">Rate Limiting Policy</a>
</li>
<li class="toctree-l2"><a class="reference internal" href="../../build-provider/configuration/downgrade-strategy/">Fallback Policy</a>
</li>
<li class="toctree-l2"><a class="reference internal" href="../../build-provider/configuration/parameter-validator/">Parameter Validator</a>
</li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../build-provider/bootup/">Boot-up Process</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../build-provider/access-log-configuration/">Access Log Configuration</a>
</li>
</ul>
<p class="caption"><span class="caption-text">Writing Service Consumer</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../build-consumer/common-configuration/">Consumer common configuration</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../build-consumer/using-resttemplate/">Using Rest Template</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../build-consumer/using-AsyncRestTemplate/">Using AsyncRestTemplate</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../build-consumer/develop-consumer-using-rpc/">Using with RPC</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../build-consumer/with-contract/">Contract</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="#">Invoke control</a>
<ul>
<li class="toctree-l2"><a class="reference internal" href="../../build-consumer/circuit-breaker/">Circuit Breaker</a>
</li>
<li class="toctree-l2"><a class="reference internal" href="../../build-consumer/flow-control/">Flow Control</a>
</li>
<li class="toctree-l2"><a class="reference internal" href="../../build-consumer/fault-injection/">Fault Injection</a>
</li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../build-consumer/3rd-party-service-invoke/">Invoke 3rd-party REST services</a>
</li>
</ul>
<p class="caption"><span class="caption-text">Transports</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../transports/rest-over-servlet/">REST over Servlet</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../transports/rest-over-vertx/">REST over Vertx</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../transports/highway-rpc/">Highway</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../transports/http2/">HTTP2</a>
</li>
</ul>
<p class="caption"><span class="caption-text">General Development</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../general-development/visit-sc/">Access Service Center</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../general-development/metrics/">Metrics</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../general-development/microservice-invocation-chain/">Microservice invocation chain</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../general-development/customized-tracing/">Customized-Tracing</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../general-development/local-develop-test/">Local development and testing</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../general-development/http-filter/">Http Filter</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../general-development/file-upload/">File Uploading</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../general-development/file-download/">File Downloading</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../general-development/reactive/">Reactive Programing</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../general-development/dnsconfig/">DNS Custom Configuration</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../general-development/dai-li-she-zhi/">Proxy Settings</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../general-development/report-framework-version/">Report framework version</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../general-development/cross-app-invocation/">Cross-application invocation</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../general-development/secret-field/">Customized serialization and deserialization</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../general-development/context/">Using Context to pass control messages</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../general-development/produceprocess/">Return value serialization extension</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../general-development/CORS/">CORS mechanism</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../general-development/AlarmEvent/">Get fuse and instance isolation alarm event information</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../general-development/shutdown/">Shutdown gracefully</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../general-development/error-handling/">Handling exceptions</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../general-development/multienvironment/">Multi-environment isolation between microservice instances</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../general-development/thread-model/">Thread Model</a>
</li>
</ul>
<p class="caption"><span class="caption-text">Configuration</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../config/general-config/">General config</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../config/inject-config/">Configuration injection</a>
</li>
</ul>
<p class="caption"><span class="caption-text">Service Capability Open</span></p>
<ul class="current">
<li class="toctree-l1"><a class="reference internal" href="../open-service/">Intruductions</a>
</li>
<li class="toctree-l1 current"><a class="reference internal current" href="./">Using Edge Service</a>
<ul class="current">
<li class="toctree-l2"><a class="reference internal" href="#developing-edge-service">Developing Edge Service</a>
</li>
<li class="toctree-l2"><a class="reference internal" href="#work-process">work process</a>
</li>
<li class="toctree-l2"><a class="reference internal" href="#custom-routing-rules">Custom routing rules</a>
<ul>
<li class="toctree-l3"><a class="reference internal" href="#using-defaultedgedispatcher">Using DefaultEdgeDispatcher</a>
</li>
<li class="toctree-l3"><a class="reference internal" href="#using-urlmappededgedispatcher">Using URLMappedEdgeDispatcher</a>
</li>
<li class="toctree-l3"><a class="reference internal" href="#custom-dispatcher">Custom Dispatcher</a>
</li>
<li class="toctree-l3"><a class="reference internal" href="#perform-authentication-and-other-business-processing">Perform authentication and other business processing</a>
</li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="#deployment-example">Deployment example</a>
</li>
<li class="toctree-l2"><a class="reference internal" href="#operating-mode">Operating mode</a>
<ul>
<li class="toctree-l3"><a class="reference internal" href="#reactive-default">reactive (default)</a>
</li>
<li class="toctree-l3"><a class="reference internal" href="#thread-pool">Thread Pool</a>
</li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="#demo-function-description">DEMO Function Description</a>
<ul>
<li class="toctree-l3"><a class="reference internal" href="#1register-dispatcher">1.Register Dispatcher</a>
</li>
<li class="toctree-l3"><a class="reference internal" href="#getorder">getOrder</a>
</li>
<li class="toctree-l3"><a class="reference internal" href="#init">init</a>
</li>
<li class="toctree-l3"><a class="reference internal" href="#2-forwarding-request">2. Forwarding request</a>
</li>
<li class="toctree-l3"><a class="reference internal" href="#3-setting-compatibility-rules">3. Setting compatibility rules</a>
</li>
<li class="toctree-l3"><a class="reference internal" href="#4authentication">4.Authentication</a>
</li>
</ul>
</li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="../nginx/">Using confd and Nginx as edge services</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../zuul/">Use zuul as edge services</a>
</li>
</ul>
<p class="caption"><span class="caption-text">Service Packing and Running</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../packaging/standalone/">Standalone mode</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../packaging/web-container/">WEB container mode</a>
</li>
</ul>
<p class="caption"><span class="caption-text">Micro Service Security</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../security/tls/">Using TLS</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../security/rsa/">Using RSA certification</a>
</li>
</ul>
<p class="caption"><span class="caption-text">Using java chassis in Spring Boot</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../using-java-chassis-in-spring-boot/using-java-chassis-in-spring-boot/">Intruductions</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../using-java-chassis-in-spring-boot/components-for-spring-boot/">spring boot starter for java-chassis</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../using-java-chassis-in-spring-boot/java-application/">JAVA application development</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../using-java-chassis-in-spring-boot/web-application/">Web development method development</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../using-java-chassis-in-spring-boot/diff-between-java-web/">The difference between JAVA application method and Web development method</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../using-java-chassis-in-spring-boot/diff-spring-mvc/">The difference in Spring MVC mode</a>
</li>
</ul>
<p class="caption"><span class="caption-text">Handlers reference</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../references-handlers/intruduction/">Intruductions</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../references-handlers/loadbalance/">Load Balancing</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../references-handlers/publickey/">Public key authentication</a>
</li>
</ul>
<p class="caption"><span class="caption-text">FAQ</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../question-and-answer/question_answer/">Q & A</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../question-and-answer/faq/">FAQ</a>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../question-and-answer/interface-compatibility/">Micro Service Interface Compatibility FAQ</a>
</li>
</ul>
</div>
</div>
</nav>
<section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">
<nav class="wy-nav-top" role="navigation" aria-label="Mobile navigation menu">
<i data-toggle="wy-nav-top" class="fa fa-bars"></i>
<a href="../..">ServiceComb Java Chassis Developers Guide</a>
</nav>
<div class="wy-nav-content">
<div class="rst-content"><div role="navigation" aria-label="breadcrumbs navigation">
<ul class="wy-breadcrumbs">
<li><a href="../.." class="icon icon-home" alt="Docs"></a> »</li>
<li>Service Capability Open »</li>
<li>Using Edge Service</li>
<li class="wy-breadcrumbs-aside">
</li>
</ul>
<hr/>
</div>
<div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
<div class="section" itemprop="articleBody">
<h1 id="using-edge-service-for-edge-services">Using Edge Service for Edge Services</h1>
<p>Edge Service is the JAVA gateway service development framework provided by ServiceComb. As the external interface of the entire microservice system, the Edge Service provides services to end users, accesses RESTful requests, and forwards them to internal microservices. The Edge Service is provided in the form of a development framework. Developers can easily build an Edge Service service and define routing and forwarding rules with a simple configuration. At the same time, Edge Service supports powerful expansion capabilities, and services such as service mapping, request parsing, encryption and decryption, and authentication can be extended.</p>
<p>The Edge Service itself is also a microservice that is subject to all microservice development rules. It can be deployed as a multi-instance, and the front-end uses a load balancing device for load distribution. It can also be deployed as a master and backup, and directly access user requests. Developers can plan according to the logic and service access and networking conditions carried by the Edge Service.</p>
<h2 id="developing-edge-service">Developing Edge Service</h2>
<p>Developing Edge Service is similar to developing a normal microservice. Developers can import [ServiceComb Edge Service Demo] (https://github.com/apache/incubator-servicecomb-java-chassis/tree/master/demo/demo -edge) Start. Building a project from scratch involves the following steps:</p>
<ul>
<li>Configure dependencies</li>
</ul>
<p>By adding edge-core dependencies to your project, you can start the Edge Service. When the Edge Service requests forwarding, it will go through the processing chain, so it can also join the dependencies of the relevant processing chain modules. The following example adds the load balancing processing chain. This is a must.</p>
<pre><code><dependency>
<groupId>org.apache.servicecomb</groupId>
<artifactId>edge-core</artifactId>
</dependency>
<dependency>
<groupId>org.apache.servicecomb</groupId>
<artifactId>handler-loadbalance</artifactId>
</dependency>
</code></pre>
<ul>
<li>Define the startup class</li>
</ul>
<p>Just like developing a normal microservice, you can pull the service by loading Spring.</p>
<pre><code>public class EdgeMain {
public static void main(String[] args) throws Exception {
Log4jUtils.init();
BeanUtils.init();
}
}
</code></pre>
<ul>
<li>Increase the configuration file microservie.yaml
The Edge Service itself is also a microservice that follows the rules of microservice lookup and will register itself. Note that APPLICAIONT_ID is the same as the microservice that needs to be forwarded. In the following configuration, the address that the Edge Service listens to, the processing chain, and so on are specified. The auth processing chain is a custom processing chain in the DEMO project for implementing authentication. At the same time, the auth service itself, without going through this processing chain, is equivalent to not authenticating.</li>
</ul>
<pre><code>APPLICATION_ID: edge
service_description:
name: edge
version: 0.0.1
servicecomb:
service:
registry:
address: http://127.0.0.1:30100
rest:
address: 127.0.0.1:18080
handler:
chain:
Consumer:
default: auth,loadbalance
service:
auth: loadbalance
</code></pre>
<h2 id="work-process">work process</h2>
<p>The workflow of the Edge Service is as follows, the blue background part is executed in the Eventloop thread, and the yellow background part:
* If working in reactive mode, execute directly in the Eventloop thread
* If working in thread pool mode, execute in the thread pool thread
<img alt="" src="../../assets/workFlow.png" /></p>
<h2 id="custom-routing-rules">Custom routing rules</h2>
<p>The core job of using the Edge Service is to configure routing rules. The rules are different, and the rules are different.
A routing rule consists of a series of AbstractEdgeDispatchers. The Edge Service provides several common Dispatchers that can be enabled through configuration. If these Dispatchers do not meet the needs of the business scenario, they can be customized.</p>
<h3 id="using-defaultedgedispatcher">Using DefaultEdgeDispatcher</h3>
<p>DefaultEdgeDispatcher is a very simple and easy to manage Dispatcher. With this Dispatcher, users do not need to manage forwarding rules dynamically. It is very convenient to apply to actual business scenarios. This is also a recommended management mechanism. It contains the following configuration items:</p>
<pre><code>servicecomb:
http:
dispatcher:
edge:
default:
enabled: true
prefix: rest
withVersion: true
prefixSegmentCount: 1
</code></pre>
<p>Examples and meanings of these common configuration items are as follows:
* [prefix=rest;withVersion=true;prefixSegmentCount=1] The URL provided by the microservice xService is: /xService/v1/abc, the address accessed by the Edge is /rest/xService/v1/abc, and the request is only forwarded to [1.0 .0-2.0.0) version of the microservice instance.
* [prefix=rest;withVersion=true;prefixSegmentCount=2] The URL provided by the microservice xService is: /v1/abc, the address accessed by the Edge is /rest/xService/v1/abc, and the request is only forwarded to [1.0.0] -2.0.0) version of the microservice instance.
* [prefix=rest;withVersion=true;prefixSegmentCount=3] The URL provided by the microservice xService is: /abc, the address accessed by Edge is /rest/xService/v1/abc, and the request is forwarded only to [1.0.0-2.0] .0) version of the microservice instance.
* [prefix=rest;withVersion=false;prefixSegmentCount=1] The URL provided by the microservice xService is: /xService/v1/abc, the address accessed by the Edge is /rest/xService/v1/abc, and the request may be forwarded to any micro Service instance.
* [prefix=rest;withVersion=false;prefixSegmentCount=2] The URL provided by the microservice xService is: /v1/abc, the address accessed by Edge is /rest/xService/v1/abc, and the request may be forwarded to any microservice. Example.
* [prefix=rest;withVersion=false;prefixSegmentCount=2] The URL provided by the microservice xService is: /abc, the address accessed by the Edge is /rest/xService/abc, and the request may be forwarded to any microservice instance.</p>
<p>The withVersion configuration item provides a client grayscale rule that allows the client to specify which server version to access. The Edge Service also includes the ability to route based on interface compatibility automatically, and requests are forwarded to instances that contain the interface. Assume that a microservice, compatibility plan for all high versions must be compatible with the lower version, deploy the following version of the instance:</p>
<ul>
<li>
<p>1.0.0, provides operation1</p>
</li>
<li>
<p>1.1.0, provided operation1, operation2</p>
</li>
</ul>
<p>When Edge Service forwards operation1, it automatically uses the rule of 1.0.0+ to filter the instance.</p>
<p>When Edge Service forwards operation2, it automatically uses the rules of 1.1.0+ to filter instances.</p>
<p>The above process does not require any intervention and is fully automated to avoid forwarding the new version of the operation to the instance of the old version.</p>
<h3 id="using-urlmappededgedispatcher">Using URLMappedEdgeDispatcher</h3>
<p>URLMappedEdgeDispatcher allows users to configure mappings between URLs and microservices. It is very flexible to define which URLs are forwarded to which microservices. It contains the following configuration items:</p>
<pre><code>servicecomb:
http:
dispatcher:
edge:
url:
enabled: true
mappings:
businessV1:
prefixSegmentCount: 1
path: "/url/business/v1/.*"
microserviceName: business
versionRule: 1.0.0-2.0.0
businessV2:
prefixSegmentCount: 1
path: "/url/business/v2/.*"
microserviceName: business
versionRule: 2.0.0-3.0.0
</code></pre>
<p>The meaning of the businessV1 configuration item is that the request with the request path of /usr/business/v1/.<em> is forwarded to the microservice of business and only forwarded to the instance with version number 1.0.0-2.0.0 (excluding 2.0). .0). The URL when forwarding is /business/v1/.</em>. Path uses the JDK regular expression, and you can view the description of the Pattern class. prefixSegmentCount indicates the number of URL segments of the prefix, and the prefix is not included in the forwarded URL path. Three forms of versionRule can be specified. 2.0.0-3.0.0 indicates the version range, including 2.0.0, but does not contain 3.0.0; 2.0.0+ indicates a version greater than 2.0.0, including 2.0.0; 2.0.0 means forwarding only to 2.0.0 version. 2, 2.0 is equivalent to 2.0.0.</p>
<p>As can be seen from the above configuration, URLMappedEdgeDispatcher also supports client grayscale. Of course, there will be more configuration items than DefaultEdgeDispatcher. The URLMappedEdgeDispatcher supports dynamic configuration modification of the configuration center to adjust routing rules.</p>
<h3 id="custom-dispatcher">Custom Dispatcher</h3>
<p>Customizing the Dispatcher involves two steps:</p>
<ol>
<li>Implement AbstractEdgeDispatcher</li>
<li>Release via SPI: add the file META-INF/services/org.apache.servicecomb.transport.rest.vertx.VertxHttpDispatcher and write the implementation class</li>
</ol>
<p>Detailed code details can be found in the following section "DEMO Functional Description". Developers can also refer to the Code such as DefaultEdgeDispatcher to define their Dispatcher.</p>
<h3 id="perform-authentication-and-other-business-processing">Perform authentication and other business processing</h3>
<p>Through the Edge Service workflow, you can see that the Edge Service features can be extended in a variety of ways, including Dispatcher, HttpServerFilter, Handler, HttpClientFilter, and more. More common and straightforward is to extend through Handler. DEMO shows how to implement authentication through Handler extensions. Detailed code details can be found in the following section "DEMO Functional Description".<Paste></p>
<h2 id="deployment-example">Deployment example</h2>
<p><img alt="" src="../../assets/deployment.png" /></p>
<h2 id="operating-mode">Operating mode</h2>
<h3 id="reactive-default">reactive (default)</h3>
<p>The Edge Service works by default in the high-performance reactive mode. This mode requires that the business code working in the Edge Service forwarding process cannot have any blocking operations, including:</p>
<ul>
<li>
<p>Remote synchronization calls, such as an asynchronous query database, synchronous call microservices, or synchronous query remote cache, etc.</p>
</li>
<li>
<p>any sleep call</p>
</li>
<li>
<p>any wait call</p>
</li>
<li>
<p>Oversized loop</p>
</li>
</ul>
<p>The underlying Edge Service is based on netty's vertx. The above constraint is netty's reactive mode constraint.</p>
<p><img alt="" src="../../assets/reactive.png" /></p>
<h3 id="thread-pool">Thread Pool</h3>
<p>If the business model cannot meet the reactive requirements, you need to use the thread pool mode.</p>
<p>In this case, you need to configure it in the microservice.yaml of the Edge Service:</p>
<pre><code>servicecomb:
executors:
default: servicecomb.executor.groupThreadPool
</code></pre>
<p>Here servicecomb.executor.groupThreadPool is the beanId of the spring bean corresponding to the default thread pool built into ServiceComb; the service can customize its thread pool and declare it as a bean whose beanId can also be configured here.</p>
<p><img alt="" src="../../assets/threadPool.png" /></p>
<h2 id="demo-function-description">DEMO Function Description</h2>
<p>Please refer to the edge service demo on GitHub:</p>
<p><a href="https://github.com/ServiceComb/ServiceComb-Java-Chassis/tree/master/demo/demo-edge">https://github.com/ServiceComb/ServiceComb-Java-Chassis/tree/master/demo/demo-edge</a></p>
<p>The demo contains the following projects:</p>
<ul>
<li>authentication: microservice: authentication server</li>
<li>edge-service</li>
<li>hiboard-business-1.0.0 microservices: business, version 1.0.0, operation add</li>
<li>hiboard-business-1.1.0 microservices: business, version 1.1.0, operation add/dec</li>
<li>hiboard-business-2.0.0 microservices: business, version 2.0.0, operation add/dec</li>
<li>hiboard-consumer as a normal httpclient, not a servicecomb consumer</li>
<li>hiboard-model non-micro service, just some public models</li>
</ul>
<p>Access different versions of microservices through edge-service and confirm that the correct instance handles them.</p>
<h3 id="1register-dispatcher">1.Register Dispatcher</h3>
<p>Implement the interface org.apache.servicecomb.transport.rest.vertx.VertxHttpDispatcher, or inherit from org.apache.servicecomb.edge.core.AbstractEdgeDispatcher to implement your own dispatcher function.</p>
<p>The implementation class is registered to the system through the Java standard SPI mechanism.</p>
<p>Dispatcher needs to implement 2 methods:</p>
<ul>
<li>
<h3 id="getorder">getOrder</h3>
</li>
</ul>
<p>Dispatcher needs to inject routing rules into vertx, and routing rules have a priority order relationship.</p>
<p>All Dispatchers in the system are sorted according to the return value of getOrder from small to large and initialized in order.</p>
<p>If the GetOrder return values the two Dispatchers are the same, the order of the two is unpredictable.</p>
<ul>
<li>
<h3 id="init">init</h3>
</li>
</ul>
<p>The init method is included in the io.vertx.ext.web.The router in the vertx framework. You need to customize the routing rules through this object.</p>
<p>You can specify the url that meets the requirements, whether you need to process the cookie, whether you need to handle the body, which custom method to use to process the received request, etc.</p>
<p>For more details on routing rules, please refer to the official vertx documentation: [vertx routing mechanism] (http://vertx.io/docs/vertx-web/java/#_routing_by_exact_path)</p>
<p><em>prompt:</em></p>
<p>_ Multiple Dispatchers can set routing rules to cover the same url. _</p>
<p>_Assuming Dispatcher A and B can both handle the same url, and A has a higher priority, then: _</p>
<ul>
<li>
<p>_ If A is processed, neither responding nor calling RoutingContext.next(), it is a bug, this request is hanged _</p>
</li>
<li>
<p>_ If A is processed and then calling RoutingContext.next(), the request will be transferred to B.</p>
</li>
</ul>
<h3 id="2-forwarding-request">2. Forwarding request</h3>
<p>When registering a route, it specifies which method is used to process the request (the following method is used to refer to the method), and the forwarding logic is implemented in the onRequest.</p>
<p>The method prototype is:</p>
<pre><code>void onRequest(RoutingContext context)
</code></pre>
<p>The system encapsulates org.apache.servicecomb.edge.core.EdgeInvocation to implement forwarding. At least the following parameters need to be prepared:</p>
<ul>
<li>
<p>microserviceName, the business makes its own rules, can be passed in the url, or according to the url search, etc.</p>
</li>
<li>
<p>context, that is, the input of onRequest</p>
</li>
<li>
<p>path, the url of the forwarding target</p>
</li>
<li>
<p>httpServerFilters, the Dispatcher parent class has initialized member variables</p>
</li>
</ul>
<pre><code> EdgeInvocation edgeInvocation = new EdgeInvocation();
edgeInvocation.init(microserviceName, context, path, httpServerFilters);
edgeInvocation.edgeInvoke();
</code></pre>
<p>The edgeInvoke call is internally called and will be forwarded as a ServiceComb standard consumer.</p>
<p>As a standard consumer, it means that the governance capabilities of all ServiceComb standards are valid here.</p>
<h3 id="3-setting-compatibility-rules">3. Setting compatibility rules</h3>
<p>Different services may have different compatibility plans, servicecomb default compatibility rules, and all new versions are required to be compatible with the old version. If this requirement is met, no special settings need to be made.</p>
<p>There is also a typical plan:</p>
<ul>
<li>
<p>1.0.0-2.0.0 is internally compatible, url is in the form of /microserviceName/v1/....</p>
</li>
<li>
<p>2.0.0-3.0.0 is internally compatible, url is in the form of /microserviceName/v2/....</p>
</li>
</ul>
<p>......</p>
<p>Incompatible between major versions</p>
<p>At this point, the developer needs to set compatibility rules for EdgeInvocation:</p>
<pre><code>private CompatiblePathVersionMapper versionMapper = new CompatiblePathVersionMapper();
……
edgeInvocation.setVersionRule(versionMapper.getOrCreate(pathVersion).getVersionRule());
</code></pre>
<p>The role of versionMapper is to convert a string such as v1 or v2 to a compatibility rule such as 1.0.0-2.0.0 or 2.0.0-3.0.0.</p>
<p><strong>note:</strong></p>
<p>Incompatible interfaces can cause many problems. The java chassis requires that the higher version of the service is compatible with the lower version of the service, and only allows the addition of the interface to not allow the interface to be deleted. After adding an interface, you must increase the version number of the microservice. In the development phase, interfaces change frequently, and developers often forget this rule. When this constraint is broken, you need to clean up the service center microservices information and restart the microservices and Edge Service\ (and other services that depend on the microservices). Otherwise, the request forwarding failure may occur.</p>
<h3 id="4authentication">4.Authentication</h3>
<p>The Edge Service is the boundary of the system and requires authentication logic for many requests.</p>
<p>Based on the standard ServiceComb mechanism, this function can be implemented by the handler.</p>
<p>The simplest code is as follows:</p>
<pre><code>public class AuthHandler implements Handler {
private Auth auth;
public AuthHandler() {
auth = Invoker.createProxy("auth", "auth", Auth.class);
}
……
@Override
public void handle(Invocation invocation, AsyncResponse asyncResp) throws Exception {
if (!auth.auth("")) {
asyncResp.consumerFail(new InvocationException(Status.UNAUTHORIZED, (Object) "auth failed"));
return;
}
LOGGER.debug("auth success.");
invocation.next(asyncResp);
}
}
</code></pre>
<p>Auth is the interface provided by the authentication microservice. Invoker.createProxy("auth", "auth", Auth.class) is the underlying api of the consumer in the transparent RPC development mode, which is equivalent to @ReferenceRpc, but not Need to rely on the spring bean mechanism.</p>
<p>The business completely defines the Auth interface, but here is just an example.</p>
<p>After the Handler development is complete, configure it into the microservice.yaml of the edge service:</p>
<pre><code>servicecomb:
handler:
chain:
Consumer:
default: auth,……
service:
auth: ……
</code></pre>
<p>In this example, it means that the forwarding request to all microservices must be authenticated, but authentication is not required when calling the authentication microservice.</p>
</div>
</div><footer>
<div class="rst-footer-buttons" role="navigation" aria-label="Footer Navigation">
<a href="../open-service/" class="btn btn-neutral float-left" title="Intruductions"><span class="icon icon-circle-arrow-left"></span> Previous</a>
<a href="../nginx/" class="btn btn-neutral float-right" title="Using confd and Nginx as edge services">Next <span class="icon icon-circle-arrow-right"></span></a>
</div>
<hr/>
<div role="contentinfo">
<!-- Copyright etc -->
</div>
Built with <a href="https://www.mkdocs.org/">MkDocs</a> using a <a href="https://github.com/readthedocs/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>.
</footer>
</div>
</div>
</section>
</div>
<div class="rst-versions" role="note" aria-label="Versions">
<span class="rst-current-version" data-toggle="rst-current-version">
<span><a href="../open-service/" style="color: #fcfcfc">« Previous</a></span>
<span><a href="../nginx/" style="color: #fcfcfc">Next »</a></span>
</span>
</div>
<script>var base_url = '../..';</script>
<script src="../../js/theme_extra.js" defer></script>
<script src="../../js/theme.js" defer></script>
<script src="../../search/main.js" defer></script>
<script defer>
window.onload = function () {
SphinxRtdTheme.Navigation.enable(true);
};
</script>
</body>
</html>