- 第一个服务是如何注册到nacos的
- 第二个服务的接口 是 如何通过 http://service-provider/echo/找到并调用第一个服务的
code:
https://gitee.com/xiaoluxiaolu2333/nacos_examples/tree/master/nacos-spring-cloud-example
package com.alibaba.nacos.example.spring.cloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.PostConstruct;
/**
* @author xiaojing
*/
@SpringBootApplication
@EnableDiscoveryClient
public class NacosProviderApplication {
public static void main(String[] args) {
SpringApplication.run(NacosProviderApplication.class, args);
}
@RestController
class EchoController {
@RequestMapping(value = "/echo/{string}", method = RequestMethod.GET)
public String echo(@PathVariable String string) {
return "Hello Nacos Discovery " + string;
}
}
@PostConstruct
public void init() {
System.out.println("Server port: " + System.getProperty("server.port"));
System.out.println("Application name: " + System.getProperty("spring.application.name"));
System.out.println("Nacos server address: " + System.getProperty("spring.cloud.nacos.discovery.server-addr"));
}
}
package com.alibaba.nacos.example.spring.cloud;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
/**
* @author xiaojing
*/
@SpringBootApplication
@EnableDiscoveryClient
public class NacosConsumerApplication {
@LoadBalanced
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(NacosConsumerApplication.class, args);
}
@RestController
public class TestController {
private final RestTemplate restTemplate;
@Autowired
public TestController(RestTemplate restTemplate) {this.restTemplate = restTemplate;}
@RequestMapping(value = "/echo/{str}", method = RequestMethod.GET)
public String echo(@PathVariable String str) {
return restTemplate.getForObject("http://service-provider/echo/" + str, String.class);
}
}
}
这个接口的调用逻辑涉及 Spring Cloud 的服务发现机制和负载均衡实现,以下是关键点分析:
- 服务发现注解
@EnableDiscoveryClient // 激活服务发现机制
通过该注解与 Nacos 服务注册中心建立连接,自动获取已注册的服务列表
- 负载均衡配置
@LoadBalanced
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
@LoadBalanced
注解为 RestTemplate 集成客户端负载均衡能力(默认使用 Spring Cloud LoadBalancer)
- 服务调用逻辑
restTemplate.getForObject("http://service-provider/echo/" + str, String.class);
关键解析步骤:
- 服务名解析:
service-provider
是目标服务在 Nacos 注册的服务名 - 服务发现:通过 Nacos Server 查询该服务名对应的实例列表
- 负载均衡:采用轮询策略(默认)选择其中一个可用实例
- 地址替换:将服务名替换为实际 IP:Port(如 192.168.1.10:8080)
- 发起请求:向真实服务地址发送 HTTP 请求
-
完整调用链路
应用启动 → 注册到 Nacos → 获取服务列表 → RestTemplate 携带负载均衡 → 服务名解析 → 负载选择 → 实际调用 -
Nacos 服务发现原理
- 服务提供者启动时会向 Nacos 注册服务信息(IP、端口、健康状态等)
- 消费者通过服务名查询时,Nacos 返回健康实例列表
- 客户端缓存服务列表并定时更新,保证服务发现的时效性
可以通过检查以下配置确保服务发现正常工作:
# application.properties
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848 # Nacos 服务器地址
spring.application.name=service-consumer # 当前应用名称