diff --git a/spring-cloud-alibaba-docs/src/main/asciidoc-zh/sidecar.adoc b/spring-cloud-alibaba-docs/src/main/asciidoc-zh/sidecar.adoc index 07f448cff..32f23d39a 100644 --- a/spring-cloud-alibaba-docs/src/main/asciidoc-zh/sidecar.adoc +++ b/spring-cloud-alibaba-docs/src/main/asciidoc-zh/sidecar.adoc @@ -40,7 +40,7 @@ Gateway的新一代Sidecar,然而官方表示并没有该计划。详见:htt * 目前Mesh主要使用场景在Kubernetes领域(Istio、Linkerd 2等,大多将Kubernetes作为First Class支持,虽然Istio也可部署在非Kubernetes环境),而目前业界,Spring -Cloud应用未必有试试Mesh的环境; +Cloud应用未必有 Service Mesh 的环境; * 使用Alibaba Sidecar一个小组件就能解决问题了(核心代码不超过200行),引入整套Mesh方案,颇有点屠龙刀杀黄鳝的意思。 @@ -66,7 +66,7 @@ Cloud本身就是基于HTTP的; === 使用示例 * 如使用Nacos作为服务发现组件,详见`spring-cloud-alibaba-examples/spring-cloud-alibaba-sidecar-examples/spring-cloud-alibaba-sidecar-nacos-example` -* 如使用Consul作为服务发现组件,详见`spring-cloud-alibaba-examples/spring-cloud-alibaba-sidecar-examples/spring-cloud-alibaba-sidecar-nacos-example` +* 如使用Consul作为服务发现组件,详见`spring-cloud-alibaba-examples/spring-cloud-alibaba-sidecar-examples/spring-cloud-alibaba-sidecar-consul-example` ==== 示例代码(以Nacos服务发现为例) @@ -178,7 +178,7 @@ Gateway还可以整合Sentinel或者Hystirx、Resilience4J,所以也带有了 === Alibaba Sidecar优缺点分析 Alibaba -Sidecar的设计和Sidecar基本一致,优缺点和Sidecar的优缺点也是一样的。 +Sidecar的设计和 Netfix Sidecar 基本一致,优缺点和 Netfix Sidecar 的优缺点也是一样的。 优点: diff --git a/spring-cloud-alibaba-docs/src/main/asciidoc/sidecar.adoc b/spring-cloud-alibaba-docs/src/main/asciidoc/sidecar.adoc new file mode 100644 index 000000000..707ec4df0 --- /dev/null +++ b/spring-cloud-alibaba-docs/src/main/asciidoc/sidecar.adoc @@ -0,0 +1,193 @@ +== Spring Cloud Alibaba Sidecar + +`Spring Cloud Alibaba Sidecar` is a Spring Cloud for quick and perfect integration +with * heterogeneous microservices * framework, inspired by +https://github.com/spring-cloud/spring-cloud-netflix/tree/master/spring-cloud-netflix-sidecar[Spring +Cloud Netflix Sidecar],currently supported service discovery components: + +* Nacos +* Consul + +=== Terminology + +==== Heterogeneous microservice + +Non-spring Cloud applications are referred to as heterogeneous micro-services. Such as your legacy projects, or non-JVM applications. + +==== Three meanings of "perfect integration" + +* Take advantage of service discovery +* There is load balancing +* with circuit breaker + +=== Why or Why not? + +==== Why write Alibaba Sidecar? + +There are two reasons: + +* Spring Cloud subproject 'Spring Cloud Netflix Sidecar' +It is possible to integrate heterogeneous microservices quickly. However, Sidecar only supports the use of Eureka as a service discovery, * if you use other service discovery components you are blinded *. +* *Sidecar is based on Zuul 1.x *, Spring +Cloud has officially stated that Zuul will be phased out in the future. Earlier this year, I gave it to Spring +The Cloud official requested an official implementation based on the Spring Cloud +Gateway's new Sidecar, though officials say there are no plans for it. See: https://github.com/spring-cloud/spring-cloud-gateway/issues/735 + +Since I didn't, I wrote it myself. + +==== Why not use a Service Mesh? + +* Currently, Mesh is mainly used in Kubernetes (Istio, Linkerd +2, etc., most of the Kubernetes as the First +Class support, although Istio can also be deployed in non-Kubernetes environments), unlike the current industry, Spring +Cloud applications may not have Service Mesh environments; +* Use Alibaba +Sidecar solves the problem in a single widget (no more than 200 lines of core code) and introduces a complete Mesh scheme, which is a bit like killing eel. + +=== principle + +* Alibaba +Sidecar registers the IP/ port of heterogeneous micro-services to the service discovery component based on the configured IP and port of heterogeneous micro-services* +. +* Alibaba Sidecar implements * Health Check *, Alibaba +Sidecar periodically checks whether heterogeneous microservices are healthy. If heterogeneous microservices are found to be unhealthy, Alibaba +Sidecar will automatically represent the heterogeneous micro services of Alibaba +Sidecar instance is offline. If the heterogeneous microservice recovers, it automatically goes online. The maximum delay is 30 seconds, see details +`Alibaba SidecarChecker#check.` + +=== requirement + +* [Must] Your heterogeneous microservice needs to communicate using HTTP. This is not strictly a requirement because of Spring +The Cloud itself is based on HTTP; +* 【 Optional 】 If the microservice is configured with `sidecar.health-check-url` +In this case, your heterogeneous micro service needs to implement health check (can be empty implementation, as long as an endpoint is exposed, return similar +`{"status": "UP"}` is a string). + +=== Use an example + +* If Nacos is used as a service discovery component, See `spring-cloud-alibaba-examples/spring-cloud-alibaba-sidecar-examples/spring-cloud-alibaba-sidecar-nacos-example` for details +* If you use Consul as a service discovery component, See `spring-cloud-alibaba-examples/spring-cloud-alibaba-sidecar-examples/spring-cloud-alibaba-sidecar-consul-example` for details + +==== Sample code (using Nacos service discovery as an example) + +* Plus dependence: ++ +[source,xml] +---- + +com.alibaba.cloud +spring-cloud-starter-alibaba-sidecar + +---- +* Write configuration: ++ +[source,yaml] +---- +server: + port: 8070 +spring: + cloud: + nacos: + discovery: + server-addr: 127.0.0.1:8848 + gateway: + discovery: + locator: + enabled: true + application: + name: node-service +sidecar: + # IP address of the heterogeneous microservice + ip: 127.0.0.1 + # Port of the heterogeneous microservice + port: 8060 + # Health check URL for heterogeneous microservices + health-check-url: http://localhost:8060/health.json +management: +endpoint: + health: + show-details: always +---- ++ +The configuration was simple, registering Alibaba Sidecar with Nacos and adding a few lines of Alibaba +Sidecar configuration. + +==== Heterogeneous microservice + +I have prepared a simple microservice written in NodeJS. + +[source,javascript] +---- +var http = require('http'); +var url = require("url"); +var path = require('path'); + +// create server +var server = http.createServer(function(req, res) { + // Get the path to the request + var pathname = url.parse(req.url).pathname; + res.writeHead(200, { 'Content-Type' : 'application/json; charset=utf-8' }); + // Visit http://localhost:8060/ and {"index":" Welcome to Home page"} + if (pathname === '/') { + res.end(JSON.stringify({ "index" : "Welcome to Home page" })); + } + // Visit http://localhost:8060/health, will return to {" status ":" UP "} + else if (pathname === '/health.json') { + res.end(JSON.stringify({ "status" : "UP" })); + } + // In other cases, 404 is returned + else { + res.end("404"); + } +}); +// Create a listener and print a log +server.listen(8060, function() { + console.log('listening on localhost:8060'); +}); +---- + +==== Test +===== Test 1: Spring Cloud microservices perfectly invoke heterogeneous microservices + +Integrate the Ribbon for your Spring Cloud microservice and build `http://node-service/**` +, you can request the heterogeneous micro service `/**`. + +Example: + +Ribbon requests `http://node-service/` will request `http://localhost:8060/` +And so on. + +As for the circuit breaker, normal for your Spring +Just integrate Sentinel, Hystirx and Resilience4J with Cloud microservices. + +===== Test 2: Heterogeneous microservices perfectly invoke Spring Cloud microservices + +Alibaba Sidecar is based on the Spring Cloud Gateway, and the gateway has its own forwarding capability. + +Example: + +If you have a Spring Cloud microservice called `spring-cloud-microservice` +Then the NodeJS application only needs to build +`http://localhost:8070/spring-cloud-microservice/**`, Alibaba +Sidecar then forwards the request to `spring-cloud-microservice`’s `/**` 。 + +The Spring Cloud Gateway integrates the Ribbon to realize load balancing. Spring Cloud +Gateway also has a circuit breaker for integrating Sentinel or Hystirx, Resilience4J. + +Analysis of advantages and disadvantages of Alibaba Sidecar + +Alibaba +The design of Sidecar is basically the same as that of Netfix Sidecar, and the advantages and disadvantages are the same as those of Netfix Sidecar. + +Advantages: + +* Easy access, a few lines of code to integrate heterogeneous micro-services into the Spring Cloud ecosystem +* Does not hack the original code + +Disadvantages: + +* You need to deploy an additional Alibaba for each heterogeneous microservice instance you connect to +Sidecar instances, increasing the deployment cost (although this cost is almost negligible in a Kubernetes environment) +Sidecar instances and heterogeneous microservices can be deployed as a Pod)); +* When a heterogeneous microservice calls a Spring Cloud microservice, it essentially takes Alibaba +Sidecar When the gateway is in use, the gateway performance deteriorates after layer 1 forwarding. \ No newline at end of file diff --git a/spring-cloud-alibaba-docs/src/main/asciidoc/spring-cloud-alibaba.adoc b/spring-cloud-alibaba-docs/src/main/asciidoc/spring-cloud-alibaba.adoc index 396046de8..afdc233a6 100644 --- a/spring-cloud-alibaba-docs/src/main/asciidoc/spring-cloud-alibaba.adoc +++ b/spring-cloud-alibaba-docs/src/main/asciidoc/spring-cloud-alibaba.adoc @@ -29,4 +29,6 @@ include::schedulerx.adoc[] include::sms.adoc[] +include::sidecar.adoc[] + include::graalvm.adoc[] \ No newline at end of file