Quick Start
This section demonstrates how to use Spring Cloud Alibaba Sidecar to access heterogeneous micro-services written in a non-Java language.
Use requirements
-
[Must] Your heterogeneous microservice needs to communicate using HTTP. This is not strictly a requirement because the Spring Cloud itself is based on HTTP;
-
[Optional] If sidecar.health-check-url is configured for the micro service, the health check is enabled. In this case, your heterogeneous micro service needs to implement the health check.
Preparation work
Download and start Nacos Server
Before adding Sidecar, start the Nacos Server.
-
Download and decompress the Nacos binary file;
-
Start the Nacos Server;
After downloading and decompressing, we need to enter the bin directory to start nacos service. Do not double-click to start NACos service. By default, double-click to start NACos service will be started in cluster mode.
Terminal window $ startup.cmd -m standalone -
Log in to Nacos.
If we go to the browser and type localhost:8848/nacos, we can see that Nacos provides the console interface username and password are both nacos/nacos.
Introduce dependencies
Introduce the following dependencies into the Spring Boot project that has been created:
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-gateway</artifactId></dependency>
<dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-sidecar</artifactId></dependency>
<dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId></dependency>
Prepare heterogeneous services
Note:
This example provides a NodeJS service. Node.js needs to be installed in the local environment to start the Node service and experience the Sidecar function!
var http = require("http");var url = require("url");var path = require("path");
// create servervar 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 the 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 logserver.listen(8060, function () { console.log("listening on localhost:8060");});
Write the relevant configuration
server: port: 8070spring: cloud: nacos: discovery: server-addr: localhost:8848 gateway: discovery: locator: enabled: true application: name: node-servicesidecar: # 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.jsonmanagement:endpoint: health: show-details: always
Start service and test
View heterogeneous services on the Nacos console:
Access heterogeneous services:
Browser to access http://127.0.0.1:8070/node-service/health.json to transfer general principles of the integration of success.
Spring Cloud Alibaba Sidecar Example reference: Spring Cloud Alibaba Sidecar Example
For more advanced Sidecar features and how to use them, see the Sidecar Advanced Guide section!