《 Rest微服务加入Config实现分布式配置中心综合实战 》
前言
在前面已经完成了《 Rest微服务加入Config实现分布式配置中心客户端的配置 》构建工作,并顺利的从 GitHub 上获取到了相应的环境配置内容。接下来将实现一个基于Spring Cloud Config 分布式配置中心的 Spring Cloud Eureka 注册中心 + 服务提供者模块 的一个微服务组合,并将两个微服务的配置内容统一交由 Spring Cloud Config 分布式配置中心服务端模块进行管理,通过该模块来获取相应的配置内容 ,完成不同环境(开发环境、测试环境、发布环境、生产环境等)的轻松切换。涉及的服务模块包括:
- 新增配置中心 Eureka 微服务配置文件,文件名为 “ eureka-client-microservice-config-center.yml ”;
- 新增配置中心 Provider 服务提供者微服务配置文件,文件名为 “ etcp-client-microservice-config-center.yml ”;
- 新构建基于 SpringCloud Config 分布式配置中心的 Eureka 服务注册中心模块,模块名为 “ microservice-config-eureka-7001 ”;
- 新构建基于 SpringCloud Config 分布式配置中心的 Provider 服务提供者模块,模块名为 “ microservice-config-provider-8001 ”;
Config实现分布式配置中心综合实战
1、新增完成 Eureka 及 服务提供者 YML 配置文件的编写,并推送到远程 GitHub 仓库
新增 Eureka 相关的环境配置文件,配置文件名称为 “ eureka-client-microservice-config-center.yml ” ,完整内容如下:
spring:
profiles:
active:
- dev
--- # 开发环境配置
server:
port: 7001 # 该服务注册中心采用7001端口,冒号后面必须要有空格
spring:
profiles: dev
application:
name: eureka-client-microservice-config-center
eureka:
instance:
hostname: www.eurekaconfig7001.com # 冒号后面必须要有空格
client:
register-with-eureka: false # 当前的eureka-server自己不注册进服务列表中
fetch-registry: false # 不通过eureka获取注册信息
service-url:
defaultZone: http://www.eurekaconfig7001.com:7001/eureka/
--- # 测试环境
server:
port: 7001 # 该服务注册中心采用7001端口,冒号后面必须要有空格
spring:
profiles: test
application:
name: eureka-client-microservice-config-center
eureka:
instance:
hostname: www.eurekaconfig7001.com # 冒号后面必须要有空格
client:
register-with-eureka: false # 当前的eureka-server自己不注册进服务列表中
fetch-registry: false # 不通过eureka获取注册信息
service-url:
defaultZone: http://www.eurekaconfig7001.com:7001/eureka/
--- # 生产环境
server:
port: 7001 # 该服务注册中心采用7001端口,冒号后面必须要有空格
spring:
profiles: prod
application:
name: eureka-client-microservice-config-center
eureka:
instance:
hostname: www.eurekaconfig7001.com # 冒号后面必须要有空格
client:
register-with-eureka: false # 当前的eureka-server自己不注册进服务列表中
fetch-registry: false # 不通过eureka获取注册信息
service-url:
defaultZone: http://www.eurekaconfig7001.com:7001/eureka/
新增 Provider 相关的环境配置文件,文件配置名称为 “ etcp-client-microservice-config-center.yml ” ,完整内容如下:
spring:
profiles:
active:
- dev
--- # 开发环境
server:
port: 8001
spring:
profiles: dev
application:
name: etcp-client-microservice-config-center
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: org.gjt.mm.mysql.Driver
url: jdbc:mysql://***.***.***.***:*****/microservice-01
username: *****
password: ************
dbcp2:
min-idle: 5
initial-size: 5
max-total: 5
max-wait-millis: 200
mybatis:
config-location: classpath:mybatis/mybatis.cfg.xml
type-aliases-package: com.huazai.springcloud.entity
mapper-locations:
- classpath:mybatis/mapper/**/*.xml
eureka:
client: # 将客户端注册进eureka服务列表内
service-url:
defaultZone: http://www.eureka7001.com:7001/eureka
instance:
instance-id: microservice-provider-8001
prefer-ip-address: true
info:
app.name: etc-microservice-springcloud
company.name: huazai-studio
build.artifactId: $project.artifactId$
build.version: $project.version$
--- # 测试环境
server:
port: 8001
spring:
profiles: test
application:
name: etcp-client-microservice-config-center
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: org.gjt.mm.mysql.Driver
url: jdbc:mysql://***.***.***.***:*****/microservice-02
username: *****
password: ************
dbcp2:
min-idle: 5
initial-size: 5
max-total: 5
max-wait-millis: 200
mybatis:
config-location: classpath:mybatis/mybatis.cfg.xml
type-aliases-package: com.huazai.springcloud.entity
mapper-locations:
- classpath:mybatis/mapper/**/*.xml
eureka:
client: # 将客户端注册进eureka服务列表内
service-url:
defaultZone: http://www.eureka7001.com:7001/eureka
instance:
instance-id: microservice-provider-8001
prefer-ip-address: true
info:
app.name: etc-microservice-springcloud
company.name: huazai-studio
build.artifactId: $project.artifactId$
build.version: $project.version$
--- # 发布环境
server:
port: 8001
spring:
profiles: prod
application:
name: etcp-client-microservice-config-center
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: org.gjt.mm.mysql.Driver
url: jdbc:mysql://***.***.***.***:*****/microservice-03
username: *****
password: ************
dbcp2:
min-idle: 5
initial-size: 5
max-total: 5
max-wait-millis: 200
mybatis:
config-location: classpath:mybatis/mybatis.cfg.xml
type-aliases-package: com.huazai.springcloud.entity
mapper-locations:
- classpath:mybatis/mapper/**/*.xml
eureka:
client: # 将客户端注册进eureka服务列表内
service-url:
defaultZone: http://www.eureka7001.com:7001/eureka
instance:
instance-id: microservice-provider-8001
prefer-ip-address: true
info:
app.name: etc-microservice-springcloud
company.name: huazai-studio
build.artifactId: $project.artifactId$
build.version: $project.version$
新建完成后,推送到远程 GitHub 仓库中,使用命令
“ git add . ”
“ git commit -a -m " 版本注释 " ”
“ git push origin master ”
如下图:
4、新增 Eureka 注册中心模块,服务名称为 “ microservice-config-eureka-7001 ”
新增 POM 配置文件,完整配置内容如下:
<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>
<parent>
<groupId>com.huazai.springcloud</groupId>
<artifactId>microservice</artifactId>
<version>${project.version}</version>
</parent>
<artifactId>microservice-config-eureka-client-7001</artifactId>
<dependencies>
<!-- SpringCloudConfig配置 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
<!-- 热部署插件 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>springloaded</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
</dependencies>
</project>
新增系统配置文件 “ bootstrap.yml ” ,完整配置内容如下:
spring:
cloud:
config:
name: eureka-client-microservice-config-center # 从 github 上读取的资源名称,注意没有yml后缀名
profile: prod # 配置默认的环境
label: master # 从 master 分支上获取配置信息
uri: http://www.config5001.com:5001 # Spring Cloud Config 获取的服务地址
新增 “ application.yml ” 配置文件,完整配置内容如下:
spring:
application:
name: eureka-client-microservice-config-center
新增主启动类 “ MicroserviceConfigEurekaApp_7001 ” ,并新增注解 “ @EnableEurekaServer ” 开启 Eureka 服务器端的支持,完整内容如下:
package com.huazai.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
/**
*
* @author HuaZai
* @contact [email protected]
* <ul>
* @description
* <li> Eureka Server 服务器端启动类,接受其它微服务注册进来
* </ul>
* @className MicroserviceConfigEurekaClient_7001
* @package com.huazai.springcloud
* @createdTime 2018年05月05日 下午5:52:07
*
* @version V1.0.0
*/
@SpringBootApplication
@EnableEurekaServer
public class MicroserviceConfigEurekaApp_7001
{
public static void main(String[] args)
{
SpringApplication.run(MicroserviceConfigEurekaApp_7001.class, args);
}
}
基于 Spring Cloud Config 分布式配置中心的 Eureka 服务注册中西 构建完成后,项目总览如下图:
测试 Eureka 注册中心,首先启动 Spring Cloud Config 分布式配置中心服务端服务器,再启动基于 Config 的 Eureka 服务器,根据定义的环境,访问 Eureka 服务器,配置正常,会出现如下图:
5、新增基于 Spring Cloud Config 分布式配置中心的提供者服务,参考之前的一号提供者服务器 “ microservice-provider-8001 ” 新建 “ microservice-config-provider-8001 ”。
新增 POM 配置文件,完整内容如下:
<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>
<parent>
<groupId>com.huazai.springcloud</groupId>
<artifactId>microservice</artifactId>
<version>${project.version}</version>
</parent>
<artifactId>microservice-config-provider-8001</artifactId>
<dependencies>
<!-- 引入自己定义的api通用包,可以使用Dept部门Entity -->
<dependency>
<groupId>com.huazai.springcloud</groupId>
<artifactId>microservice-api</artifactId>
<version>${project.version}</version>
</dependency>
<!-- Spring Cloud Config 相关 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- eureka 相关 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<!-- 数据库相关 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
</dependency>
<!-- spring boot 相关 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<!-- 热部署相关 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>springloaded</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
</dependencies>
</project>
系统配置文件 “ bootstrap.yml ” ,完整内容如下:
spring:
cloud:
config:
name: etcp-client-microservice-config-center # 从 github 上读取的资源名称,注意没有yml后缀名
profile: prod # 配置默认的环境
label: master # 从 master 分支上获取配置信息
uri: http://www.config5001.com:5001 # Spring Cloud Config 获取的服务地址
application.yml ,主要作用为指定使用仓库中的那个配置文件,完整内容如下:
spring:
application:
name: etcp-client-microservice-config-center
修改主启动类 “ MicroserviceConfigProviderApp_8001 ”,完整内容如下:
package com.huazai.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
/**
*
* @author HuaZai
* @contact [email protected]
* <ul>
* @description
* <li>服务提供者1号服务器
* </ul>
* @className MicroserviceProviderApp_8001
* @package com.huazai.springcloud
* @createdTime 2018年05月06日 下午2:21:44
*
* @version V1.0.0
*/
@SpringBootApplication
@EnableEurekaClient // 本服务启动后会自动注册进eureka服务中
@EnableDiscoveryClient // 服务发现
public class MicroserviceConfigProviderApp_8001
{
public static void main(String[] args)
{
SpringApplication.run(MicroserviceConfigProviderApp_8001.class, args);
}
}
基于 Spring Cloud Config 分布式配置中心的 Provider 提供者服务器创建完成后,项目概览如下图:
注意:关于系统配置文件 “ bootstrap.yml ” 里的 profile 属性:该属性起到至关重要的作用,它直接决定了环境,通过该属性可以便捷的切换环境,该属性的属性值直接决定能从 GitHub 上取得什么样的环境配置,例如:在上面的 etcp-client-microservice-config-center.yml 环境配置内容中,开发环境 dev 使用 1 号数据库,测试环境 test 使用 2 号数据库,发布环境 prod 使用 3 号数据库,这样一来就达到了分布式配置的目的。
测试
测试开发环境,启动基于 Spring Cloud Config 分布式配置中心的提供者服务器,并访问提供者服务器地址,可以看到连接的是 1 号数据库,如下图:
测试环境:
发布环境:
GitLab 源码地址:
项目源码地址(zip格式的工程包):
好了,关于 Spring Cloud 进阶--Rest微服务加入config分布式配置中心实战 就写到这儿了,如果还有什么疑问或遇到什么问题欢迎扫码提问,也可以给我留言哦,我会一一详细的解答的。
歇后语:“ 共同学习,共同进步 ”,也希望大家多多关注CSND的IT社区。
作 者: | 华 仔 |
联系作者: | [email protected] |
来 源: | CSDN (Chinese Software Developer Network) |
原 文: | https://blog.csdn.net/Hello_World_QWP/article/details/88087904 |
版权声明: | 本文为博主原创文章,请在转载时务必注明博文出处! |