一、相关文档
必读文档 | 地址 |
---|---|
nacos | 官方文档 |
spring-cloud-alibaba | 版本说明 |
二、Nacos版本
组件 | 版本 |
---|---|
nacos | 2.0.3 |
三、代码
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 https://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.4.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>customer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>customer</name>
<description>Demo project for Spring Boot</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.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>2021.1</version>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
<version>2021.1</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
<version>3.0.3</version>
</dependency>
<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>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
2、Controller
package com.example.customer.controller;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RefreshScope
@RestController
@RequestMapping("/customer")
public class CustomerController {
@Value("${world}")
public String world;
@GetMapping("hello")
public String hello(){
return world;
}
}
3、bootstrap.yml
spring:
application:
name: customer
profiles:
# 激活开发环境配置
active: dev
cloud:
nacos:
config:
server-addr: 127.0.0.1:8848
group: CloudDemo
username: nacos
password: nacos
namespace: public
file-extension: yaml
discovery:
server-addr: 127.0.0.1:8848
group: CloudDemo
username: nacos
password: nacos
namespace: public
4、Nacos配置管理
新建 customer-dev.yaml、customer-test.yaml
# 具体的配置
world: hello world!
# 阻止SpringBoot输出大量心跳日志
logging:
level:
com.alibaba.nacos.client.config.impl: WARN
四、Bug记录
五、效果展示
1、配置中心
访问接口 http://localhost:8080/customer/hello
输出 hello world!