In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article is about how Spring Cloud integrates with the Nacos Configuration Center. Xiaobian thinks it is quite practical, so share it with everyone for reference. Let's follow Xiaobian and have a look.
1. Two configurations are defined in the application.properties file: member.nickname = "Wukong Chat Architecture"
member.age = "18"
Private variables nickname and age are defined in the example controller,@value represents the value @Value("${member.nickname}") from the configuration
private String nickname;
@Value("$member.age")
private Integer age;
Method defined in sample controller: Get values for nick and age @RequestMapping("/test-local-config")
public R testLocalConfig() {
return R.ok().put("nickname", nickname).put("age", age);
}
Test results mark
Summary: Get configuration from configuration file.
What are the disadvantages of this approach? If you want to modify configuration parameters, you need to restart the service. If there are many services, you need to restart all services, which is very inconvenient.
Is there any way to keep changing the service configuration and make it work?
Answer: Yes, it can be done with Spring Cloud Alibaba's Nacos component.
2. Introducing Nacos dependency
PassJava-Common project pom.xml file introduces Spring Cloud Alibaba Nacos Config dependency
com.alibaba.cloud
spring-cloud-starter-alibaba-nacos-config
3. Configure Nacos metadata
javpassa-member Add/src/main/resources/bootstrap.properties configuration file (note: bootstrap.properties takes precedence over other configuration files)
Configure Nacos Config metadata
bootstrap.propertiesspring.application.name=passjava-member
spring.cloud.nacos.config.server-addr=127.0.0.1:8848
4. Nacos background new configuration
Data ID: passjava-member.properties
Group: DEFAULT_GROUP
Configuration format:
member.nick="Wukong"
member.age=10
Nacos background new configuration 5. Enable dynamic refresh configuration
Add comment @RefreshScope Enable dynamic refresh configuration
@RefreshScope
@RestController
@RequestMapping("member/sample")
publicclass SampleController {}
Log messages can be seen from the console:
Refresh keys changed: [member.age]
2020-04-19 23:34:07.154 INFO 8796 --- [-127.0.0.1_8848] c.a.nacos.client.config.impl.CacheData : [fixed-127.0.0.1_8848] [notify-ok] dataId=passjava-member.properties, group=DEFAULT_GROUP, md5=df136e146c83cbf857567e75acb11e2b, listener=com.alibaba.cloud.nacos.refresh.NacosContextRefresher$1@4f49b78b
2020-04-19 23:34:07.154 INFO 8796 --- [-127.0.0.1_8848] c.a.nacos.client.config.impl.CacheData : [fixed-127.0.0.1_8848] [notify-listener] time cost=529ms in ClientWorker, dataId=passjava-member.properties, group=DEFAULT_GROUP, md5=df136e146c83cbf857567e75acb11e2b, listener=com.alibaba.cloud.nacos.refresh.NacosContextRefresher$1@4f49b78b
member.age updated, notified member service, refreshed configuration.
The corresponding configuration id is javpassa-member.properties, grouped as DEFAULT_GROUP.
Listen to com.alibaba.cloud.nacos.refresh. NacosContextRefresh
6. test results
Visit: http://localhost:10000/member/sample/test-local-config
Result: Nickname and age are consistent with Nacos background configuration
Conclusion: The configuration can be modified in real time only by changing the configuration in Nacos background.
Note: Nacos configuration items take precedence over application.propertite.
Test Results 7. Namespaces
We now have 5 microservices, each microservice may use different configurations, so how do different microservices get their own microservice configuration?
We can use namespaces here, and we create a namespace for each microservice.
create namespace
Create namespaces #Create 5 namespaces
passjava-channel
passjava-content
passjava-member
passjava-question
passjava-study
namespace
Create configuration under namespace
We open the Configuration List menu and see that there are five namespaces.
Create configuration under namespace
Select the passjava-channel namespace, and then add a configuration item, the same as the previous steps for adding a configuration, or clone the configuration by cloning the namespace.
clone configuration
Modify the specified namespace
bootstrap.properties Configure namespaces
spring.cloud.nacos.config.namespace=passjava-member
Test whether the configuration works
Modify the configuration content of passjava-member.properties
passjava-member.properties
Restart member services
Access method: /member/sample/test-local-config
Implementation results:
{
"msg": "success",
"code": 0,
"nickname": "\" Wukong member\",
"age": 30
}
Description Get the configuration of the passjava-member namespace
8. Grouping
If we have multiple environments, such as development environment, test environment, and production environment, and the configuration parameters of each environment are different, how should the configuration center be configured?
We can use the grouping function of Configuration Center. Each environment is a subset.
First create a dev environment configuration entry, then clone the configuration into the test and prod environments dev environment
dev, test, prod grouping bootstrap.properties Configure the grouping currently used: prodspring.cloud.nacos.config.group=prod
Test Get Production Environment Configuration
{
"msg": "success",
"code": 0,
"nickname": "\" Wukong-prod\",
"age": 10
}
You can see that the configuration obtained is the prod grouping
9. multiple configuration set
We can disassemble the datasource, mybatis-plus and other configurations in the application.yml file and put them in the configuration center. Group can create 3 sets, dev/test/prod.
1. Configuration Center New datasource.yml Configuration
datasource.yml configuration
2. Configuration Center New mybatis.yml Configuration
mybatis.yml configuration
3. Configuration Center New more.yml Configuration
more.yml configuration
4. Clone dev environment to test and prod environment
mark
5.bootstrap.properties Add nacos configuration, application.yml comment configuration
spring.application.name=passjava-member
spring.cloud.nacos.config.server-addr=127.0.0.1:8848
spring.cloud.nacos.config.namespace=passjava-member
spring.cloud.nacos.config.group=prod
spring.cloud.nacos.config.extension-configs[0].data-id=datasource.yml
spring.cloud.nacos.config.extension-configs[0].group=dev
spring.cloud.nacos.config.extension-configs[0].refresh=true
spring.cloud.nacos.config.extension-configs[1].data-id=mybatis.yml
spring.cloud.nacos.config.extension-configs[1].group=dev
spring.cloud.nacos.config.extension-configs[1].refresh=true
spring.cloud.nacos.config.extension-configs[2].data-id=more.yml
spring.cloud.nacos.config.extension-configs[2].group=dev
spring.cloud.nacos.config.extension-configs[2].refresh=true
6. Test whether the configuration works
Test whether the passjava-member.properties and more.yml configurations take effect
The requested URL/member/sample/test-local-config was not found on this server.
Returns the configured nick and age, and the port is 10000, and the member service is registered to the registry
{
"msg": "success",
"code": 0,
"nickname": "\" Wukong-prod1\",
"age": 22
}
Test whether the datasource.yml and mybatis.yml configurations take effect
The requested URL/member/list.html was not found on this server.
Return database query results
{
"msg": "success",
"code": 0,
"page": {
"totalCount": 0,
"pageSize": 10,
"totalPage": 0,
"currPage": 1,
"list": []j
}
}
This means that the above configuration is in effect.
Thank you for reading! About "SpringCloud how to integrate Nacos configuration center" this article is shared here, I hope the above content can be of some help to everyone, so that everyone can learn more knowledge, if you think the article is good, you can share it to let more people see it!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.