项目地址:https://github.com/lijianleiGit/SpringBoot-nacos

1、环境准备

名称版本
Java1.8
springBoot2.1.9.RELEASE
springCloudGreenwich.SR3
Nacos1.1.4

2、创建maven项目

1、父项目pom.xml配置

  <parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.1.9.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><properties><java.version>1.8</java.version><spring-cloud.version>Greenwich.SR3</spring-cloud.version>
</properties><dependencies><dependency><groupId> org.springframework.boot </groupId><artifactId> spring-boot-configuration-processor </artifactId><optional> true </optional></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></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><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>${spring-cloud.version}</version><type>pom</type><scope>import</scope></dependency></dependencies>
</dependencyManagement><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins>
</build>

2、子模块配置

1)pom.xml配置

 <dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId><version>0.2.2.RELEASE</version><type>pom</type></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId><version>0.2.2.RELEASE</version></dependency>
</dependencies>

2)resources下的文件配置

在子模块resources中添加bootstrap.yml文件 配置如下:

server:port: 8081
spring:application:name: nacosDemo_1cloud:nacos:##使用nacos的配置中心config:server-addr: localhost:8848#nacos config server 动态配置文件格式file-extension: yaml#nacos 配置中心功能,默认trueenabled: true

配置参数理解

添加application.properties文件,配置如下

comtest.test1=0

3)创建一个动态参数接收类

package com.blaineli.entity;import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Component;/*** @author :* @date :Created in 2019/10/29 9:58* @description:* @modified By:* @version:*/
@ConfigurationProperties("comtest")
@RefreshScope
@Data
@Component
public class TestBean {private Integer test1;
}

4)对外接口controller配置

package com.blaineli.contrller;import com.blaineli.entity.TestBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;/*** @author :* @date :Created in 2019/10/29 10:03* @description:* @modified By:* @version:*/
@RestController
@RequestMapping("nacos/")
public class TestController {@Autowiredprivate TestBean testBean;/*** 获取配置判断是否配置成功* @return*/@RequestMapping("getConfig")public Object getConfig() {return testBean.getTest1();}}

5)在没有对子模块配置参数时访问如下

6)、创建对该模块的参数配置

配置如下
Data ID名称与应模块的服务名一致

点击发布后,项目日志会出现如下变化

此时在访问接口