不会Nacos的配置中心?你怎么敢的呀!如何一步步实现Nacos作为服务的配置中心(全网最详系列)
原创
©著作权归作者所有:来自51CTO博客作者漫话人生的原创作品,请联系作者获取转载授权,否则将追究法律责任
1
、
前
置
准
备
{1、前置准备}
1、前置准备
首先应该安装好nacos服务,我使用的是云服务器安装的nacos;
下载安装教程:javascript:void(0)
nacos官网地址:https://nacos.io/en-us/docs/quick-start.html
springcloud alibaba-nacos地址:https://spring-cloud-alibaba-group.github.io/github-pages/hoxton/en-us/index.html#_spring_cloud_alibaba_nacos_config
2、框架搭建
Ⅰ、搭建父工程
<?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>
<groupId>com.hao</groupId>
<artifactId>cloudAlibaba-nacos</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>nacos-confing</module>
</modules>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.6.RELEASE</version>
</parent>
<properties>
<spring.cloud.alibaba.version>2021.1</spring.cloud.alibaba.version>
<spring.cloud.alibaba.nacos.version>2.1.1.RELEASE</spring.cloud.alibaba.nacos.version>
<spring.cloud.alibaba.nacos.config.version>2.1.1.RELEASE</spring.cloud.alibaba.nacos.config.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>${spring.cloud.alibaba.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
<version>${spring.cloud.alibaba.nacos.config.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>${spring.cloud.alibaba.nacos.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>
Ⅱ、创建一个子模块nacos-config
1、子模块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">
<parent>
<artifactId>cloudAlibaba-nacos</artifactId>
<groupId>com.hao</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>nacos-confing</artifactId>
<dependencies>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
2、编写controller
@RestController
@RefreshScope
public class ConfigController {
@Value("${config.info}") //注入nacos服务中心我自定义的配置项(后面会详细说)
private String configInfo;
@GetMapping(value = "/config/info")
public String getInfo() {
return configInfo;
}
}
bootstrap.yml和application.yml的区别是bootstrap早于application加载(加载时机不同)
3、bootstrap.yml
server:
port: 8082
spring:
application:
name: nacos-config-client #服务名
cloud:
nacos:
discovery:
server-addr: 你的ip:8848 #注册进nacos的服务地址
config:
server-addr: 你的ip:8848
file-extension: yaml #配置中心的配置文件类型(后面详解)
4、application.yml
spring:
profiles:
active: dev #表示当前处于开发环境
3、配置中心设置
注意需要先启动nacos服务,如果有疑问请移步:javascript:void(0)
由于我在controller使用@Value注解获取配置中心的配置文件的config.info的值,所以配置中心的配置文件需要有这个配置项
那么如何在配置中心配置这个值呢?且听我细细道来
点击+号
最主要的是要找对这个Data ID,如何找呢?
匹配模式:${spring.application.name}- ${spring.profiles.active}. ${spring.cloud.nacos.config.file-extension}
启动nacos-config服务
成功从nacos配置中心读取配置项了!