- 新建一maven项目spring-cloud-alibaba
- 删除 spring-cloud-alibaba 项目中的 src文件
- pom.xml中引入springboot及springcloudalibab依赖
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<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>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring.cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<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"?>
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>spring-cloud-alibaba</artifactId>
<groupId>spring-cloud-alibaba</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>producer-example</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>com.cloud.alibaba.ProducerApplication</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>
- 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;
@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;
@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;
@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;
@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接口观察返回值