目录
一、Spring Boot和Spring Cloud的版本号说明
4.2 创建bootstrap.yml配置文件(系统级别参数配置 )
4.3 启动类上要加@EnableDiscoveryClient注解
5.1 修改Nacos文件下的application.properties文件
一、Spring Boot和Spring Cloud的版本号说明
1.1 版本号说明:
Spring Cloud是基于Springboot的对应版本号开发的,属于依赖的关系,所以不能都选用最新版本
下表为按时间顺序发布的 Spring Cloud Alibaba 以及对应的适配 Spring Cloud 和 Spring Boot 版本关系:
Spring Cloud Alibaba Version | Spring Cloud Version | Spring Boot Version |
---|---|---|
2021.0.1.0 | Spring Cloud 2021.0.1 | 2.6.3 |
2.2.7.RELEASE | Spring Cloud Hoxton.SR12 | 2.3.12.RELEASE |
2021.1 | Spring Cloud 2020.0.1 | 2.4.2 |
2.2.6.RELEASE | Spring Cloud Hoxton.SR9 | 2.3.2.RELEASE |
2.1.4.RELEASE | Spring Cloud Greenwich.SR6 | 2.1.13.RELEASE |
2.2.1.RELEASE | Spring Cloud Hoxton.SR3 | 2.2.5.RELEASE |
2.2.0.RELEASE | Spring Cloud Hoxton.RELEASE | 2.2.X.RELEASE |
2.1.2.RELEASE | Spring Cloud Greenwich | 2.1.X.RELEASE |
2.0.4.RELEASE(停止维护,建议升级) | Spring Cloud Finchley | 2.0.X.RELEASE |
1.5.1.RELEASE(停止维护,建议升级) | Spring Cloud Edgware | 1.5.X.RELEASE |
二、Nacos简介与准备工作
2.1 Nacos是什么
Nacos 是阿里巴巴的新开源项目,其核心定位是 “一个更易于帮助构建云原生应用的集注册中心与配置中心于一体的管理平台”。
2.2 pom.xml依赖引入
<!--父模块的依赖-->
<dependencyManagement>
<dependencies>
<!--Spring Boot-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.3.2.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!--spring cloud Netflix-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Hoxton.SR9</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!--spring cloud 阿里巴巴-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2.2.6.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<!--子模块的依赖-->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--配置管理-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<!--nacos客户端-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
</dependencies>
2.3 Nacos安装和启动
下载地址:Tags · alibaba/nacos · GitHub
在虚拟机上安装Nacos
先把nacos压缩包上传至虚拟机/usr/upload目录下
解压安装包
[root@localhost ~]# cd /usr/upload
[root@localhost upload]# tar -zxvf nacos-server-1.4.1.tar.gz -C /usr/local
启动
[root@localhost local]# cd nacos/bin/
[root@localhost bin]# ./startup.sh -m standalone #非集群模式启动
关闭
[root@localhost bin]# ./shutdown.sh
The nacosServer(3543) is running...
Send shutdown request to nacosServer(3543) OK
2.4 测试
浏览器访问:http://虚拟机的ip:8848/nacos,默认用户名/密码为: nacos/nacos
三、Nacos注册中心(存放服务)
3.1 什么是Nacos注册中心
注册中心主要由三部分组成: Nacos-Server:注册中心,Nacos-Provider:服务提供者,Nacos-Consumer:服务调用者
在这里 注册中心 相当于中介的角色,提供服务的注册和发现。
服务提供者 相当于房东的角色,把自身的服务实例注册到 Nacos Server 中
服务调用者 相当于消费者,通过 Nacos Server 获取服务列表,消费服务。
3.2 服务提供者: nacos_provider
application.yml设置
spring:
cloud:
nacos:
discovery:
server-addr: 192.168.209.129:8848 #nacos服务的地址
application:
name: nacos-provider #向注册中心注册的名字
SpringBoot启动类上添加@EnableDiscoveryClient注解
@SpringBootApplication
@EnableDiscoveryClient//向注册中心注册该服务,并可以获取其他服务的调用地址
public class ProviderApp {
public static void main(String[] args) {
SpringApplication.run(ProviderApp.class);
}
}
3.3 启动测试
在Nacos页面中可以看到注册后服务项的信息
3.4 服务调用者: nacos_consumer
application.yml设置
spring:
cloud:
nacos:
discovery:
server-addr: 192.168.209.129:8848 #nacos服务的地址
application:
name: nacos-consumer #向注册中心注册的名字
SpringBoot启动类上添加@EnableDiscoveryClient注解
@SpringBootApplication
@EnableDiscoveryClient//向注册中心注册该服务,并可以获取其他服务的调用地址
public class ConsumerApp {
public static void main(String[] args) {
SpringApplication.run(ConsumerApp.class);
}
}
controller层调用提供者
@RestController
@RequestMapping(value &