Skip to content

Commit 0cefa93

Browse files
authored
Merge pull request #3625 from yuluo-yx/0215-yuluo/feat-update-code
feat: optimize sidecar-example and readme docs
2 parents c20ced1 + 72814f8 commit 0cefa93

File tree

14 files changed

+398
-125
lines changed

14 files changed

+398
-125
lines changed

.licenscheckconfig.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ header:
6868
- 'distribution/NOTICE-BIN'
6969
- 'test/src/test/resources/**'
7070
- '**/src/test/resources/statelang/**'
71+
- '**/*.mod'
72+
- '**/*.sum'
7173

7274
comment: on-failure
7375
dependency:

spring-cloud-alibaba-examples/spring-cloud-alibaba-sidecar-examples/readme-zh.md

Lines changed: 43 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -2,43 +2,41 @@
22

33
## 项目说明
44

5-
本项目演示如何使用 Nacos + Spring Cloud Alibaba Sidecar 完成 异构语言微服务的接入。
6-
7-
[Spring Cloud Alibaba Sidecar](https://spring-cloud-alibaba-group.github.io/github-pages/hoxton/zh-cn/index.html#_spring_cloud_alibaba_sidecar) 是一个用来快速完美整合 Spring Cloud 与 异构语言微服务 的框架
8-
5+
本项目演示如何使用 Nacos + Spring Cloud Alibaba Sidecar 完成异构微服务的接入。
96

7+
[Spring Cloud Alibaba Sidecar](https://sca.aliyun.com/zh-cn/docs/2022.0.0.0/user-guide/sidecar/overview) 是一个用来快速完美整合 Spring Cloud 应用与异构微服务的框架。
108

119
## 准备工作
1210

1311
### 下载并启动 Nacos
1412

15-
**在接入 Sidecar 之前,首先需要启动 Nacos服务器**
13+
**在接入 Sidecar 之前,首先需要启动 Nacos Server**
1614

17-
1. 下载[Nacos二进制文件](https://github.com/alibaba/nacos/releases/download/2.1.0/nacos-server-2.1.0.zip) 并解压
15+
1. 下载 [Nacos Server](https://github.com/alibaba/nacos/releases/) 并解压
1816

1917
2. 启动 Nacos Server
2018

21-
下载解压后 我们需要进入到 bin 目录启动 nacos 服务, 一定不要双击启动,双击默认会以集群方式启动,我们以单机方式启动。
22-
```bash
23-
startup.cmd -m standalone
24-
```
19+
下载解压,需要进入到 bin 目录启动 nacos server, 一定不要双击启动,双击默认会以集群方式启动。这里以单机方式启动:
20+
21+
```bash
22+
startup.cmd -m standalone
23+
```
2524

2625
3. 登录 Nacos
2726

28-
我们来到浏览器 输入localhost:8848/nacos 可以看到Nacos的运行的界面
29-
用户名和密码都是 `nacos`
30-
27+
浏览器输入 localhost:8848/nacos 可以看到 Nacos 的 控制台界面,输入用户名和密码以登录 Nacos(用户名和密码都是 `nacos`);
3128

3229
## 简单示例
33-
本文以Nacos作为注册中心为例,Sidecar接入一个非Java语言的服务。
3430

31+
本文以 Nacos 作为注册中心为例,Sidecar 接入一个非 Java 语言的服务。
32+
33+
> 默认以 golang 语言服务为示例,可以在 `application.yml` 中启动 `node` 异构服务。
3534
3635
### Step1: 引入依赖
3736

3837
修改 `pom.xml` 文件,引入 Spring Cloud Alibaba Sidecar Starter。
3938

4039
```xml
41-
4240
<dependency>
4341
<groupId>org.springframework.cloud</groupId>
4442
<artifactId>spring-cloud-starter-gateway</artifactId>
@@ -53,78 +51,82 @@ startup.cmd -m standalone
5351
<groupId>com.alibaba.cloud</groupId>
5452
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
5553
</dependency>
56-
5754
```
5855

5956
### Step2: 配置 Sidecar 相关信息
60-
然后在项目的application.yml 文件中指定以下配置
57+
58+
然后在项目的 `application.yml` 配置文件中指定以下配置:
59+
6160
```yaml
6261
server:
6362
port: 8070
63+
6464
spring:
65+
profiles:
66+
active: node
67+
6568
cloud:
6669
nacos:
67-
username: nacos
68-
password: nacos
6970
discovery:
71+
username: 'nacos'
72+
password: 'nacos'
7073
server-addr: 127.0.0.1:8848
71-
group: test
7274
gateway:
7375
discovery:
7476
locator:
7577
enabled: true
7678

7779
application:
78-
name: node-service
79-
# sidecar 相关的配置
80+
name: sidecar-service
81+
8082
sidecar:
81-
# 异构微服务的IP
83+
# heterogeneous microservices IP
8284
ip: 127.0.0.1
83-
# 异构微服务的端口
84-
port: 8060
85+
# heterogeneous microservices Port
86+
port: 8050
87+
88+
# heterogeneous microservices health url
89+
# health-check-url: http://localhost:8050/api/v1/health-check
8590

86-
# 异构微服务的健康检查URL(这里不配置的话,默认会认为是UP)
87-
health-check-url: http://localhost:8060/health.json
88-
# springboot actuator监控相关
8991
management:
9092
endpoint:
9193
health:
9294
show-details: always
9395
```
94-
注意:这里的 localhost:8060,是我本机起了一个nginx 代理了这个health.json的请求。在实际使用过程中可以是任意的REST服务,只需要返回正确的JSON格式的健康检测数据即可。
95-
```json
9696
97+
注意:这里的 localhost:8050,是异构服务地址。在实际使用过程中可以是任意的 REST 服务,只需要返回正确的 JSON 格式的健康检测数据即可。
98+
99+
```json
97100
{
98-
"status": "DOWN"
101+
"status": "UP"
99102
}
100103
```
101104

102105
### Step3: 启动应用
106+
103107
之后分别启动 Sidecar 服务、本地异构服务。
104108

105109
IDE 直接启动:找到主类 `com.alibaba.cloud.sidecar.DemoApplication`,执行 main 方法启动应用。
106110

107-
注意:本文是以 `spring-cloud-alibaba-sidecar-nacos-example`项目为例,所以启动的是它下面的`DemoApplication`启动类。
111+
注意:本文是以 `spring-cloud-alibaba-sidecar-nacos-example` 项目为例,所以启动的是它下面的 `DemoApplication` 启动类。
112+
108113
![idea.png](https://cdn.nlark.com/yuque/0/2022/png/1752280/1662550869316-98d574af-d1ba-4c00-a0af-5e33e13075fd.png)
109114

110115
### Step4: 查看服务注册情况
111116

112117
![nacos.png](https://cdn.nlark.com/yuque/0/2022/png/1752280/1662548324337-566cc824-4d08-4041-ac83-1968c7347a9e.png)
113118

119+
### Step4: 访问异构服务
114120

121+
完成上面4步,我们发现对应的服务 `sidecar-service` 已经成功注册到了注册中心。此时,这个服务已经成功的融入到了 Spring Cloud 微服务的怀抱。对于 Spring Cloud 微服务而言,访问它跟访问其它的 Java 微服务没有任何区别。
122+
而这,也正是 Spring Cloud Alibaba Sidecar 的魅力所在。接下来,将继续演示怎样访问这个服务。
115123

124+
浏览器访问:`http://127.0.0.1:8070/sidecar-service/test`
116125

117-
### Step4: 访问异构服务
118-
完成上面4步,我们发现对应的服务`node-service`已经成功注册到了注册中心。此时,这个服务已经成功的融入到了Spring Cloud 微服务的怀抱。对于Spring Cloud 微服务而言,访问它跟访问其它的Java微服务没有任何的区别。
119-
而这,也正是 Spring Cloud Alibaba Sidecar的魅力所在。接下来,我们将继续演示怎样访问这个服务。
120-
121-
浏览器访问
122-
http://127.0.0.1:8070/node-service/health.json
123126
能调通则说明整合成功。
124127

125-
![](https://cdn.nlark.com/yuque/0/2022/png/1752280/1662549893322-1b7a761a-ecd7-44ae-88b6-872eca43a866.png)
128+
![img](https://cdn.nlark.com/yuque/0/2022/png/1752280/1662549893322-1b7a761a-ecd7-44ae-88b6-872eca43a866.png)
126129

127130
## More
128131

129-
如果您对 spring cloud starter alibaba sidecar 有任何建议或想法,欢迎在 issue 中或者通过其他社区渠道向我们提出。
130-
132+
如果您对 spring cloud starter alibaba sidecar 有任何建议或想法,欢迎在 Issue 中或者通过其他社区渠道向我们提出。
Lines changed: 64 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,40 @@
1-
# Spring Cloud Alibaba Sidecar Example
1+
## Spring Cloud Alibaba Sidecar Example
22

3-
## Project Instruction
3+
## Project Description
44

5-
This project demonstrates how to use `Nacos + Spring Cloud Alibaba Sidecar` to access heterogeneous language microservices.
5+
This project demonstrates how to use Nacos + Spring Cloud Alibaba Sidecar to accomplish heterogeneous microservice access.
66

7-
[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.
7+
[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.
88

9+
## Preparation
910

11+
### Download and start Nacos
1012

11-
## Preparation
13+
**Before you can access Sidecar, you need to start Nacos Server.**
1214

13-
### Download and Startup Nacos
15+
1. Download [Nacos Server](https://github.com/alibaba/nacos/releases/download/2.1.0/nacos-server-2.1.0.zip) and unzip it. 2.
1416

15-
**You should startup Nacos Server before using Sidecar**
17+
2. Start Nacos Server
1618

17-
1. Download [Nacos](https://archive.apache.org/dist/rocketmq/4.3.2/rocketmq-all-4.3.2-bin-release.zip) and unzip it.
19+
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:
1820

19-
2. Startup Name Server
21+
```bash
22+
startup.cmd -m standalone
23+
```
2024

21-
```bash
22-
startup.cmd -m standalone
23-
```
25+
3. Log in to Nacos
26+
27+
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`);
2428

25-
3. Sign in Nacos
26-
27-
Open you browser then input `localhost:8848/nacos` ,you can see the Nacos dashboard ui .
28-
The default username and password are `nacos`
29+
## Simple Example
2930

31+
In this article, we use Nacos as a registry as an example, and Sidecar to access a non-Java language service.
3032

31-
## Simple example
32-
In this paper, Sidecar accesses a non-Java language service using Nacos as a registry as an example.
33+
> By default, we use the golang language service as an example, and we can start the `node` heterogeneous service in `application.yml`.
3334
35+
### Step1: Introducing dependencies
3436

35-
### Step1: Declare dependency
36-
Add dependency spring-cloud-starter-alibaba-sidecar to the `pom.xml` file in your Sidecar project.
37+
Modify the `pom.xml` file to introduce Spring Cloud Alibaba Sidecar Starter.
3738

3839
```xml
3940
<dependency>
@@ -52,68 +53,80 @@ Add dependency spring-cloud-starter-alibaba-sidecar to the `pom.xml` file in you
5253
</dependency>
5354
```
5455

55-
### Step2: Configure sidecar
56-
Then add necessary configurations to file `/src/main/resources/application.yml`.
56+
### Step2: Configure Sidecar related information
57+
58+
Then specify the following configuration in the project's `application.yml` configuration file:
59+
5760
```yaml
5861
server:
5962
port: 8070
63+
6064
spring:
65+
profiles:
66+
active: node
67+
6168
cloud:
6269
nacos:
63-
username: nacos
64-
password: nacos
6570
discovery:
71+
username: 'nacos'
72+
password: 'nacos'
6673
server-addr: 127.0.0.1:8848
67-
group: test
6874
gateway:
6975
discovery:
7076
locator:
7177
enabled: true
7278

7379
application:
74-
name: node-service
75-
sidecar:
76-
# heterogeneous service‘s ip
77-
ip: 127.0.0.1
78-
# heterogeneous service's port
79-
port: 8060
80+
name: sidecar-service
8081

81-
# heterogeneous service's health check URL
82-
health-check-url: http://localhost:8060/health.json
82+
sidecar:
83+
# heterogeneous microservices IP
84+
ip: 127.0.0.1
85+
# heterogeneous microservices Port
86+
port: 8050
87+
88+
# heterogeneous microservices health url
89+
# health-check-url: http://localhost:8050/api/v1/health-check
90+
91+
management:
92+
endpoint:
93+
health:
94+
show-details: always
8395
```
84-
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.
85-
```json
8696
97+
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.
98+
99+
```json
87100
{
88-
"status": "DOWN"
101+
"status": "UP"
89102
}
90103
```
91104

92-
### Step3: Start Application
93-
After that, start the `Sidecar` service and the local heterogeneous service respectively.
105+
### Step3: Starting the application
106+
107+
After that, start the Sidecar service and local heterogeneous service respectively.
94108

95-
Start in IDE: Find main class `com.alibaba.cloud.sidecar.DemoApplication`, and execute the main method.
109+
IDE direct start: find the main class `com.alibaba.cloud.sidecar.DemoApplication` and execute the main method to start the application.
110+
111+
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.
96112

97-
Note: This article takes the `spring-cloud-alibaba-sidecar-nacos-example` project as an example, so it starts the `DemoApplication` startup class under it.
98113
![idea.png](https://cdn.nlark.com/yuque/0/2022/png/1752280/1662550869316-98d574af-d1ba-4c00-a0af-5e33e13075fd.png)
99114

100-
### Step4: View service registration
101-
![nacos.png](https://cdn.nlark.com/yuque/0/2022/png/1752280/1662605412601-06780784-915c-40f6-b6b2-67176f6c5419.png)
115+
### Step4: Check the service registrations.
102116

117+
![nacos.png](https://cdn.nlark.com/yuque/0/2022/png/1752280/1662548324337-566cc824-4d08-4041-ac83-1968c7347a9e.png)
103118

119+
### Step4: Accessing Heterogeneous Services
104120

121+
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.
122+
This is the beauty of Spring Cloud Alibaba Sidecar. Next, we will continue to demonstrate how to access this service.
105123

106-
### Step4: Accessing services
107-
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.
108-
This is where the beauty of Spring Cloud Alibaba Sidecar comes in. Next, we will continue to demonstrate how to access this service.
124+
Browser Access: `http://127.0.0.1:8070/sidecar-service/test`
109125

110-
Browser Access below address:
111-
http://127.0.0.1:8070/node-service/health.json
112-
If you see the following message, the access was successful.
126+
The integration is successful if it works.
113127

114-
![](https://cdn.nlark.com/yuque/0/2022/png/1752280/1662549893322-1b7a761a-ecd7-44ae-88b6-872eca43a866.png)
128+
![img](https://cdn.nlark.com/yuque/0/2022/png/1752280/1662549893322-1b7a761a-ecd7-44ae-88b6-872eca43a866.png)
115129

116130
## More
117131

118-
If you have any ideas or suggestions for `Spring Cloud Alibaba Sidecar`, please don't hesitate to tell us by submitting github issues.
119-
132+
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.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//
2+
// Copyright 2013-2023 the original author or authors.
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// https://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
//
16+
17+
package main
18+
19+
import (
20+
"log"
21+
22+
"golang-sidecar/pkg/constant"
23+
route "golang-sidecar/pkg/route"
24+
25+
"github.com/gin-gonic/gin"
26+
)
27+
28+
func main() {
29+
30+
if err := route.SettingRouter(gin.Default()).Run(constant.ServicePort); err != nil {
31+
log.Fatal("Golang sidecar service start failed.")
32+
}
33+
34+
log.Println("Golang sidecar service listener in localhost:8060 ...")
35+
36+
}

0 commit comments

Comments
 (0)