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.
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
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.
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!