springboot 之 nacos实现服务注册与发现,动态配置及实时更新配置

环境

nacos版本: NACOS1.4.0
具体怎么安装nacos,百度一下即可。

依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>nacos</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>nacos</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
        <spring-cloud-alibaba.version>2.2.5.RELEASE</spring-cloud-alibaba.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencyManagement>
        <dependencies>
            <!--阿里巴巴cloud-->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Hoxton.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>${spring-cloud-alibaba.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

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

        <!-- 启用nacos配置 -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
            <version>${spring-cloud-alibaba.version}</version>
        </dependency>
        <!-- nacos服务注册与发现 -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
            <version>${spring-cloud-alibaba.version}</version>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

本地配置

spring:
  application:
    # 应用名称
    name: nacos-test
  cloud:
    nacos:
      config:
        # 设置配置中心服务端地址
        server-addr: 192.168.1.20:8848
        file-extension: yaml
        # 文件前缀
        prefix: test
        # 命名空间ID
        namespace: b53ece8f-3e05-473a-b141-e8d066ef3998

      discovery:
        # nacos注册中心地址
        server-addr: 192.168.1.20:8848
        namespace: b53ece8f-3e05-473a-b141-e8d066ef3998
        #register-enabled: false  只从注册中心获取服务而不将自己注册
#开启包日志
logging:
  level:
    com.example.nacos: debug
#设置监听端口
server:
  port: 8001

启动类

@SpringBootApplication
//开启服务发现
@EnableDiscoveryClient
public class NacosApplication {

    public static void main(String[] args) {
        SpringApplication.run(NacosApplication.class, args);
    }

}

相关类

package com.example.nacos.config;

import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.Configuration;

import java.io.Serializable;

@Data
@Configuration
//添加这个配置就可以实时更新nacos的配置参数
@RefreshScope
public class CommonParameter implements Serializable {
    @Value("${common.name}")
    private String name;
    @Value("${common.age}")
    private Integer age;
    @Value("${common.address}")
    private String address;
}

测试类

package com.example.nacos.controller;

import com.example.nacos.config.CommonParameter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestParameter {
    @Autowired
    private CommonParameter commonParameter;

    @GetMapping("test")
    public String test(){
        String str = "name:"+commonParameter.getName()
                            + " age:"+commonParameter.getAge()
                            + " address:"+commonParameter.getAddress();
        return  str;
    }
}

nacos配置

common: 
    name: zhangsan
    age: 444
    address: street 555

在这里插入图片描述

测试

1.启动服务,查看nacos服务注册
在这里插入图片描述
2.使用浏览器测试
在这里插入图片描述
3.修改nacos配置,重新访问查看结果
在这里插入图片描述
查看服务器日志,看到配置信息已经被拉取下来。
浏览器访问:
在这里插入图片描述
ok!至此,nacos的服务器注册与发现,动态配置及实时更新配置已经完成。
over!





前人少挖坑,后人少踩坑;前人记录,后人好踩坑;前人多种树,后人好乘凉

相关推荐
©️2020 CSDN 皮肤主题: 技术黑板 设计师:CSDN官方博客 返回首页