服务注册与发现
2026/1/15大约 2 分钟Consul服务注册服务发现
服务注册与发现
一、服务注册
1.1 配置文件注册
// /etc/consul.d/user-service.json
{
"service": {
"id": "user-service-1",
"name": "user-service",
"tags": ["primary", "v1"],
"address": "192.168.1.100",
"port": 8080,
"meta": {
"version": "1.0.0",
"env": "production"
},
"check": {
"http": "http://192.168.1.100:8080/health",
"interval": "10s",
"timeout": "5s"
}
}
}1.2 HTTP API 注册
# 注册服务
curl -X PUT http://localhost:8500/v1/agent/service/register \
-H "Content-Type: application/json" \
-d '{
"ID": "user-service-1",
"Name": "user-service",
"Tags": ["primary"],
"Address": "192.168.1.100",
"Port": 8080,
"Check": {
"HTTP": "http://192.168.1.100:8080/health",
"Interval": "10s"
}
}'1.3 命令行注册
consul services register -name=user-service \
-id=user-service-1 \
-address=192.168.1.100 \
-port=8080 \
-tag=primary二、服务发现
2.1 HTTP API 查询
# 查询所有服务
curl http://localhost:8500/v1/catalog/services
# 查询特定服务
curl http://localhost:8500/v1/catalog/service/user-service
# 查询健康的服务实例
curl http://localhost:8500/v1/health/service/user-service?passing=true2.2 DNS 查询
# 查询服务
dig @127.0.0.1 -p 8600 user-service.service.consul
# 查询带 Tag 的服务
dig @127.0.0.1 -p 8600 primary.user-service.service.consul
# 查询 SRV 记录(包含端口)
dig @127.0.0.1 -p 8600 user-service.service.consul SRV2.3 服务发现流程
三、Spring Cloud Consul
3.1 添加依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>3.2 配置文件
spring:
application:
name: user-service
cloud:
consul:
host: localhost
port: 8500
discovery:
service-name: ${spring.application.name}
instance-id: ${spring.application.name}:${server.port}
prefer-ip-address: true
health-check-path: /actuator/health
health-check-interval: 10s3.3 启动类
@SpringBootApplication
@EnableDiscoveryClient
public class UserServiceApplication {
public static void main(String[] args) {
SpringApplication.run(UserServiceApplication.class, args);
}
}3.4 服务调用
@RestController
public class OrderController {
@Autowired
private DiscoveryClient discoveryClient;
@Autowired
@LoadBalanced
private RestTemplate restTemplate;
@GetMapping("/order/{id}")
public Order getOrder(@PathVariable Long id) {
// 使用服务名调用
String url = "http://user-service/user/" + id;
User user = restTemplate.getForObject(url, User.class);
Order order = new Order();
order.setId(id);
order.setUser(user);
return order;
}
@GetMapping("/instances")
public List<ServiceInstance> getInstances() {
return discoveryClient.getInstances("user-service");
}
}四、服务注销
4.1 HTTP API 注销
curl -X PUT http://localhost:8500/v1/agent/service/deregister/user-service-14.2 命令行注销
consul services deregister -id=user-service-14.3 Spring Cloud 自动注销
Spring Cloud Consul 在应用关闭时会自动注销服务。
五、服务元数据
5.1 配置元数据
spring:
cloud:
consul:
discovery:
metadata:
version: v1.0.0
region: beijing
weight: 105.2 读取元数据
@GetMapping("/metadata")
public Map<String, String> getMetadata() {
List<ServiceInstance> instances = discoveryClient.getInstances("user-service");
if (!instances.isEmpty()) {
return instances.get(0).getMetadata();
}
return Collections.emptyMap();
}六、服务标签
6.1 配置标签
spring:
cloud:
consul:
discovery:
tags:
- primary
- v1
- env=production6.2 按标签查询
# DNS 查询
dig @127.0.0.1 -p 8600 primary.user-service.service.consul
# HTTP API
curl "http://localhost:8500/v1/catalog/service/user-service?tag=primary"