您的当前位置:首页正文

Springboot 整合nacos

2024-11-11 来源:个人技术集锦

nacos 既可以做配置中心也可以做注册中心,用户可以自行选择是否开启注册中心

1.自行安装nacos

服务端 nacos-server-1.4.1.jar。版本自行选择,可能会有区别。

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Finchley.SR1</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>2.1.0.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>2.5</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.5</version>
        </dependency>

        <dependency>
            <groupId>com.alibaba.boot</groupId>
            <artifactId>nacos-config-spring-boot-starter</artifactId>
            <version>0.2.7</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba.boot</groupId>
            <artifactId>nacos-discovery-spring-boot-starter</artifactId>
            <version>0.2.7</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
        <dependency>
            <groupId>io.github.openfeign</groupId>
            <artifactId>feign-core</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-openfeign-core</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
        </dependency>
    </dependencies>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.0.RELEASE</version>
        <relativePath/>
    </parent>
    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

2.配置文件

nacos.config.server-addr=127.0.0.1:8848
nacos.discovery.server-addr=127.0.0.1:8848
nacos.discovery.autoRegister=true
nacos.discovery.username=nacos
nacos.discovery.password=nacos
nacos.discovery.namespace=public
spring.application.name=simple

3. 展示配置中心

import com.alibaba.nacos.api.config.annotation.NacosValue;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("config")
public class ConfigController {

    @NacosValue(value = "${nacos.custom.tag}", autoRefreshed = true)
    private String useLocalCache;

    @RequestMapping(value = "/get", method = RequestMethod.GET)
    public String get() {
        return useLocalCache;
    }
}

4.注册中心

默认该版本的自动注册为关闭状态,需要主动开启。

@ConfigurationProperties(NacosDiscoveryConstants.PREFIX)
public class NacosDiscoveryProperties {
	private String serverAddr = "127.0.0.1:8848";
	private String contextPath;
	private String clusterName;
	private String endpoint;
	private String namespace;
	private String accessKey;
	private String secretKey;
	private boolean autoRegister = false;
	@NestedConfigurationProperty
	private Register register = new Register();
	private String username;
	private String password;

5.服务调用

Top