当前位置:网站首页>[microservices | Nacos] quickly realize the configuration center function of Nacos, and complete configuration update and version iteration

[microservices | Nacos] quickly realize the configuration center function of Nacos, and complete configuration update and version iteration

2022-06-22 03:04:00 Bulst


Take on the last article , Let's continue our study nacos Another function of , Configuration center .

The new configuration

stay Nacos Spring Cloud in ,dataId The complete format is as follows :

${prefix}-${spring.profiles.active}.${file-extension}

  • prefix The default is spring.application.name Value , You can also configure spring.cloud.nacos.config.prefix To configure the .
  • spring.profiles.active That is, the current environment corresponds to profile, For details, please refer to Spring Boot file . Be careful : When spring.profiles.active It's empty time , Corresponding connector - There will be no ,dataId The splicing format of becomes ${prefix}.${file-extension}
  • file-exetension To configure the data format of the content , You can use the configuration item spring.cloud.nacos.config.file-extension To configure the . Currently only supported properties and yaml type .
     Insert picture description here

To configure @RefreshScope

adopt Spring Cloud Primary annotation @RefreshScope Implement automatic configuration update :

@RestController
@RequestMapping("/config")
@RefreshScope
public class ConfigController {
    

    @Value("${useLocalCache:false}")
    private boolean useLocalCache;

    @RequestMapping("/get")
    public boolean get() {
    
        return useLocalCache;
    }
}

Test code

Main code

@RefreshScope
@RequestMapping(value = "/producer")
@RestController
public class ProducerController {
    

    @Value(value = "${server.port}")
    private String port;

    @Value("${ossa.info}")
    private String config;

    @GetMapping("/{id}")
// @SentinelResource(value = "producerById", fallback = "error")
    public ResponseEntity<ProducerVO> producerById(@PathVariable(value = "id") String id) {
    

        ProducerVO producerVO = new ProducerVO();
        producerVO.setId(id);
        producerVO.setPort(port + ": " + UUID.randomUUID().toString());
        producerVO.setConfigBody(config);

        return ResponseEntity.ok(producerVO);
    }
spring:
  application:
    name: ossa-service-producer
  cloud:
    nacos:
      # docker start-up nacos:
      # docker run --name mynacos -itd -e MODE=standalone -p 8848:8848 -p 9848:9848 -p 9849:9849 nacos/nacos-server:v2.0.3
      discovery:
        server-addr: ip:8848
        namespace: 78e10147-ff76-4e56-8b19-2e2bbd39f750
        group: OSSA_GROUP
      config:
        server-addr: ip:8848
        file-extension: yml
        refresh-enabled: true
        group: OSSA_GROUP
        namespace: 78e10147-ff76-4e56-8b19-2e2bbd39f750

To configure nacos

 Insert picture description here

Publish new configuration

 Insert picture description here

 Insert picture description here

test

 Insert picture description here

原网站

版权声明
本文为[Bulst]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/172/202206211649388341.html