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

How to connect to Nacos in SpringBoot project

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

Share

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

Editor to share with you how to access Nacos in the SpringBoot project, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's learn about it!

Preface

Instead of using the SpringBoot integration approach provided officially by nacos, the project uses Alibaba Spring Cloud's dependency package for integration. The reason is that in the SpringBoot integration method provided by the official website, the use of configuration center and service discovery feature at the same time will make some attribute conflicts in the configuration of service discovery feature invalid. The most direct thing is that the configuration center and the service discovery feature cannot configure two different namespace. By default, the namespace configured in the configuration center will be selected as the namespace for service discovery.

Another point is that it is well compatible with Spring annotations without the need for additional comments. For example, the @ NacosValue annotation.

Maven relies on com.alibaba.cloud spring-cloud-starter-alibaba-nacos-config 2.2.5.RELEASE com.alibaba.cloud spring-cloud-starter-alibaba-nacos-config-server 2.2.5.RELEASE org.apache.dubbo dubbo-spring-boot-starter 2.7.9Nacos configuration Center

Configuration file

Create a new bootstrap.properties file and configure the configuration information for nacos in the configuration file.

# = nacos-config = # spring.cloud.nacos.config.server-addr=127.0.0.1:8848spring.cloud.nacos.config.username=nacosspring.cloud.nacos.config.password=nacosspring.cloud.nacos.config.namespace=config-devspring.cloud.nacos.config.group=demeter

Note: due to the loading mechanism of spring cloud alibaba nacos config, the configuration information cannot be parsed if it is written in the configuration file of application- {env} .properties. So if you need to load dynamically in multiple environments, you can write it in the bootstrap- {env} .properties file. Configure the active profiles+ environment at startup to read the configuration correctly.

Dynamic configuration

New configuration

Create a new configuration demeter-application-admin in the namespace named config-dev.

Configuration of the data-id will not say any more, this can view the information, there are a lot of information on the Internet are described in detail. The reference for data-id is as follows:

Demeter-application-admin

Demeter-application-admin.properties

Demeter-application-admin-dev.properties

Configure entity classes

DemeterAdminProperties.class

@ Data@Accessors (chain = true) @ ConfigurationProperties (prefix = "demeter.admin") public class DemeterAdminProperties implements Serializable {private static final long serialVersionUID = 1563821704172888362L; private String name; private String description; private String time;}

Make the configuration effective. The @ EnableConfigurationProperties annotation enables DemeterAdminProperties to be instantiated and automatically injected into other components.

Configuration@ComponentScan (value = {"org.demeter.application.admin",}) @ Import (value = {AutoRedisConfiguration.class}) @ EnableConfigurationProperties ({DemeterAdminProperties.class}) public class AdminWebConfig {}

Access result

Controller

Nacos service discovery

Configuration file # = nacos-discovery = # spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848spring.cloud.nacos.discovery.username=nacosspring.cloud.nacos.discovery.password=nacosspring.cloud.nacos.discovery.namespace=discovery-devspring.cloud.nacos.discovery.group=demeter to make the configuration effective

Use the @ EnableDiscoveryClient annotation to make the configuration file effective.

The above is all the contents of the article "how to access Nacos in the SpringBoot Project". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

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