Skip to content

Commit 01c5937

Browse files
committed
initial commit
0 parents  commit 01c5937

12 files changed

+268
-0
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.java-version
2+
*.iml
3+
target/

README.md

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
# SOFA RPC Consul Registry Demo
2+
3+
这是一个基于 [sofa-rpc](https://github.com/sofastack/sofa-rpc) 5.6.0 版本 consul-registry 演示的 demo,会介绍如何使用 consul 作为 sofa-rpc 的注册中心,并使用自定义的 service name、health check 配置。
4+
5+
## Step 1,基础服务搭建
6+
7+
参考 pom 中的依赖配置和 src 下的 `HelloController``HelloService``HelloServiceImpl`,搭建一个最小的 sofa-rpc 调用环境。
8+
9+
为了演示自定义 http health check 的效果,我们额外引入了 `healthcheck-sofa-boot-starter`,启动后可以在 `/actuator/readiness` 中查看到服务的健康状态。
10+
11+
## Step 2,使用 Consul Registry
12+
13+
使用 consul registry 需要在 pom 中额外加入 consul 依赖,这里使用的版本为 `com.ecwid.consul:consul-api:1.4.2`
14+
15+
然后在 `application.yml` 中,通过 `com.alipay.sofa.rpc.registry.address` 配置 consul 注册中心的地址,最简配置为:`consul://127.0.0.1:8500`
16+
17+
## Step 3,启动服务
18+
19+
在本地启动 consul server(例如 `consul agent -dev`)后启动服务,查看 `localhost:8500` 的 consul ui,可以看到服务已经注册成功。
20+
21+
![consul services](images/consul-services.jpg)
22+
23+
点击可以看到服务的健康检查信息,默认的健康检查方式是 ttl 定期上报。
24+
25+
![consul service health](images/consul-service-health.jpg)
26+
27+
访问提供的接口 `http://localhost:8090/say/hello-world`,可以看到正常调用并得到了返回信息。
28+
29+
## Step 4,使用自定义别名
30+
31+
默认的 consul 服务名对应 sofa-rpc 中的 interfaceId,但是因为 interfaceId 比较长可能不利于人类阅读,并且其中含有 `.` 导致无法使用 consul 的 dns 功能,所以可以通过配置给每一个服务配置更可读的名称。
32+
33+
通过 `@SofaServiceBinding``@SofaReferenceBinding``paramters` 变量可以配置参数信息,使用 `ConsulConstants.CONSUL_SERVICE_NAME_KEY` 可以修改注册的 service name。
34+
35+
```
36+
@SofaService(
37+
bindings = @SofaServiceBinding(
38+
bindingType = "bolt",
39+
parameters = @SofaParameter(key = ConsulConstants.CONSUL_SERVICE_NAME_KEY, value = "${spring.application.name}-hello-service")
40+
)
41+
)
42+
```
43+
44+
```
45+
@SofaReference(
46+
binding = @SofaReferenceBinding(
47+
bindingType = "bolt",
48+
parameters = @SofaParameter(key = ConsulConstants.CONSUL_SERVICE_NAME_KEY, value = "${spring.application.name}-hello-service")
49+
),
50+
jvmFirst = false
51+
)
52+
private HelloService helloService;
53+
```
54+
55+
注意:service 和 reference 中的配置必须相同才能正常进行服务发现,否则 refernce 将无法找到 service。
56+
57+
启动后可以再次查看 consul ui,可以看到 service name 变成了修改后的值。
58+
59+
![consul service alias](images/consul-service-alias.jpg)
60+
61+
## Step 5,使用自定义健康检查
62+
63+
consul 作为注册中心的一个非常强大的特性就是,它支持 agent 主动发起健康检查,例如 http/tcp 调用,而不仅仅依赖于应用的 ttl 上报。
64+
65+
配置自定义的健康检查只需要修改 `application.yml` 中的 `com.alipay.sofa.rpc.registry.address`,在 url 的 query 参数中增加对应配置即可。
66+
67+
以使用 sofa-boot 的 readiness endpoint 进行健康检查为例,只需要配置 `healthCheck.type` 为 http、`healthCheck.port``server.port``healthCheck.path``/actuator/readiness` 即可:
68+
69+
```
70+
com:
71+
alipay:
72+
sofa:
73+
rpc:
74+
registry:
75+
address: consul://127.0.0.1:8500?healthCheck.type=http&healthCheck.path=/actuator/readiness&healthCheck.port=${server.port}
76+
```
77+
78+
修改后重启应用,查看 consul ui,等待 consul agent 进行第一次健康检查之后,查看健康检查信息,可以发现 output 一栏中显示着 health check 的结果。
79+
80+
![consul service alias](images/consul-service-http-health-check.jpg)

images/consul-service-alias.jpg

97.4 KB
Loading

images/consul-service-health.jpg

87.8 KB
Loading
144 KB
Loading

images/consul-services.jpg

99.6 KB
Loading

pom.xml

+106
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>com.scienjus</groupId>
8+
<artifactId>sofa-rpc-consul-registry-demo</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
11+
<name>sofa-rpc-consul-registry-demo</name>
12+
<url>https://www.sofastack.tech</url>
13+
14+
<properties>
15+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
16+
<maven.compiler.source>1.8</maven.compiler.source>
17+
<maven.compiler.target>1.8</maven.compiler.target>
18+
</properties>
19+
20+
<parent>
21+
<groupId>com.alipay.sofa</groupId>
22+
<artifactId>sofaboot-dependencies</artifactId>
23+
<version>3.1.5</version>
24+
</parent>
25+
26+
<dependencies>
27+
<dependency>
28+
<groupId>com.alipay.sofa</groupId>
29+
<artifactId>rpc-sofa-boot-starter</artifactId>
30+
</dependency>
31+
32+
<dependency>
33+
<groupId>org.springframework.boot</groupId>
34+
<artifactId>spring-boot-starter-web</artifactId>
35+
</dependency>
36+
37+
<dependency>
38+
<groupId>com.alipay.sofa</groupId>
39+
<artifactId>sofa-rpc-all</artifactId>
40+
<version>5.6.0</version>
41+
</dependency>
42+
43+
<dependency>
44+
<groupId>com.alipay.sofa</groupId>
45+
<artifactId>healthcheck-sofa-boot-starter</artifactId>
46+
</dependency>
47+
48+
<dependency>
49+
<groupId>com.alipay.sofa</groupId>
50+
<artifactId>bolt</artifactId>
51+
<version>1.5.2</version>
52+
</dependency>
53+
54+
<dependency>
55+
<groupId>com.ecwid.consul</groupId>
56+
<artifactId>consul-api</artifactId>
57+
<version>1.4.2</version>
58+
</dependency>
59+
</dependencies>
60+
61+
<build>
62+
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
63+
<plugins>
64+
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
65+
<plugin>
66+
<artifactId>maven-clean-plugin</artifactId>
67+
<version>3.1.0</version>
68+
</plugin>
69+
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
70+
<plugin>
71+
<artifactId>maven-resources-plugin</artifactId>
72+
<version>3.0.2</version>
73+
</plugin>
74+
<plugin>
75+
<artifactId>maven-compiler-plugin</artifactId>
76+
<version>3.8.0</version>
77+
</plugin>
78+
<plugin>
79+
<artifactId>maven-surefire-plugin</artifactId>
80+
<version>2.22.1</version>
81+
</plugin>
82+
<plugin>
83+
<artifactId>maven-jar-plugin</artifactId>
84+
<version>3.0.2</version>
85+
</plugin>
86+
<plugin>
87+
<artifactId>maven-install-plugin</artifactId>
88+
<version>2.5.2</version>
89+
</plugin>
90+
<plugin>
91+
<artifactId>maven-deploy-plugin</artifactId>
92+
<version>2.8.2</version>
93+
</plugin>
94+
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
95+
<plugin>
96+
<artifactId>maven-site-plugin</artifactId>
97+
<version>3.7.1</version>
98+
</plugin>
99+
<plugin>
100+
<artifactId>maven-project-info-reports-plugin</artifactId>
101+
<version>3.0.0</version>
102+
</plugin>
103+
</plugins>
104+
</pluginManagement>
105+
</build>
106+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.scienjus.sofa.rpc.consul;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class Application {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(Application.class, args);
11+
}
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.scienjus.sofa.rpc.consul;
2+
3+
import com.alipay.sofa.rpc.registry.consul.ConsulConstants;
4+
import com.alipay.sofa.runtime.api.annotation.SofaParameter;
5+
import com.alipay.sofa.runtime.api.annotation.SofaReference;
6+
import com.alipay.sofa.runtime.api.annotation.SofaReferenceBinding;
7+
import org.springframework.web.bind.annotation.GetMapping;
8+
import org.springframework.web.bind.annotation.PathVariable;
9+
import org.springframework.web.bind.annotation.RestController;
10+
11+
@RestController
12+
public class HelloController {
13+
14+
@SofaReference(
15+
binding = @SofaReferenceBinding(
16+
bindingType = "bolt",
17+
parameters = @SofaParameter(key = ConsulConstants.CONSUL_SERVICE_NAME_KEY, value = "${spring.application.name}-hello-service")
18+
),
19+
jvmFirst = false
20+
)
21+
private HelloService helloService;
22+
23+
@GetMapping("/say/{word}")
24+
public String hello(@PathVariable("word") String word) {
25+
return helloService.say(word);
26+
}
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.scienjus.sofa.rpc.consul;
2+
3+
public interface HelloService {
4+
5+
String say(String string);
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.scienjus.sofa.rpc.consul;
2+
3+
import com.alipay.sofa.rpc.registry.consul.ConsulConstants;
4+
import com.alipay.sofa.runtime.api.annotation.SofaParameter;
5+
import com.alipay.sofa.runtime.api.annotation.SofaService;
6+
import com.alipay.sofa.runtime.api.annotation.SofaServiceBinding;
7+
import org.springframework.stereotype.Service;
8+
9+
@Service
10+
@SofaService(
11+
bindings = @SofaServiceBinding(
12+
bindingType = "bolt",
13+
parameters = @SofaParameter(key = ConsulConstants.CONSUL_SERVICE_NAME_KEY, value = "${spring.application.name}-hello-service")
14+
)
15+
)
16+
public class HelloServiceImpl implements HelloService {
17+
@Override
18+
public String say(String string) {
19+
return "Say: " + string;
20+
}
21+
}

src/main/resources/application.yml

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
spring:
2+
application:
3+
name: consul-demo
4+
5+
server:
6+
port: 8090
7+
8+
com:
9+
alipay:
10+
sofa:
11+
rpc:
12+
registry:
13+
address: consul://127.0.0.1:8500?healthCheck.type=http&healthCheck.path=/actuator/readiness&healthCheck.port=${server.port}

0 commit comments

Comments
 (0)