springcloud nacos
1.POM
<!--nacos-config-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
        </dependency>
<!--        nacos-discovery-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
2. yml
Why 需要两个配置文件?
Nacos同springcloud-config 一样,在项目初始化时,要保证先从配置中心进行配置拉取,拉取配置以后,才能保证项目的正常运行
springboot中配置文件的加载存在优先级别顺序 bootstrap.yml 优先级别高于 application.yml
application.yml
server:
  port: 8887
spring:
  profiles:
    active: dev
bootstrap.yml
spring:
  application:
    name: cloud-consumer-service
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848 #服务注册中心地址
      config:
        server-addr: localhost:8848 #配置中心地址
        file-extension: yaml #指定yaml格式的配置
        group: DEV_GROUP
        namespace: 23567cb2-1be6-4fa5-8dd4-8f0d817474c5
# ${spring.application.name}-${spring.profile.active}.${spring.cloud.nacos.config.file-extension}
# nacos-config-client-dev.yaml
# nacos-config-client-test.yaml   ----> config.info
缺少 bootstrap.yml 报错如下
NacosConfigProperties      : create config service error!properties=NacosConfigProperties{serverAddr='null', encode='null', group='DEFAULT_GROUP', prefix='null', fileExtension='properties', timeout=3000, endpoint='null', namespace='null', accessKey='null', secretKey='null', contextPath='null', clusterName='null', name='null', sharedDataids='null', refreshableDataids='null', extConfig=null},e=,
3.启动类
加注解 @EnableDiscoveryClient 激活nacos
@SpringBootApplication
@EnableDiscoveryClient
public class OrderMain {
    public static void main(String[] args) {
        SpringApplication.run(OrderMain.class,args);
    }
}
4.业务类
@RefreshScope 支持动态刷新
@RestController
@RefreshScope
@RequestMapping("/consumer")
public class ConfigController {
    @Value("${config.info}")
    private String configInfo;
    @GetMapping("/config/info")
    public String getConfigInfo() {
        return configInfo;
    }
}
 
                    
                     
                    
                 
                    
                
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号