Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The method of automatic refresh configuration of SpringBoot integrating Nacos

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article focuses on "SpringBoot integration Nacos automatic refresh configuration method", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn "SpringBoot integration Nacos automatic refresh configuration method" it!

Purpose

Nacos acts as the registry and configuration center for SpringBoot services.

Modify the configuration file in NacosServer and get the modified content without SpringBoot restarting.

This example will configure a cml.age=100 configuration item in the configuration file, write a method to read the configuration file in the program, and provide it to the browser through the Get--- > / test/age interface.

If the age in the configuration file is changed to 200, you do not need to restart the program, but access the / test/age interface directly, and you will get the latest value 200.

If there is no configuration item for age in the configuration file, or if there is no configuration item for cml at all, access to the / test/age interface will return the default value of 18

Environment

SpringCloud:2020.0.3

SpringCloudAlibaba:2021.1

SpringBoot:2.5.2

Pom

Introducing nacos related configuration into pom: discovery,config,bootstrap

Some people on the Internet say that it is necessary to introduce actuator, but it is not necessary. Spring-cloud-starter-oauth3 is also integrated in this example, and there is no problem of SpringSecurity interception at all.

Question: how do NacosServer and NacosClient communicate? If it is called back and forth by http interface, why is it not intercepted by SpringSecurity? Is it rpc?

Org.springframework.cloud spring-cloud-dependencies ${spring.cloud.dependencies} pom import com.alibaba.cloud spring-cloud-alibaba-dependencies ${spring.cloud.alibaba.dependencies} pom Import com.alibaba.cloud spring-cloud-starter-alibaba-nacos-discovery com.alibaba.cloud spring-cloud-starter-alibaba-nacos-config org.springframework.cloud spring-cloud -starter-bootstrap profile

Bootstrap.yml

Server: port: 9556spring: application: name: app profiles: active: test nacos: discovery: username: nacos password: nacos server-addr: 192.168.1.61:8848 config: server-addr: 192.168.1.61:8848 file-extension: yaml

App-dev.yml

This configuration refers to the configuration file app-dev.yml in NacosServer, which only intercepts the cml.age part.

Cml: age: 100Code

RefreshScope note: must be added to the controller, add to the main boot above does not work. Which resources need to automatically refresh the configuration to add this note on top of the controller to encapsulate a BaseController.

@ Value ("${cml.age:18}"): reads the value of the cml.age configuration item in the configuration file and assigns it to the variable age. The default value is 18.

GetAge: get the age API

/ test/age interface needs to be added to Security.permitAll

Question: why do RefreshScope annotations have to be added to controller? Why does it not take effect to add to the main startup class

@ RefreshScope @ Api (tags = "Test-api") @ Slf4j @ RestController @ RequestMapping ("/ test") public class TestController {/ * get the cml.age content in the configuration file. If not, the default is 18 * / @ Value ("${cml.age:18}") private String age. @ ApiOperation (value = "get age-test configuration auto refresh", notes = "get age-test configuration auto refresh") @ GetMapping ("/ age") public String getAge () {return age;}} log

Open nacos-refresh log and print updates of configuration content

Logging: level: com.alibaba.cloud.nacos.refresh: debug

Printed log:

2022-01-28 13 Refresh Nacos config group=DEFAULT_GROUP,dataId=identityQrCodeAdmin-service-cml-test.yaml,configInfo=spring 4314 30.574 [com.alibaba.nacos.client.Worker.longPolling.fixed-192.168.1.61_8848-zjrkm-admin] DEBUG com.alibaba.cloud.nacos.refresh.NacosContextRefresher.innerReceive:136-Refresh Nacos config group=DEFAULT_GROUP,dataId=identityQrCodeAdmin-service-cml-test.yaml,configInfo=spring:

Application:

Name:

test

In the case of not restarting the SpringBoot service, modify the value of the cml.age configuration item in NacosServer many times, and then access the / test/age interface through the browser to find that the latest age value can be obtained each time.

At this point, I believe that everyone on the "SpringBoot integration Nacos automatic refresh configuration method" have a deeper understanding, might as well to the actual operation of it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report