角色说明
流程说明
引入依赖
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
编写配置文件
management:
endpoints:
web:
exposure:
include: '*' #暴露所有端点
server:
port: 8081
spring:
application:
name: service-provider
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848 #nacos地址
启动类添加注解
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient
public class ServiceProviderApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceProviderApplication.class, args);
}
}
页面查看
引入依赖
编写bootstrap.yml配置文件
bootstrap.yml
是系统级别的,加载优先级高于application.yml
,负责从外部加载配置并解析
management:
endpoints:
web:
exposure:
include: '*' #springboot监控开启所有端点
server:
port: 8083
spring:
application:
name: config-client
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848 #服务注册到nacos注册中心的地址
config:
server-addr: 127.0.0.1:8848 #Nacos作为配置中心地址
file-extension: yaml #指定yaml格式的配置
编写application.yml
配置文件
可以通过指定不同的激活文件配合Data Id从nacos获取不同环境下的配置
spring:
profiles:
active: dev #激活 dev 的配置
nacos 控制台添加配置
Nacos Server 配置的Data ID的完整格式如下:${prefix}-${spring.profiles.active}.${file-extension} dataId 格式中各参数说明如下:
spring.application.name
的值,我们可以在配置文件中通过配置 spring.cloud.nacos.config.prefix
来指定。spring.cloud.nacos.config.file-extension
来配置,例如 properties 和 yaml。启动类添加注解
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient
public class ConfigClientApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigClientApplication.class, args);
}
}
编写配置类获取配置
注意注解@RefreshScope
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix = "config")
@RefreshScope
public class Config {
private String version;
private String name;
public String getVersion() {
return version;
}
public void setVersion(String version) {
this.version = version;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
配置使用
注意注解@RefreshScope
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RefreshScope
@RestController
@RequestMapping("api/vi/config-client")
public class Controller {
@Autowired
private Config config;
@GetMapping
public String get() {
return "name:" + config.getName() + " | version:" + config.getVersion();
}
}