spring cloud + nacos 服务注册与发现_分析

  • 第一个服务是如何注册到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 的服务发现机制和负载均衡实现,以下是关键点分析:

  1. 服务发现注解
@EnableDiscoveryClient // 激活服务发现机制

通过该注解与 Nacos 服务注册中心建立连接,自动获取已注册的服务列表

  1. 负载均衡配置
@LoadBalanced
@Bean
public RestTemplate restTemplate() {
    return new RestTemplate();
}

@LoadBalanced 注解为 RestTemplate 集成客户端负载均衡能力(默认使用 Spring Cloud LoadBalancer)

  1. 服务调用逻辑
restTemplate.getForObject("http://service-provider/echo/" + str, String.class);

关键解析步骤:

  • 服务名解析service-provider 是目标服务在 Nacos 注册的服务名
  • 服务发现:通过 Nacos Server 查询该服务名对应的实例列表
  • 负载均衡:采用轮询策略(默认)选择其中一个可用实例
  • 地址替换:将服务名替换为实际 IP:Port(如 192.168.1.10:8080)
  • 发起请求:向真实服务地址发送 HTTP 请求
  1. 完整调用链路
    应用启动 → 注册到 Nacos → 获取服务列表 → RestTemplate 携带负载均衡 → 服务名解析 → 负载选择 → 实际调用

  2. Nacos 服务发现原理

  • 服务提供者启动时会向 Nacos 注册服务信息(IP、端口、健康状态等)
  • 消费者通过服务名查询时,Nacos 返回健康实例列表
  • 客户端缓存服务列表并定时更新,保证服务发现的时效性

可以通过检查以下配置确保服务发现正常工作:

# application.properties
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848 # Nacos 服务器地址
spring.application.name=service-consumer # 当前应用名称
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值