开发者配置中心Spring Cloud Config Server
说明文档
1. 简介
之前每个项目的配置文件都在自己的项目中配置,这样在生产环境中会带来很多麻烦,每改动一个配置可能要修改很多个项目, 被修改的项目可能都要重新打包!这时用Spring Cloud Config统一部署各个微服务的配置(外部配置(名称-值对或等效的YAML内容)提供了基于HTTP资源的API)
2.架构
本文依赖Eureka注册服务中心(对外暴露地址,供调用者寻找需要的服务)
- 原理图
-
架构中的三个核心角色:
-
服务注册中心-development-eureka-server
Eureka的服务端应用,提供服务注册和发现功能。
-
服务提供者-development-config-server
提供服务的应用,可以是SpringBoot应用,也可以是其它任意技术实现,只要对外提供的是Rest风格服务即可。
-
服务消费者-development-client-server
消费应用从注册中心获取服务列表,从而得知每个服务方的信息,知道去哪里调用服务方。
-
3. development-eureka-server:8761服务注册中心
服务发现框架,基于REST的服务。注册中心节点默认时间:Eureka Server发送心跳,默认周期为30秒,如果Eureka Server在多个心跳周期内没有接收到某个节点的心跳,Eureka Server将会从服务注册表中把这个服务节点移除(默认90秒)
- 依赖: pom.xml
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
- 配置:application.yml
server:
port: 8761
eureka:
server:
#enableSelfPreservation: false #是否将自己注册到Eureka Server
instance:
hostname: localhost
client:
register-with-eureka: false #注册自己
fetch-registry: false #是否从Eureka Server获取注册信息
service-url:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ #设置与Eureka Server交互的地址
- Application 启动类
@SpringBootApplication
@EnableEurekaServer #注解表示此服务是一个服务注册中心服务
public class DevelopmentEurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(DevelopmentEurekaServerApplication.class, args);
}
}
- 项目启动,浏览器输入:http://localhost:8761 出现Sping Eureka
4. development-config-server:8762服务提供者(核心)
Config-Server服务提供者提供客户端调用配置参数,本文用JPA+Mysql存取参数配置(添加、修改)参数.
集成Spring Cloud Bus + RabbitMQ 消息总线实现自动刷新(自行安装rabbitMQ,本文不再演示安装)
- 依赖 pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--服务注册Eureka中心Client-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>2.2.3.RELEASE</version>
</dependency>
<!--配置中心Config Server-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
<version>2.2.3.RELEASE</version>
</dependency>
<!--SppringCloud Bus 实现自动刷新(RabbitMQ)-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
<!--连接msql数据库相关jar包-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.21</version>
</dependency>
<!-- springBoot jpa依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- java lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.10</version>
<scope>provided</scope>
</dependency>
- 配置:application.yml
server:
port: 8762
#Spring
spring:
application:
name: development-config-server #指定配置服务名称
profiles:
active: jdbc #设置使用mysql配置(默认Git)
cloud:
bus:
enabled: true #启用springcloud config bus
trace:
enabled: true #开启跟踪总线事件
config:
server:
default-label: masters
jdbc:
sql: SELECT akey , avalue FROM config_server where APPLICATION=? and PROFILE=? and LABEL=?
# MySQL
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/test?serverTimezone=UTC&useUnicode=true&characterEncoding=UTF-8
username: root
password: root
# JPA
jpa:
hibernate:
ddl-auto: update
show-sql: true
#RabbitMQ
rabbitmq:
host: localhost
port: 5672 #
username: guest
password: guest
#bus-refresh 暴露刷新的地址
management:
endpoints:
web:
exposure:
include: bus-refresh
#eureka
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
- 新建数据库表config_server
DROP TABLE IF EXISTS `config_server`;
CREATE TABLE `config_server` (
`id` bigint(20) UNSIGNED ZEROFILL NOT NULL AUTO_INCREMENT,
`akey` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT '',
`application` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT '',
`avalue` varchar(200) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT '',
`label` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT '',
`profile` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT '',
`createTime` datetime(0) NULL DEFAULT NULL,
`updateTime` datetime(0) NULL DEFAULT NULL,
`create_time` datetime(6) NULL DEFAULT NULL,
`update_time` datetime(6) NULL DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 26 CHARACTER SET = utf8 COLLATE = utf8_bin ROW_FORMAT = Dynamic;
--插入测试数据
INSERT INTO `config_server` VALUES (1, 'item_url', 'config-service', 'com.master.com', 'master', 'master', NULL, NULL, NULL, '2020-07-02 18:03:09.000000');
INSERT INTO `config_server` VALUES (2, 'item_url', 'config-service', 'www.test.com', 'test', 'test', NULL, NULL, NULL, NULL);
INSERT INTO `config_server` VALUES (3, 'item_url', 'product-service', 'www.dev.com', 'dev', 'dev', NULL, NULL, NULL, NULL);
INSERT INTO `config_server` VALUES (18, 'urlSaveConfig', 'config-service', 'urlSaveConfig P', 'master', 'master', NULL, NULL, '2020-07-03 18:22:22.000000', NULL);
INSERT INTO `config_server` VALUES (25, 'urlTestaaaaa', 'config-service', 'mastrTesthhhhhh', 'master', 'master', NULL, NULL, NULL, '2020-07-06 15:17:11.000000');
- Application启动类 配置中心服务端已经构建完成
@SpringBootApplication
@EnableDiscoveryClient //注册中心发现
@EnableConfigServer//开启服务对配置中心的支持
public class DevelopmentConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(DevelopmentConfigServerApplication.class, args);
System.out.println("DevelopmentConfigServerApplication启动!");
}
}
5. development-config-client:8763服务消费者(调用)
config-server服务提供者提供客户端调用配置参数,本文用JPA+Mysql存取参数配置(添加、修改)参数
- 依赖 pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
<version>2.0.1.RELEASE</version>
</dependency>
<!--服务注册Eureka中心jar包-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>2.0.1.RELEASE</version>
</dependency>
<!--SppringCloud Bus 实现自动刷新(RabbitMQ)-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
<version>2.0.1.RELEASE</version>
</dependency>
<!-- Spring 程序进行监控,可以通过http接口得到该程序的各种信息-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
- application.yml
server:
port: 8763
spring:
application:
name: development-config-client #mysql de application name获取配置文件的名称
- bootstarp.yml(需新建,优先加载于application.yml)
#Spring
spring:
cloud:
config:
uri: http://localhost:8762
fail-fast: true
profile: master
label: master
name: config-service #mysql de application name获取配置文件的名称
discovery:
service-id: development-config-server #配置中心的service-id,便于扩展为高可用配置集群
enabled: true #开启配置信息发现
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest
#eureka
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/ #设置与Eureka Server交互的地址
- Application启动类
@SpringBootApplication
@EnableDiscoveryClient
public class DevelopmentConfigClientApplication {
public static void main(String[] args) {
SpringApplication.run(DevelopmentConfigClientApplication.class, args);
}
}
- client 测试调用(Controller)
@RefreshScope //刷新机制,必须
@RestController
@RequestMapping("/configController")
public class ConfigController {
//获取的配置参数key,对应数据库akey字段
@Value("${urlTestaaaaa}")
private String avalue;
/**
*
* 输出val
*/
@RequestMapping("/list")
public String list() {
return avalue;
}
}
- postman 测试:http://localhost:8763/configController/list