一. 简单解释
- Nacos 可以作为注册中心,服务提供方以指定名称注册到 Nacos 上,服务消费方通过该名称在注册中心获取服务列表,本地负载进行调用
- Nacos 作为注册中心与其它注册中心的不同
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/a85c87e6fa6444e1e1c4be56bcdbf75e.png)
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/f79b77019cc8fc03117721acf50e4193.png)
- Nacos 底层通过 DiscoveryClient 实现服务的注册与发现 (SpringBoot中启动类添加 @EnableDiscoveryClient),内部整合了Ribbon 支持负载均衡
- SpringBoot 项目 Nacos 作为注册中心的使用步骤
- 启动运行 Nacos 获取到 Nacos 地址
- 创建服务提供方,服务消费方微服务
- pom 文件中引入支持 Nacos 作为注册中心的依赖
- yml 文件中配置注册服务获取服务的 Nacos 地址
- 服务提供方 yml 文件中配置暴露的端点
- 启动类添加 @EnableDiscoveryClient 注解修饰,开启 Naocs 的服务注册与发现功能
二. 服务提供方
- pom 文件中引入 nacos-discovery 依赖
<!-- SpringCloud ailibaba nacos-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
- yml 文件中增加 Nacos 相关配置
server:
port: 9001 #当前服务端口号
spring:
application:
name: nacos-payment-provider #当前服务名称
cloud:
nacos:
discovery:
server-addr: localhost:8848 #Nacos服务注册中心地址,将当前服务注册到Nacos上
#配置当前服务暴露的端点"*"代表所有
management:
endpoints:
web:
exposure:
include: '*'
- 启动类增加 @EnableDiscoveryClient 修饰
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient
public class NacosPaymentMain9001 {
public static void main(String[] args) {
SpringApplication.run(NacosPaymentMain9001.class,args);
}
}
- 编写提供服务的接口
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class PaymentController {
@Value("${server.port}")
private String serverPort;
@GetMapping(value = "/payment/nacos/{id}")
public String getPayment(@PathVariable("id") String id) {
return "服务提供方端口号: " + serverPort + ": " + id;
}
}
三. 服务消费方
- pom 文件中引入依赖,针对 Nacos 作为注册中心部分与提供方相同
- yml 文件
server:
port: 83 #当前服务端口号
spring:
application:
name: nacos-order-consumer # 当前服务名称
cloud:
nacos:
discovery:
server-addr: localhost:8848 #配置Nacos地址,服务消费方通过该地址获取服务,并将自身注册到 Nacos上
#通过配置文件创建服务消费方调用地址变量,
#在代码中调用服务时直接读取该变量即可(实现代码配置分离)
service-url:
nacos-user-service: http://nacos-payment-provider
- 此处通过 RestTemplate 调用服务消费方进行实例,创建 RestTemplate 配置类
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
@Configuration
public class ApplicationConfig {
@Bean
@LoadBalanced
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
}
- 通过 RestTemplate 调用服务消费方进行消费
@RestController
public class ConsumerController {
@Autowired
private RestTemplate restTemplate;
private String urlName = "nacos-payment-provider";
@GetMapping("/nacos/consumer/payment/{id}")
public String consumerTest(@PathVariable("id") String id) {
return restTemplate.getForObject("http://" + urlName + "/payment/nacos/" + id, String.class);
}
@Value("${service-url.nacos-user-service}")
private String serverURL;
@GetMapping("/nacos/consumer/payment2/{id}")
public String consumerTest2(@PathVariable("id") String id) {
return restTemplate.getForObject(serverURL+ "/payment/nacos/" + id, String.class);
}
}
- 启动类添加 @EnableDiscoveryClient 注解
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient
public class NacosConsumerMain83 {
public static void main(String[] args) {
SpringApplication.run(NacosConsumerMain83.class,args);
}
}
- 此时启动 Nacos ,启动服务提供方, 启动服务消费方,访问 Nacos
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/8fe70c609ec64b520c256e6f56802e4d.png)