注:本篇文章主要参考周阳老师讲解的cloud进行整理的!
1、总体介绍
2、Nacos简介
2.1、是什么
https://nacos.io/docs/latest/what-is-nacos/
一个更易于构建云原生应用的动态服务发现、配置管理和服务管理平台
- 一句话
Nacos就是注册中心 + 配置中心的组合
Nacos = Eureka + Config + Bus
Nacos = Spring Cloud Consul
2.2、能干嘛
- 替代Eureka/Consul做服务注册中心
- 替代(Config + Bus)/ Consul做服务配置中心和满足动态刷新广播通知
2.3、去哪下
https://nacos.io/zh-cn
2.4、各种注册中心比较
据说 Nacos 在阿里巴巴内部有超过 10 万的实例运行,已经过了类似双十一等各种大型流量的考验,Nacos默认是AP模式, 但也可以调整切换为CP,我们一般用默认AP即可。
3、Nacos下载安装
- 本地Java17 + Maven环境
3.1、官网下载Nacos
https://nacos.io/zh-cn/index.html
https://nacos.io/docs/v2/quickstart/quick-start/
https://github.com/alibaba/nacos/releases
3.2、解压安装包,直接运行bin目录下的startup.cmd
startup.cmd -m standalone
3.3、命令运行成功后直接访问
http://localhost:8848/nacos
默认账号密码都是nacos
3.4、结果页面
3.5、关闭服务器
shutdown.cmd
4、Nacos Discovery服务注册中心
4.1、概述
https://nacos.io/zh-cn/docs/v2/ecology/use-nacos-with-spring-cloud.html
4.2、SpringCloudAlibaba参考中文文档
https://spring-cloud-alibaba-group.github.io/github-pages/2022/zh-cn/2022.0.0.0-RC2.html
4.3、基于Nacos的服务提供者
4.3.1、新建Module
4.3.2、pom
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.mui.cloud</groupId>
<artifactId>cloud2024</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<groupId>com.mui.cloud</groupId>
<artifactId>cloudalibaba-provider-payment9001</artifactId>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!--nacos-discovery-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!-- 引入自己定义的api通用包 -->
<dependency>
<groupId>com.mui.cloud</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!--SpringBoot通用依赖模块-->
<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>
<!--hutool-->
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
</dependency>
<!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.28</version>
<scope>provided</scope>
</dependency>
<!--test-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
4.3.3、YML
server:
port: 9001
spring:
application:
name: nacos-payment-provider
cloud:
nacos:
discovery:
# 配置Nacos地址
server-addr: localhost:8848
4.3.4、主启动
package com.mui.cloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient
public class Main9001 {
public static void main(String[] args) {
SpringApplication.run(Main9001.class, args);
}
}
4.3.5、业务类
package com.mui.cloud.controller;
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 PayAlibabaController {
@Value("${server.port}")
private String serverPort;
@GetMapping("/pay/nacos/{id}")
public String getPayInfo(@PathVariable("id") Integer id) {
return "nacos registry, serverport:" + serverPort + "\t id:" + id;
}
}
4.3.6、测试
http://localhost:9001/pay/nacos/1
nacos控制台
nacos8448注册中心 + 服务提供者pay9001都OK了
4.4、基于Nacos的服务消费者
4.4.1、新建Module
4.4.2、POM
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.mui.cloud</groupId>
<artifactId>cloud2024</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<groupId>com.mui.cloud</groupId>
<artifactId>cloudalibaba-consumer-nacos-order83</artifactId>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!--nacos-discovery-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!--loadbalancer-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
</dependency>
<!--web + actuator-->
<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>
<!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
4.4.3、YML
server:
port: 83
spring:
application:
name: nacos-order-consumer
cloud:
nacos:
discovery:
server-addr: localhost:8848
#消费者将要去访问的微服务名称(nacos微服务提供者叫什么你写什么)
service-url:
nacos-user-service: http://nacos-payment-provider
4.4.4、业务类-配置类
package com.mui.cloud.config;
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 RestTemplateConfig {
@Bean
// 赋予RestTemplate负载均衡的能力
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
4.4.5、业务类-控制器
package com.mui.cloud.controller;
import jakarta.annotation.Resource;
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;
import org.springframework.web.client.RestTemplate;
@RestController
public class OrderNacosController {
@Resource
private RestTemplate restTemplate;
@Value("${service-url.nacos-user-service}")
private String serverURL;
@GetMapping("/consumer/pay/nacos/{id}")
public String paymentInfo(@PathVariable("id") Integer id) {
String result = restTemplate.getForObject(serverURL + "/pay/nacos/" + id, String.class);
return result + "\t" + " 我是OrderNacosController83调用者。。。。。。";
}
}
4.4.6、测试
- nacos控制台
http://localhost:83/consumer/pay/nacos/1
4.4.7、负载均衡
-
参考9001新建9002
-
测试
- http://localhost:83/consumer/pay/nacos/1 可以看到端口号9001/9002交替出现,负载均衡达到