02--创建一个工程并将其注册至nacos,并使用nacos配置
nacos 环境搭建请参考
- 新建一maven项目spring-cloud-alibaba
- 删除 spring-cloud-alibaba 项目中的 src文件
pom.xml中引入springboot及springcloudalibab依赖
<?xml version=”1.0” encoding=”UTF-8”?>
<modelVersion>4.0.0</modelVersion>
<groupId>spring-cloud-alibaba</groupId>
<artifactId>spring-cloud-alibaba</artifactId>
<version>1.0-SNAPSHOT</version>
<name>spring-cloud-alibaba</name>
<url>http://www.xxx.xxx</url>
<description>spring-cloud-alibaba</description>
<properties>
<java.version>1.8</java.version>
<spring.cloud.version>Greenwich.SR6</spring.cloud.version>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.7.RELEASE</version>
</parent>
<!-- 依赖声明 -->
<dependencyManagement>
<dependencies>
<!-- SpringCloud 微服务 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring.cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- SpringCloud Alibaba 微服务 -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2.1.0.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.2</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
- 在spring-cloud-alibaba项目中新建producer-example模块
producer-example 模块中引入nacos开发包
<?xml version=”1.0” encoding=”UTF-8”?>
spring-cloud-alibaba
spring-cloud-alibaba
1.0-SNAPSHOT
4.0.0
producer-example
org.springframework.boot
spring-boot-starter-web
com.alibaba.cloud
spring-cloud-starter-alibaba-nacos-discovery
com.alibaba.cloud
spring-cloud-starter-alibaba-nacos-config
org.springframework.boot
spring-boot-maven-plugin
com.cloud.alibaba.ProducerApplication
producer-example模块中 java目录下新建开发目录 com.cloud.alibaba 并新建ProducerApplication启动类
package com.cloud.alibaba;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;/* @author 刘志强 @date 2020/11/17 09:29 @EnableDiscoveryClient 启动客户端 */
@EnableDiscoveryClient
@SpringBootApplication
@Slf4j
public class ProducerApplication implements CommandLineRunner {
public static void main(String[] args) {
SpringApplication.run(ProducerApplication.class, args);
}
@Override
public void run(String... args) {
log.info("producer已启动");
}
}
- resources 目录下新建 bootstrap.yml 文件。
- bootstrap.yml和application.yml的区别。Bootstrap属性的优先级高,因此默认情况下不能被本地配置覆盖。
bootstrap.yml
spring:
application:name: producer-example
profiles:
active: dev
main:
allow-bean-definition-overriding: true
cloud:
nacos:
discovery:
# 服务注册地址
server-addr: 101.37.152.195:8848
config:
server-addr: 101.37.152.195:8848
# 配置文件格式
file-extension: yml
# 共享配置
shared-dataids: application-${ spring.profiles.active}.${ spring.cloud.nacos.config.file-extension}
服务启动 producer-example 服务会注册进nacos, 并读取nacos配置中的 producer-example-dev.yml,producer-example.yml 以及 application-dev.yml配置信息
nacos配置如下
Data ID: producer-example-dev.yml
Group:DEFAULT_GROUP
配置格式:YAML
配置内容:
author:userName: 刘志强
mailbox: 2425358736@qq.com
Data ID: producer-example.yml
Group:DEFAULT_GROUP
配置格式:YAML
配置内容:
server:
port: 10000
Data ID: application-dev.yml
Group:DEFAULT_GROUP
配置格式:YAML
配置内容:
author:
weChat: Liu19940528
修改启动类 ProducerApplication,打印配置信息
package com.cloud.alibaba;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.core.env.Environment;/* @author 刘志强 @date 2020/11/17 09:29 @EnableDiscoveryClient 启动客户端 */
@EnableDiscoveryClient
@SpringBootApplication
@Slf4j
public class ProducerApplication implements CommandLineRunner {@Autowired
private Environment env;
public static void main(String[] args) {
SpringApplication.run(ProducerApplication.class, args);
}
@Override
public void run(String... args) {
log.info("producer已启动");
log.info(env.getProperty("author.userName"));
log.info(env.getProperty("author.mailbox"));
log.info(env.getProperty("author.weChat"));
}
}
启动项目 观察启动端口和打印信息是否来自nacos配置
- 观察nacos服务列表是否已将producer-example 注册
- springcloud 默认支持热更新spring.cloud.refresh.enabled=true
@RefreshScope 注解 刷新作用域。 新增Author类
package com.cloud.alibaba.domain;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Component;/* @author 刘志强 @date 2020/11/17 13:39 /
@RefreshScope
@Component
public class Author {@Value("${author.userName}")
private String userName;
@Value("${author.mailbox}")
private String mailbox;
@Value("${author.weChat}")
private String weChat;
@Override
public String toString() {
return "Author{" +
"userName='" + userName + '\'' +
", mailbox='" + mailbox + '\'' +
", weChat='" + weChat + '\'' +
'}';
}
}
修改启动类
package com.cloud.alibaba;
import com.cloud.alibaba.domain.Author;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;/* @author 刘志强 @date 2020/11/17 09:29 @EnableDiscoveryClient 启动客户端 */
@EnableDiscoveryClient
@SpringBootApplication
@Slf4j
@RestController
public class ProducerApplication implements CommandLineRunner {@Autowired
private Author author;
public static void main(String[] args) {
SpringApplication.run(ProducerApplication.class, args);
}
@Override
public void run(String... args) {
log.info("producer已启动");
log.info(author.toString());
}
@GetMapping("/test")
public String test() {
return author.toString();
}
}
启动后 修改配置 在访问test接口观察返回值
还没有评论,来说两句吧...