赞
踩
因为 SpirngBoot, SpringCloud 的各个版本之间差异还是挺大的, 所以在参照本博客进行学习时, 有可能出现因为版本不一致, 而出现不同的问题。
如果可以和本项目使用的环境保持一致, 即使不一致, 也尽可能不要跨大版本。
环境清单
框架 | 版本 |
---|---|
JDK | 1.8 |
Spring Boot | 2.1.4.RELEASE |
Spring Cloud | Greenwich.SR1 |
<?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> <!-- 手动引入 spring boot 版本依赖--> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.4.RELEASE</version> <relativePath/> </parent> <groupId>lcn29.github.io</groupId> <artifactId>spring-cloud-eureka</artifactId> <packaging>pom</packaging> <version>1.0-SNAPSHOT</version> <dependencyManagement> <dependencies> <!-- 手动引入 spring cloud 对应的配置 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Greenwich.SR1</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <!-- 手动 引入 spring cloud 的基础模块--> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter</artifactId> </dependency> </dependencies> <!-- 省略一些打包相关的配置 --> </project>
eureka-server
<dependencies>
<!-- eureka 服务端的依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
@EnableEurekaServer
@EnableEurekaServer
@SpringBootApplication
public class EurekaServe {
public static void main(String[] args) {
SpringApplication.run(EurekaServe.class, args);
}
}
在 src/main/resources 里面新建 3 个配置文件, 分别是 application.yml
, application-8081.yml
, application-8081.yml
在 application.yml
里面加上这 2 个配置
# eureka 服务端的通用配置
spring:
application:
name: server-eureka
eureka:
instance:
instance-id: ${spring.application.name}:${server.port}
application-8081.yml
里面加上这 2 个配置# eureka 服务端 1 的配置
server:
port: 8081
eureka:
instance:
hostname: eureka-8081.com
client:
service-url:
defaultZone: http://eureka-8082.com:8082/eureka/
application-8082.yml
里面加上这 2 个配置# eureka 服务端 1 的配置
server:
port: 8082
eureka:
instance:
hostname: eureka-8082.com
client:
service-url:
defaultZone: http://eureka-8081.com:8081/eureka/
127.0.0.1 eureka-8081.com
127.0.0.1 eureka-8082.com
- Idea 找到右上角的这个
- 点一下左边的 +, 找到 SpringBoot 项, 配置一下下面的三项
(如果 Program arguments 没有的话, 可以在绿色区域搜索添加)
- Program arguments 配置的内容为
--spring.profiles.active=8081
, 作用是启动这个 SpringBoot 应用时使用8081
环境, 也就是对应application-8081.yml
里面的配置
- 同理, 在配置多一条
8082
环境的启动命令, 这样子模块就能启动 2 个程序了
在父模块里面新建一个子模块, 模块名 config-server
在模块里面添加如下依赖
<!-- eureka 服务端的依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!-- web 功能的支持, 没有这个 客户端就会启动完就结束程序 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
@EnableEurekaClient
@EnableEurekaClient
@SpringBootApplication
public class ConfigServer {
public static void main(String[] args){
SpringApplication.run(EurekaClientOne.class, args);
}
}
同样在 src/main/resources 里面新建 1 个配置文件, application.yml
在 application.yml
里面加上这 2 个配置
spring:
application:
name: spring-cloud-config-server
server:
port: 9091
eureka:
instance:
instance-id: ${spring.application.name}:${server.port}
client:
service-url:
# 向服务端的注册地址
defaultZone: http://eureka-8081.com:8081/eureka/,http://eureka-8082.com:8082/eureka
config-client
, 作为服务的调用方<!-- eureka 服务端的依赖--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <!-- web 功能的支持, 没有这个 客户端就会启动完就结束程序 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--远程调用--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency>
@EnableEurekaClient
@EnableFeignClients
@EnableEurekaClient
@SpringBootApplication
public class ClientConsumer {
public static void main(String[] args){
SpringApplication.run(ClientConsumer.class, args);
}
}
application.yml
中添加配置spring:
application:
name: client-consumer
server:
port: 10101
eureka:
instance:
instance-id: ${spring.application.name}:${server.port}
client:
service-url:
# 向服务端的注册地址
defaultZone: http://eureka-8081.com:8081/eureka/,http://eureka-8082.com:8082/eureka
@RestController
public class PropertyController {
private String name = "local";
@GetMapping("/property")
public String getProperty() {
String resp = "从远程获取到的配置" + name;
return resp;
}
}
spring-cloud-config
, 名字随意application-config
, 里面新建了三个文件 config-dev.yml
, config-test.yml
, config-prod.yml
# config-dev.yml 配置, config-test.yml, config-prod.yml 配置类似, 改为 my.name 修改为 test/prod 即可
my:
name: dev
到此, 环境准备基本完成了。
config-center
引入对应的依赖<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
application.yml
中添加配置spring:
application:
name: spring-cloud-config-server
cloud:
config:
server:
git:
uri: 你GitHub配置仓库的地址
# 指定某个目录
search-paths: 配置仓库里面哪个文件夹存放着你这个项目需要的配置文件(这里我的就是 /application-config)
username: GitHub的用户名
password: GitHub的登录密码
@EnableConfigServer
@EnableEurekaClient
@EnableConfigServer
@SpringBootApplication
public class ConfigServer {
public static void main(String[] args) {
SpringApplication.run(ConfigServer.class, args);
}
}
{
name: "config", profiles: [
"dev"
], label: "master", version: "dcc1f4eb6c3f44c59b28d3699edf39ac4da0e97b", state: null, propertySources: [
{
name: "https://github.com/GitHub用户名/你的配置仓库名/配置文件夹名/配置文件名.yml", source: {
my.name: "dev-update-v1.2"
}, }
], }
config-client
引入对应的依赖<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
bootstrap.yml
的配置文件 (启动配置文件, 他的优先级高于 application.yml), 在里面配置eureka: instance: instance-id: ${spring.application.name}:${server.port} client: service-url: # 向服务端的注册地址 defaultZone: http://eureka-8081.com:8081/eureka/,http://eureka-8082.com:8082/eureka spring: cloud: config: # 下面三个参数对应登录我们服务端获取配置信息的 url 的三个参数 name: config profile: dev label: master discovery: # Config 服务发现支持 enabled: true # config server 的应用名 serviceId: spring-cloud-config-server
application.yml
设置我们客户端的其他信息的, 如端口, 应用名server:
port: 9091
spring:
application:
name: spring-cloud-config-client
@RestController
public class PropertyController {
@Value("${my.name}")
private String name;
@GetMapping("/property")
public String getProperty() {
String resp = "从远程获取到的配置" + name;
return resp;
}
}
启动客户端, 访问 http://localhost:你的端口/property
可以看到 从远程获取到的配置 dev
这时候你修改了 GitHub 上面 config-dev.yml 的内容, 你会发现你的客户端没法获取到最新的配置, SpringCloud 提供了一个 POST
方法, 请求一下 /refresh
这个接口就行了。但是需要做一下配置
在测试客户端引入 actuator
依赖, 他里面有我们需要的的 /refresh
的实现
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
@RefreshScope
, 我们这里就是在 Controller 上面加上就行了@RestController
public class PropertyController {
// 省略
}
actuator
默认的 refresh
端点是关闭的, 需要开启 (配置在 application.yml
就行了)# 暴露所有的端点 management.security.enabled=false 在 spring boot 2.0 版本以上已过期
management:
endpoints:
web:
exposure:
include: '*'
当我们的远程仓库的配置继续了修改, 只需要我们手动发起一个 Post 请求到 http://你客户端的地址:端口/actuator/refresh
, 你的项目里面的配置就会进行刷新
上面的主动请求 /refresh
, 可以通过 GitHub 的 Webhooks 进行回调。但是随着系统的客户端的增加, Webhooks 的 url 也会增大, 不好管理。下面看一下, 通过消息总线的方式的解决
流程大概是这样的
- 当我们代码提交了, 通过GitHub的Webhook功能回调我们的服务端(单机集群都可以), 地址 http://你的服务端的地址:端口/actuator/bus-refresh
- 这时候服务端通知消息总线 (也就是 MQ), 消息总线通知所有的客户端
- 客户端主动去请求服务端获取最新的配置
首先搭建一套 MQ 环境, 这里使用的是 RabbitMQ, 你可以使用其他的, 但是需要修改配置
在配置客户端 config-center
引入对应的依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-bus</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
config-center
的 `application.yml`` 配置文件里面加上配置spring: cloud: bus: # 开启总线 enabled: true trace: # 追踪链路 enabled: true rabbitmq: host: RabbitMQ 的地址 port: RabbitMQ 的端口 username: RabbitMQ 用户名 password: RabbitMQ 密码 # 暴露端点 management: endpoints: web: exposure: include: bus-refresh
config-client
移除 actuator
的依赖和配置(这些我们配置在服务端), 同时加上依赖<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-bus</artifactId>
</dependency>
spring:
cloud:
bus:
# 开启总线
enabled: true
trace:
# 追踪链路
enabled: true
rabbitmq:
host: RabbitMQ 的地址
port: RabbitMQ 的端口
username: RabbitMQ 用户名
password: RabbitMQ 密码
在 GitHub 的 webhook 配置上我们的配置服务端的请求地址 http://localhost:9091/actuator/bus-refresh
(这个地址, Github 是无论都访问不到的, 如果是正式环境需要正确的配置), 在这里我们通过模拟发送 Post 请求到这个地址, 达到测试的效果
刷新页面, 可以看到你修改并提交到 GitHub 的内容都能不用重启就取到了
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。