|
|
|
@ -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]
|
|
|
|
|
----
|
|
|
|
|
<dependency>
|
|
|
|
|
<groupId>com.alibaba.cloud</groupId>
|
|
|
|
|
<artifactId>spring-cloud-starter-alibaba-sidecar</artifactId>
|
|
|
|
|
</dependency>
|
|
|
|
|
----
|
|
|
|
|
* 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.
|