Spring access to nacos configuration center

Table of contents

The first step is to start naocs

The second step is to configure related configurations on nacos

The third step is to write the automatic configuration class of nacos

The fourth step, test


The configuration center in the project needs to be transferred from zookeeper to nacos, and the framework used by the project is springmvc4.3.15

The whole demo of the local environment first, record the integration process

Import dependencies:

<dependency>
     <groupId>com.alibaba.nacos</groupId>
     <artifactId>nacos-spring-context</artifactId>
     <version>1.1.0</version>
</dependency>

The first step is to start naocs

Just start a nacos locally, so I won’t go into details

The second step is to configure related configurations on nacos

The third step is to write the automatic configuration class of nacos

package com.bes.nacos;

import com.alibaba.nacos.api.annotation.NacosProperties;
import com.alibaba.nacos.api.config.ConfigType;
import com.alibaba.nacos.spring.context.annotation.config.EnableNacosConfig;
import com.alibaba.nacos.spring.context.annotation.config.NacosPropertySource;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

/**
 * @Author: cjian
 * @Date: 2023/3/31 10:31
 * @Des:
 */
@Configuration
@PropertySource("classpath:config.properties")
@EnableNacosConfig(globalProperties = @NacosProperties(serverAddr = "${nacos.server-addr}", namespace = "${nacos.namespace}"))
@NacosPropertySource(dataId = "${nacos.data.id}", groupId = "${nacos.group.id}", type = ConfigType.YAML, autoRefreshed = true)
public class NacosConfiguration {

}
config.properties configuration:
# nacos 相关配置
# nacos地址
nacos.server-addr=127.0.0.1:8848
# nacos配置的命名空间
nacos.namespace=WEBGATE
# nacos配置的dataId
nacos.data.id=config.yaml
# nacos配置的groupId
nacos.group.id=webgate_console

The fourth step, test

A total of two points were tested:

package com.bes.nacos;

import com.alibaba.nacos.api.config.annotation.NacosValue;
import com.bes.bean.Project;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import static org.springframework.web.bind.annotation.RequestMethod.GET;

/**
 * @Author: cjian
 * @Date: 2023/3/31 10:32
 * @Des:
 */
@Controller
@RequestMapping("config")
public class ConfigController {

    //debug 使用
    @Autowired
    private ConfigurableApplicationContext applicationContext;

    //1、测试nacos自动的注入配置是否正常
    @NacosValue(value = "${com.cjian.project.name}", autoRefreshed = true)
    private String projectName;

    // 2、测试通过spring xml的配置方式,注入到ioc容器中的类的属性,如果使用EL表达式获取naocs上的配置,是否正常
    @Autowired
    private Project project;

    @RequestMapping(value = "/get", method = GET)
    @ResponseBody
    public String get() {
        return projectName;
    }

    @RequestMapping(value = "/getProjectName", method = GET)
    @ResponseBody
    public String getProjectName() {
        return project.getProjectName();
    }
}

XMl simple configuration injects a Project class whose property is projectName:

<bean id="project" class="com.bes.bean.Project">
        <property name="projectName" value="${com.cjian.project.name2}"></property>
    </bean>

Test 1:

Test 2:

At the end, you can also get the configuration on nacos by using the @Value annotation, but it is not recommended to use it~

Guess you like

Origin blog.csdn.net/cj_eryue/article/details/129885173