Home  >  Article  >  Java  >  How SpringBoot integrates Nacos to implement registration center and configuration center

How SpringBoot integrates Nacos to implement registration center and configuration center

PHPz
PHPzforward
2023-05-12 11:40:221837browse

SpringBoot integrates Nacos

Introducing Maven dependencies

First of all, we still have to introduce Maven dependencies

<!--注册中心的依赖-->
<dependency>
    <groupId>com.alibaba.boot</groupId>
    <artifactId>nacos-discovery-spring-boot-starter</artifactId>
    <version>0.2.3</version>
</dependency>
<!-- 配置中心的依赖 -->
<dependency>
    <groupId>com.alibaba.boot</groupId>
    <artifactId>nacos-config-spring-boot-starter</artifactId>
    <version>0.2.3</version>
</dependency>

There is one thing to note here: the registration center and the configuration center The dependent version should be selected according to the SpringBoot version. Version 0.2.x.RELEASE corresponds to Spring Boot 2.x version, and version 0.1.x.RELEASE corresponds to Spring Boot 1.x version. The SpringBoot version I am using here is 2.2.4.RELEASE, so I chose the 0.2.3 version of the registration center and configuration center.

Add configuration

The next step is to add relevant configuration in application.yml????

server:
port: 80
servlet:
context-path: /
spring:
application:
name: NacosDemo
nacos:
config:
server-addr: 127.0.0.1:8848
discovery:
server-addr: 127.0.0.1:8848

Sample code

First we need to add two Nacos annotations to the project startup class? ???

import com.alibaba.nacos.spring.context.annotation.config.NacosPropertySource;
import com.alibaba.nacos.spring.context.annotation.discovery.EnableNacosDiscovery;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@EnableNacosDiscovery //注册中心注解 使用nacos
@NacosPropertySource(dataId = "product_config",autoRefreshed = true) //配置中心注解:autoRefreshed 代表自动刷新注解
public class NacosdemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(NacosdemoApplication.class, args);
    }
}

Next we need to add another Nacos configuration file???

import com.alibaba.nacos.api.annotation.NacosInjected;
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.naming.NamingService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import javax.annotation.PostConstruct;
/**
 * @program: NacosDemo
 * @description: NacosConfig
 **/
@Configuration
public class NacosConfig {
    @Value("${server.port}")
    private int serverPort;
    @Value("${spring.application.name}")
    private String applicationName;
    @NacosInjected
    private NamingService namingService;
    @PostConstruct
    public void registerInstance() throws NacosException {
        namingService.registerInstance(applicationName, "127.0.0.1", serverPort);
    }
}

Finally we write a Controller class that simulates obtaining configuration parameters????

import com.alibaba.nacos.api.config.annotation.NacosValue;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
 * ConfigController 配置控制器
 * @description: ConfigController
 **/
@RestController
@RequestMapping("/test")
public class ConfigController {
    @NacosValue(value = "${productName}",autoRefreshed = true)
    private  String productName;
    @RequestMapping("/productName")
    public String getProductName(){
        return productName;
    }
}

The code has been prepared here. Since we have a Controller that obtains configuration parameters, we must define a configuration parameter to be obtained. We start Nacos, log in to its backend page, find the configuration list in the configuration management on the left, and create a new configuration under the configuration list.

How SpringBoot integrates Nacos to implement registration center and configuration center

How SpringBoot integrates Nacos to implement registration center and configuration center

##❗❗❗Be sure to note ❗❗❗ here: the Data ID value filled in when adding parameters on the Nacos management page It must be consistent with the dataId value in the @NacosPropertySource annotation on the startup class; and when defining the configuration content, the configuration name must be consistent with the name defined in the Controller. No matter which of the two names does not match, an error that the configuration cannot be found will be reported when starting the project.

At this point, the code and configuration have been prepared. Let's start the project to see the specific effects... After the project is started, we find the service list under service management on the left side of the Nacos management page and open the service You can see in the list that our project was successfully registered into Nacos.

How SpringBoot integrates Nacos to implement registration center and configuration center

Next, we visit http://localhost/test/productName in the browser, and we can see that our new configuration has been successfully taken out. If the configuration needs to be changed at this time, we only need to modify the corresponding configuration in the background of Nacos, and then refresh the page to see that the configuration has been dynamically updated

How SpringBoot integrates Nacos to implement registration center and configuration center

How SpringBoot integrates Nacos to implement registration center and configuration center

The above is the detailed content of How SpringBoot integrates Nacos to implement registration center and configuration center. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete