本文介绍 SpringBoot 如何使用阿里巴巴 Nacos 做配置中心.
1.Nacos 简介
Nacos 是阿里巴巴集团开源的一个易于使用的平台, 专为动态服务发现, 配置和服务管理而设计. 它可以帮助您轻松构建云本机应用程序和微服务平台.
Nacos 基本上支持现在所有类型的服务, 例如, Dubbo / gRPC 服务, Spring Cloud RESTFul 服务或 Kubernetes 服务.
尤其是使用 Eureka 注册中心的, 并且担心 Eureka 闭源的开发者们, 可以将注册中心修改为 Nacos, 本文主要介绍 Nacos 配置中心的使用.
Nacos 官网如下图所示, 官网地址 https://nacos.io/zh-cn/
2.Nacos 安装
Nacos 安装可以采用如下两种方式:
1. 官网下载稳定版本解压使用.
2. 下载源代码编译使用, 目前最新的版本是 0.8.0 版本.
本文简单介绍一下第二种方式, 到 Nacos 的稳定版本下载地址 https://github.com/alibaba/nacos/releases , 下载最新版, 本文下的是 tag.gz 文件, 下载后解压即安装完成, 然后进入解压目录后的 bin 目录执行如下命令启动 Nacos.
sh startup.sh -m standalone
启动可以看到控制台如图所示, 端口号是 8848(好像是因为珠穆朗玛峰的高度), 版本 0.8.0 等等信息.
3.SpringBoot 使用 Nacos
接下来, 创建项目, 项目中加入使用 Nacos 配置中心的依赖 nacos-config-spring-boot-starter, 完整 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>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-parent</artifactId>
- <version>2.1.1.RELEASE</version>
- <relativePath/> <!-- lookup parent from repository -->
- </parent>
- <groupId>com.dalaoyang</groupId>
- <artifactId>springboot2_nacos_config</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- <name>springboot2_nacos_config</name>
- <description>springboot2_nacos_config</description>
- <properties>
- <java.version>1.8</java.version>
- </properties>
- <dependencies>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-web</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-devtools</artifactId>
- <scope>runtime</scope>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-test</artifactId>
- <scope>test</scope>
- </dependency>
- <!-- https://mvnrepository.com/artifact/com.alibaba.boot/nacos-config-spring-boot-starter -->
- <dependency>
- <groupId>com.alibaba.boot</groupId>
- <artifactId>nacos-config-spring-boot-starter</artifactId>
- <version>0.2.1</version>
- </dependency>
- </dependencies>
- <build>
- <plugins>
- <plugin>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-maven-plugin</artifactId>
- </plugin>
- </plugins>
- </build>
- </project>
配置文件中需要配置 Nacos 服务的地址, 如下所示.
- spring.application.name=springboot2-nacos-config
- nacos.config.server-addr=127.0.0.1:8848
在启动类, 加入 @NacosPropertySource 注解其中包含两个属性, 如下:
dataId: 这个属性是需要在 Nacos 中配置的 Data Id.
autoRefreshed: 为 true 的话开启自动更新.
在使用 Nacos 做配置中心后, 需要使用 @NacosValue 注解获取配置, 使用方式与 @Value 一样, 完整启动类代码如下所示.
- package com.dalaoyang;
- import com.alibaba.nacos.API.config.annotation.NacosValue;
- import com.alibaba.nacos.spring.context.annotation.config.NacosPropertySource;
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
- import org.springframework.Web.bind.annotation.GetMapping;
- import org.springframework.Web.bind.annotation.RestController;
- @SpringBootApplication
- @NacosPropertySource(dataId = "springboot2-nacos-config", autoRefreshed = true)
- @RestController
- public class Springboot2NacosConfigApplication {
- public static void main(String[] args) {
- SpringApplication.run(Springboot2NacosConfigApplication.class, args);
- }
- @NacosValue(value = "${nacos.test.propertie:123}", autoRefreshed = true)
- private String testProperties;
- @GetMapping("/test")
- public String test(){
- return testProperties;
- }
- }
由于本文只是简单示例使用 Nacos 做配置中心, 所以将启动类加了一个 MVC 方法, 作为输出配置信息进行测试, 这个测试的配置给了一个默认值 123, 启动项目, 访问 http://localhost:8080/test , 可以看到如下所示:
4. 使用 Nacos 修改配置
访问 Nacos 服务, http://localhost:8848/nacos/#/login , 默认情况用户名密码都是 nacos, 登录页如图所示.
登录后如图所示.
接下来点击右侧加号, 添加我们刚刚创建的 data id 的服务, 并将配置由 123 修改为 111, 如图所示.
然后点击右下角发布按钮, 再次访问 http://localhost:8080/test 如图所示.
到这里 SpringBoot 使用 Nacos 配置中心就完成了, 感兴趣可以查看源码仔细研究.
来源: https://yq.aliyun.com/articles/689618