springcloud integrates nacos configuration center

Why do microservices need a configuration center

1. In the microservice architecture, the number and complexity of services will increase significantly, so a configuration center is needed to centrally manage and configure these different microservices.

2. The configuration center can eliminate the problems caused by hard coding, realize multi-environment configuration management, avoid the risks caused by manual modification of configuration files, and support dynamic expansion and deployment of services.

3. Through the configuration center, administrators can easily monitor and manage the configuration of microservices, update service configurations in time, and reduce operation and maintenance costs and risks.

4. The configuration center can also provide version management functions to ensure the consistency and visibility of the system configuration, and ensure the stability and performance of the microservice architecture.

How to use the configuration center

1. This example environment

JDK: 17

SpringBoot: 3.0.6

spring-cloud-starter-alibaba-nacos-config: 2021.0.4.0

nacos: 2.2.0-BATA

2. nacos installation

Refer to my previous article: Nacos startup and configuration

3. nacos configuration

2.1 Create a new namespace

image-20230606232920729

2.2 New configuration

image-20230606233129828

image-20230606233549054

4.pom.xml

<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>3.0.6</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.springcloud.nacos.config</groupId>
	<artifactId>springcloud-nacos-config-demo</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>springcloud-nacos-config-demo</name>
	<description>Demo project for Spring Cloud</description>

	<properties>
		<java.version>17</java.version>
		<maven.compiler.source>17</maven.compiler.source>
		<maven.compiler.target>17</maven.compiler.target>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-actuator</artifactId>
		</dependency>

		<dependency>
			<groupId>com.alibaba.cloud</groupId>
			<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
			<version>2021.0.4.0</version>
		</dependency>
	</dependencies>

5.application.yml

spring:
  cloud:
    nacos:
      config:
        server-addr: 127.0.0.1:8848
        group: DEFAULT_GROUP
        #指定文件名,没有则默认${spring.application.name}
        #指定文件后缀
        file-extension: yaml
        namespace: dca4fe79-6c62-44df-aa7f-12f58de7e05c #这里是nacos的命名空间的id
  config:
    import:
      - optional:nacos:${spring.application.name}.${spring.cloud.nacos.config.file-extension}

  application:
    name: nacos.cfg.test

server:
  port: 8080

6.NacosConfigController

@Controller
@RequestMapping("config")
public class NacosConfigController {

    //通过 Nacos 的 @NacosValue 注解设置属性值
    @Value(value = "${nacos.test.properties:null}")
    private String testProperties;

    /**
     * 获取配置
     * @return
     */
    @RequestMapping(value = "/get", method = RequestMethod.GET)
    @ResponseBody
    public String get() {
        return testProperties;
    }
}

7. Start to test the results with the http tool

image-20230606233847071

The entire example above is complete

If you need the complete source code, please pay attention to the official account "Architecture Hall", and reply to "springcloud+nacos configuration center" to get

write at the end

If you are interested in related articles, you can pay attention to the official account "Architecture Hall", and will continue to update AIGC, java basic interview questions, netty, spring boot, spring cloud and other series of articles, and a series of dry goods will be delivered at any time!

Guess you like

Origin blog.csdn.net/jinxinxin1314/article/details/131078356