feat: optimize sidecar-example and readme docs

pull/3625/head
yuluo 12 months ago
parent 32f76fddb2
commit be4ae70b4d

@ -2,43 +2,41 @@
## 项目说明
本项目演示如何使用 Nacos + Spring Cloud Alibaba Sidecar 完成 异构语言微服务的接入。
[Spring Cloud Alibaba Sidecar](https://spring-cloud-alibaba-group.github.io/github-pages/hoxton/zh-cn/index.html#_spring_cloud_alibaba_sidecar) 是一个用来快速完美整合 Spring Cloud 与 异构语言微服务 的框架
本项目演示如何使用 Nacos + Spring Cloud Alibaba Sidecar 完成异构微服务的接入。
[Spring Cloud Alibaba Sidecar](https://sca.aliyun.com/zh-cn/docs/2022.0.0.0/user-guide/sidecar/overview) 是一个用来快速完美整合 Spring Cloud 应用与异构微服务的框架。
## 准备工作
### 下载并启动 Nacos
**在接入 Sidecar 之前,首先需要启动 Nacos服务器。**
**在接入 Sidecar 之前,首先需要启动 Nacos Server。**
1. 下载[Nacos二进制文件](https://github.com/alibaba/nacos/releases/download/2.1.0/nacos-server-2.1.0.zip) 并解压
1. 下载 [Nacos Server](https://github.com/alibaba/nacos/releases/) 并解压
2. 启动 Nacos Server
下载解压后 我们需要进入到 bin 目录启动 nacos 服务, 一定不要双击启动,双击默认会以集群方式启动,我们以单机方式启动。
```bash
startup.cmd -m standalone
```
下载解压,需要进入到 bin 目录启动 nacos server 一定不要双击启动,双击默认会以集群方式启动。这里以单机方式启动:
```bash
startup.cmd -m standalone
```
3. 登录 Nacos
我们来到浏览器 输入localhost:8848/nacos 可以看到Nacos的运行的界面
用户名和密码都是 `nacos`
浏览器输入 localhost:8848/nacos 可以看到 Nacos 的 控制台界面,输入用户名和密码以登录 Nacos用户名和密码都是 `nacos`
## 简单示例
本文以Nacos作为注册中心为例Sidecar接入一个非Java语言的服务。
本文以 Nacos 作为注册中心为例Sidecar 接入一个非 Java 语言的服务。
> 默认以 golang 语言服务为示例,可以在 `application.yml` 中启动 `node` 异构服务。
### Step1: 引入依赖
修改 `pom.xml` 文件,引入 Spring Cloud Alibaba Sidecar Starter。
```xml
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
@ -53,78 +51,82 @@ startup.cmd -m standalone
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
```
### Step2: 配置 Sidecar 相关信息
然后在项目的application.yml 文件中指定以下配置
然后在项目的 `application.yml` 配置文件中指定以下配置:
```yaml
server:
port: 8070
spring:
profiles:
active: node
cloud:
nacos:
username: nacos
password: nacos
discovery:
username: 'nacos'
password: 'nacos'
server-addr: 127.0.0.1:8848
group: test
gateway:
discovery:
locator:
enabled: true
application:
name: node-service
# sidecar 相关的配置
name: sidecar-service
sidecar:
# 异构微服务的IP
# heterogeneous microservices IP
ip: 127.0.0.1
# 异构微服务的端口
port: 8060
# heterogeneous microservices Port
port: 8050
# heterogeneous microservices health url
# health-check-url: http://localhost:8050/api/v1/health-check
# 异构微服务的健康检查URL(这里不配置的话默认会认为是UP)
health-check-url: http://localhost:8060/health.json
# springboot actuator监控相关
management:
endpoint:
health:
show-details: always
```
注意:这里的 localhost:8060是我本机起了一个nginx 代理了这个health.json的请求。在实际使用过程中可以是任意的REST服务只需要返回正确的JSON格式的健康检测数据即可。
```json
注意:这里的 localhost:8050是异构服务地址。在实际使用过程中可以是任意的 REST 服务,只需要返回正确的 JSON 格式的健康检测数据即可。
```json
{
"status": "DOWN"
"status": "UP"
}
```
### Step3: 启动应用
之后分别启动 Sidecar 服务、本地异构服务。
IDE 直接启动:找到主类 `com.alibaba.cloud.sidecar.DemoApplication`,执行 main 方法启动应用。
注意:本文是以 `spring-cloud-alibaba-sidecar-nacos-example`项目为例,所以启动的是它下面的`DemoApplication`启动类。
注意:本文是以 `spring-cloud-alibaba-sidecar-nacos-example` 项目为例,所以启动的是它下面的 `DemoApplication` 启动类。
![idea.png](https://cdn.nlark.com/yuque/0/2022/png/1752280/1662550869316-98d574af-d1ba-4c00-a0af-5e33e13075fd.png)
### Step4: 查看服务注册情况
![nacos.png](https://cdn.nlark.com/yuque/0/2022/png/1752280/1662548324337-566cc824-4d08-4041-ac83-1968c7347a9e.png)
### Step4: 访问异构服务
完成上面4步我们发现对应的服务 `sidecar-service` 已经成功注册到了注册中心。此时,这个服务已经成功的融入到了 Spring Cloud 微服务的怀抱。对于 Spring Cloud 微服务而言,访问它跟访问其它的 Java 微服务没有任何区别。
而这,也正是 Spring Cloud Alibaba Sidecar 的魅力所在。接下来,将继续演示怎样访问这个服务。
浏览器访问:`http://127.0.0.1:8070/sidecar-service/test`
### Step4: 访问异构服务
完成上面4步我们发现对应的服务`node-service`已经成功注册到了注册中心。此时这个服务已经成功的融入到了Spring Cloud 微服务的怀抱。对于Spring Cloud 微服务而言访问它跟访问其它的Java微服务没有任何的区别。
而这,也正是 Spring Cloud Alibaba Sidecar的魅力所在。接下来我们将继续演示怎样访问这个服务。
浏览器访问
http://127.0.0.1:8070/node-service/health.json
能调通则说明整合成功。
![](https://cdn.nlark.com/yuque/0/2022/png/1752280/1662549893322-1b7a761a-ecd7-44ae-88b6-872eca43a866.png)
![img](https://cdn.nlark.com/yuque/0/2022/png/1752280/1662549893322-1b7a761a-ecd7-44ae-88b6-872eca43a866.png)
## More
如果您对 spring cloud starter alibaba sidecar 有任何建议或想法,欢迎在 issue 中或者通过其他社区渠道向我们提出。
如果您对 spring cloud starter alibaba sidecar 有任何建议或想法,欢迎在 Issue 中或者通过其他社区渠道向我们提出。

@ -1,39 +1,40 @@
# Spring Cloud Alibaba Sidecar Example
## Spring Cloud Alibaba Sidecar Example
## Project Instruction
## Project Description
This project demonstrates how to use `Nacos + Spring Cloud Alibaba Sidecar` to access heterogeneous language microservices.
This project demonstrates how to use Nacos + Spring Cloud Alibaba Sidecar to accomplish heterogeneous microservice access.
[Spring Cloud Alibaba Sidecar](https://spring-cloud-alibaba-group.github.io/github-pages/hoxton/zh-cn/index.html#_spring_cloud_alibaba_sidecar) is a framework for fast and seamless integration of Spring Cloud with heterogeneous language microservices.
[Spring Cloud Alibaba Sidecar](https://sca.aliyun.com/zh-cn/docs/2022.0.0.0/user-guide/sidecar/overview) is a framework for quickly and seamlessly integrating Spring Cloud applications with heterogeneous microservices. services.
## Preparation
### Download and start Nacos
## Preparation
**Before you can access Sidecar, you need to start Nacos Server.**
### Download and Startup Nacos
1. Download [Nacos Server](https://github.com/alibaba/nacos/releases/download/2.1.0/nacos-server-2.1.0.zip) and unzip it. 2.
**You should startup Nacos Server before using Sidecar**
2. Start Nacos Server
1. Download [Nacos](https://archive.apache.org/dist/rocketmq/4.3.2/rocketmq-all-4.3.2-bin-release.zip) and unzip it.
After downloading and unpacking, you need to go to the bin directory to start the nacos server, make sure you don't double-click to start it, double-clicking will start it as a cluster by default. Here, we will start it as a standalone server:
2. Startup Name Server
```bash
startup.cmd -m standalone
```
```bash
startup.cmd -m standalone
```
3. Log in to Nacos
Type localhost:8848/nacos in your browser to see the Nacos console, and enter your username and password to log in to Nacos (username and password both are `nacos`);
3. Sign in Nacos
Open you browser then input `localhost:8848/nacos` ,you can see the Nacos dashboard ui .
The default username and password are `nacos`
## Simple Example
In this article, we use Nacos as a registry as an example, and Sidecar to access a non-Java language service.
## Simple example
In this paper, Sidecar accesses a non-Java language service using Nacos as a registry as an example.
> By default, we use the golang language service as an example, and we can start the `node` heterogeneous service in `application.yml`.
### Step1: Introducing dependencies
### Step1: Declare dependency
Add dependency spring-cloud-starter-alibaba-sidecar to the `pom.xml` file in your Sidecar project.
Modify the `pom.xml` file to introduce Spring Cloud Alibaba Sidecar Starter.
```xml
<dependency>
@ -52,68 +53,80 @@ Add dependency spring-cloud-starter-alibaba-sidecar to the `pom.xml` file in you
</dependency>
```
### Step2: Configure sidecar
Then add necessary configurations to file `/src/main/resources/application.yml`.
### Step2: Configure Sidecar related information
Then specify the following configuration in the project's `application.yml` configuration file:
```yaml
server:
port: 8070
spring:
profiles:
active: node
cloud:
nacos:
username: nacos
password: nacos
discovery:
username: 'nacos'
password: 'nacos'
server-addr: 127.0.0.1:8848
group: test
gateway:
discovery:
locator:
enabled: true
application:
name: node-service
sidecar:
# heterogeneous services ip
ip: 127.0.0.1
# heterogeneous service's port
port: 8060
name: sidecar-service
# heterogeneous service's health check URL
health-check-url: http://localhost:8060/health.json
sidecar:
# heterogeneous microservices IP
ip: 127.0.0.1
# heterogeneous microservices Port
port: 8050
# heterogeneous microservices health url
# health-check-url: http://localhost:8050/api/v1/health-check
management:
endpoint:
health:
show-details: always
```
Note: `localhost:8060` here, is my local machine started a nginx proxy for this `health.json` request. In actual use it can be any REST service, just need to return the correct JSON format health detection data.
```json
Note: localhost:8050 is the address of the heterogeneous service. In practice it can be any REST service, it just needs to return the correct JSON formatted health test data.
```json
{
"status": "DOWN"
"status": "UP"
}
```
### Step3: Start Application
After that, start the `Sidecar` service and the local heterogeneous service respectively.
### Step3: Starting the application
After that, start the Sidecar service and local heterogeneous service respectively.
Start in IDE: Find main class `com.alibaba.cloud.sidecar.DemoApplication`, and execute the main method.
IDE direct start: find the main class `com.alibaba.cloud.sidecar.DemoApplication` and execute the main method to start the application.
Note: In this article, we take the `spring-cloud-alibaba-sidecar-nacos-example` project as an example, so we start the `DemoApplication` startup class under it.
Note: This article takes the `spring-cloud-alibaba-sidecar-nacos-example` project as an example, so it starts the `DemoApplication` startup class under it.
![idea.png](https://cdn.nlark.com/yuque/0/2022/png/1752280/1662550869316-98d574af-d1ba-4c00-a0af-5e33e13075fd.png)
### Step4: View service registration
![nacos.png](https://cdn.nlark.com/yuque/0/2022/png/1752280/1662605412601-06780784-915c-40f6-b6b2-67176f6c5419.png)
### Step4: Check the service registrations.
![nacos.png](https://cdn.nlark.com/yuque/0/2022/png/1752280/1662548324337-566cc824-4d08-4041-ac83-1968c7347a9e.png)
### Step4: Accessing Heterogeneous Services
After completing the above 4 steps, we find that the corresponding service `sidecar-service` has been successfully registered to the registry. At this point, this service has been successfully integrated into Spring Cloud microservices. For Spring Cloud microservices, accessing it is no different from accessing other Java microservices.
This is the beauty of Spring Cloud Alibaba Sidecar. Next, we will continue to demonstrate how to access this service.
### Step4: Accessing services
After completing the above 4 steps, we find that the corresponding service `node-service` has been successfully registered to the registry. At this point, the service has been successfully integrated into the Spring Cloud microservice. For Spring Cloud microservices, accessing it is no different than accessing any other Java microservice.
This is where the beauty of Spring Cloud Alibaba Sidecar comes in. Next, we will continue to demonstrate how to access this service.
Browser Access: `http://127.0.0.1:8070/sidecar-service/test`
Browser Access below address:
http://127.0.0.1:8070/node-service/health.json
If you see the following message, the access was successful.
The integration is successful if it works.
![](https://cdn.nlark.com/yuque/0/2022/png/1752280/1662549893322-1b7a761a-ecd7-44ae-88b6-872eca43a866.png)
![img](https://cdn.nlark.com/yuque/0/2022/png/1752280/1662549893322-1b7a761a-ecd7-44ae-88b6-872eca43a866.png)
## More
If you have any ideas or suggestions for `Spring Cloud Alibaba Sidecar`, please don't hesitate to tell us by submitting github issues.
If you have any suggestions or ideas about spring cloud starter alibaba sidecar, please feel free to submit them to us in an Issue or through other community channels.

@ -0,0 +1,20 @@
package main
import (
"log"
"golang-sidecar/pkg/constant"
route "golang-sidecar/pkg/route"
"github.com/gin-gonic/gin"
)
func main() {
if err := route.SettingRouter(gin.Default()).Run(constant.ServicePort); err != nil {
log.Fatal("Golang sidecar service start failed.")
}
log.Println("Golang sidecar service listener in localhost:8060 ...")
}

@ -0,0 +1,32 @@
module golang-sidecar
go 1.21
require github.com/gin-gonic/gin v1.9.1
require (
github.com/bytedance/sonic v1.9.1 // indirect
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect
github.com/gabriel-vasile/mimetype v1.4.2 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.14.0 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/cpuid/v2 v2.2.4 // indirect
github.com/leodido/go-urn v1.2.4 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.0.8 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.11 // indirect
golang.org/x/arch v0.3.0 // indirect
golang.org/x/crypto v0.9.0 // indirect
golang.org/x/net v0.10.0 // indirect
golang.org/x/sys v0.8.0 // indirect
golang.org/x/text v0.9.0 // indirect
google.golang.org/protobuf v1.30.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

@ -0,0 +1,86 @@
github.com/bytedance/sonic v1.5.0/go.mod h1:ED5hyg4y6t3/9Ku1R6dU/4KyJ48DZ4jPhfY1O2AihPM=
github.com/bytedance/sonic v1.9.1 h1:6iJ6NqdoxCDr6mbY8h18oSO+cShGSMRGCEo7F2h0x8s=
github.com/bytedance/sonic v1.9.1/go.mod h1:i736AoUSYt75HyZLoJW9ERYxcy6eaN6h4BZXU064P/U=
github.com/chenzhuoyu/base64x v0.0.0-20211019084208-fb5309c8db06/go.mod h1:DH46F32mSOjUmXrMHnKwZdA8wcEefY7UVqBKYGjpdQY=
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 h1:qSGYFH7+jGhDF8vLC+iwCD4WpbV1EBDSzWkJODFLams=
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311/go.mod h1:b583jCggY9gE99b6G5LEC39OIiVsWj+R97kbl5odCEk=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/gabriel-vasile/mimetype v1.4.2 h1:w5qFW6JKBz9Y393Y4q372O9A7cUSequkh1Q7OhCmWKU=
github.com/gabriel-vasile/mimetype v1.4.2/go.mod h1:zApsH/mKG4w07erKIaJPFiX0Tsq9BFQgN3qGY5GnNgA=
github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=
github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI=
github.com/gin-gonic/gin v1.9.1 h1:4idEAncQnU5cB7BeOkPtxjfCSye0AAm1R0RVIqJ+Jmg=
github.com/gin-gonic/gin v1.9.1/go.mod h1:hPrL7YrpYKXt5YId3A/Tnip5kqbEAP+KLuI3SUcPTeU=
github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s=
github.com/go-playground/assert/v2 v2.2.0/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA=
github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY=
github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY=
github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY=
github.com/go-playground/validator/v10 v10.14.0 h1:vgvQWe3XCz3gIeFDm/HnTIbj6UGmg/+t63MyGU2n5js=
github.com/go-playground/validator/v10 v10.14.0/go.mod h1:9iXMNT7sEkjXb0I+enO7QXmzG6QCsPWY4zveKFVRSyU=
github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU=
github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
github.com/klauspost/cpuid/v2 v2.2.4 h1:acbojRNwl3o09bUq+yDCtZFc1aiwaAAxtcn8YkZXnvk=
github.com/klauspost/cpuid/v2 v2.2.4/go.mod h1:RVVoqg1df56z8g3pUjL/3lE5UfnlrJX8tyFgg4nqhuY=
github.com/leodido/go-urn v1.2.4 h1:XlAE/cm/ms7TE/VMVoduSpNBoyc2dOxHs5MZSwAN63Q=
github.com/leodido/go-urn v1.2.4/go.mod h1:7ZrI8mTSeBSHl/UaRyKQW1qZeMgak41ANeCNaVckg+4=
github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA=
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
github.com/pelletier/go-toml/v2 v2.0.8 h1:0ctb6s9mE31h0/lhu+J6OPmVeDxJn+kYnJc2jZR9tGQ=
github.com/pelletier/go-toml/v2 v2.0.8/go.mod h1:vuYfssBdrU2XDZ9bYydBu6t+6a6PYNcZljzZR9VXg+4=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/stretchr/testify v1.8.3 h1:RP3t2pwF7cMEbC1dqtB6poj3niw/9gnV4Cjg5oW5gtY=
github.com/stretchr/testify v1.8.3/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI=
github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08=
github.com/ugorji/go/codec v1.2.11 h1:BMaWp1Bb6fHwEtbplGBGJ498wD+LKlNSl25MjdZY4dU=
github.com/ugorji/go/codec v1.2.11/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg=
golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8=
golang.org/x/arch v0.3.0 h1:02VY4/ZcO/gBOH6PUaoiptASxtXU10jazRCP865E97k=
golang.org/x/arch v0.3.0/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8=
golang.org/x/crypto v0.9.0 h1:LF6fAI+IutBocDJ2OT0Q1g8plpYljMZ4+lty+dsqw3g=
golang.org/x/crypto v0.9.0/go.mod h1:yrmDGqONDYtNj3tH8X9dzUun2m2lzPa9ngI6/RUPGR0=
golang.org/x/net v0.10.0 h1:X2//UzNDwYmtCLn7To6G58Wr6f5ahEAQgKNzv9Y951M=
golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg=
golang.org/x/sys v0.0.0-20220704084225-05e143d24a9e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.8.0 h1:EBmGv8NaZBZTWvrbjNoL6HVt+IVy3QDQpJs7VRIw3tU=
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/text v0.9.0 h1:2sjJmO8cDvYveuX97RDLsxlyUxLl+GHoLxBiRdHllBE=
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.30.0 h1:kPPoIgf3TsEvrm0PFe15JQ+570QVxYzEvvHqChK+cng=
google.golang.org/protobuf v1.30.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4=

@ -0,0 +1,9 @@
package constant
const (
ServicePort = ":8050"
Success = "{'status':'UP'}"
Test = "{'index':'Welcome to golang sidecar service.'}"
)

@ -0,0 +1,24 @@
package router
import (
"golang-sidecar/pkg/constant"
"net/http"
"github.com/gin-gonic/gin"
)
func SettingRouter(r *gin.Engine) *gin.Engine {
v1 := r.Group("/api/v1")
{
// Health check request
v1.GET("health-check", func(ctx *gin.Context) {
ctx.JSON(http.StatusOK, constant.Success)
})
v1.GET("test", func(ctx *gin.Context) {
ctx.JSON(http.StatusOK, constant.Test)
})
}
return r
}

@ -16,27 +16,30 @@
var http = require('http');
var url = require("url");
var path = require('path');
// 创建server
// Created server
var server = http.createServer(function(req, res) {
// 获得请求的路径
// Get request path
var pathname = url.parse(req.url).pathname;
res.writeHead(200, { 'Content-Type' : 'application/json; charset=utf-8' });
// 访问http://localhost:8060/,将会返回{"index":"欢迎来到首页"}
// Request http://localhost:8060/,
// Return {"index":"Welcome to index pages."}
if (pathname === '/') {
res.end(JSON.stringify({ "index" : "欢迎来到首页" }));
res.end(JSON.stringify({ "index" : "Welcome to index pages." }));
}
// 访问http://localhost:8060/health将会返回{"status":"UP"}
// Request http://localhost:8060/health,
// Return {"status":"UP"}
else if (pathname === '/health.json') {
res.end(JSON.stringify({ "status" : "UP" }));
}
// 其他情况返回404
// Other situations return '404'
else {
res.end("404");
}
});
// 创建监听,并打印日志
// Created listener, print logs.
server.listen(8060, function() {
console.log('listening on localhost:8060');
});
console.log('node sidecar service listening on localhost:8060 ...');
});

@ -40,11 +40,6 @@
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>

@ -23,7 +23,7 @@ import org.springframework.cloud.loadbalancer.annotation.LoadBalancerClients;
@SpringBootApplication
@LoadBalancerClients({
@LoadBalancerClient("node-service")
@LoadBalancerClient("sidecar-service")
})
public class DemoApplication {

@ -0,0 +1,24 @@
#
# Copyright 2013-2023 the original author or authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
sidecar:
# heterogeneous microservices IP
ip: 127.0.0.1
# heterogeneous microservices Port
port: 8050
# heterogeneous microservices health url
# health-check-url: http://localhost:8050/api/v1/health-check

@ -0,0 +1,24 @@
#
# Copyright 2013-2023 the original author or authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
sidecar:
# heterogeneous microservices IP
ip: 127.0.0.1
# heterogeneous microservices Port
port: 8060
# heterogeneous microservices health url
# health-check-url: http://localhost:8060/health.json

@ -16,35 +16,26 @@
server:
port: 8070
spring:
profiles:
active: node
cloud:
nacos:
username: 'nacos'
password: 'nacos'
discovery:
username: 'nacos'
password: 'nacos'
server-addr: 127.0.0.1:8848
group: test
gateway:
discovery:
locator:
enabled: true
loadbalancer:
nacos:
enabled: true
ribbon:
enabled: false
application:
name: node-service
sidecar:
# 异构微服务的IP
ip: 127.0.0.1
# 异构微服务的端口
port: 8060
name: sidecar-service
# 异构微服务的健康检查URL
#health-check-url: http://localhost:8060/health.json
management:
endpoint:
health:
show-details: always
show-details: always

Loading…
Cancel
Save