目录
- 1、分布式配置中心应用场景
 - 2、Spring Cloud Config
 - 2.1、Config简介
 - 2.2、Config分布式配置应用
 - 2.3、构建Config Server统一配置中心
 - 2.4、构建Client客户端(在已有简历微服务基础上)
 
1、分布式配置中心应用场景
往往,我们使用配置文件管理⼀些配置信息,比如application.yml
单体应用架构:配置信息的管理、维护并不会显得特别麻烦,手动操作就可以,因为就一个工程;
微服务架构:因为我们的分布式集群环境中可能有很多个微服务,我们不可能一个一个去修改配置然后重启生效,在一定场景下我们还需要在运行期间动态调整配置信息,比如:根据各个微服务的负载情况,动态调整数据源连接池大小,我们希望配置内容发生变化的时候,微服务可以自动更新。
场景总结如下:
- 集中配置管理,一个微服务架构中可能有成百上千个微服务,所以集中配置管理是很重要的(一次修改、到处生效)
 - 不同环境不同配置,比如数据源配置在不同环境(开发dev,测试test,⽣产prod)中是不同的
 - 运行期间可动态调整。例如,可根据各个微服务的负载情况,动态调整数据源连接池大小等配置修改后可自动更新
 - 如配置内容发生变化,微服务可以自动更新配置
 
那么,我们就需要对配置文件进行集中式管理(相同配置),这也是分布式配置中心的作用。
2、Spring Cloud Config
2.1、Config简介
Spring Cloud Config是一个分布式配置管理方案,包含了 Server端和 Client端两个部分。

- Server 端:提供配置文件的存储、以接口的形式将配置文件的内容提供出去,通过使用@EnableConfigServer注解在 Spring boot 应用中非常简单的嵌⼊
 - Client 端:通过接口获取配置数据并初始化自己的应用
 
2.2、Config分布式配置应用
说明:Config Server是集中式的配置服务,用于集中管理应用程序各个环境下的配置。 默认使用Git存储配置文件内容,也可以SVN。
比如,我们要对“简历微服务”的application.yml进行管理(区分开发环境、测试环境、生产环境)
登录Github,创建项目lagou-config-repo
上传yml配置文件,命名规则如下:
- {application}-{profile}.yml 或者 {application}-{profile}.properties
 - 其中,application为应用名称,profile指的是环境(用于区分开发环境,测试环境、生产环境等)
 - 示例:lagou-service-resume-dev.yml、lagou-service-resume-test.yml、lagouservice-resume-prod.yml
 
2.3、构建Config Server统一配置中心
新建SpringBoot工程,引入依赖坐标(需要注册自己到Eureka)
<?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>lagou-parent</artifactId>
        <groupId>com.lagou.edu</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <artifactId>lagou-cloud-configserver-9006</artifactId>
    <dependencies>
        <!--eureka client 客户端依赖引入-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <!--config配置中心服务端-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
    </dependencies>
</project>
配置启动类,使用注解@EnableConfigServer开启配置中心服务器功能
package com.lagou.edu;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.config.server.EnableConfigServer;
@SpringBootApplication
@EnableDiscoveryClient
@EnableConfigServer  // 开启配置中心功能
public class ConfigServerApplication9006 {
    public static void main(String[] args) {
        SpringApplication.run(ConfigServerApplication9006.class,args);
    }
}
application.yml配置
server:
port: 9006
#注册到Eureka服务中心
eureka:
client:
service-url:
# 注册到集群,就把多个Eurekaserver地址使用逗号连接起来即可;注册到单实例(非集群模式),那就写一个就ok
defaultZone: http://LagouCloudEurekaServerA:8761/eureka,http://LagouCloudEurekaServerB:8762/eureka
instance:
prefer-ip-address: true #服务实例中显示ip,而不是显示主机名(兼容老的eureka版本)
# 实例名称: 192.168.1.103:lagou-service-resume:8080,我们可以自定义它
instance-id: ${spring.cloud.client.ip-address}:${spring.application.name}:${server.port}:@project.version@
spring:
application:
name: lagou-cloud-configserver
# =================config核心配置==============
cloud:
config:
server:
git:
uri: https://github.com/5173098004/lagou-config-repo.git #配置git服务地址
username: 517309804@qq.com #配置git用户名
password: yingdian12341 #配置git密码
search-paths:
– lagou-config-repo
# 读取分支
label: master
# =================config核心配置==============
#针对的被调用方微服务名称,不加就是全局生效
#lagou-service-resume:
# ribbon:
# NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RoundRobinRule #负载策略调整
# springboot中暴露健康检查等断点接口
management:
endpoints:
web:
exposure:
include: "*"
# 暴露健康接口的细节
endpoint:
health:
show-details: always
测试访问:http://localhost:9006/master/lagou-service-resume-dev.yml,查看到配置文件内容
2.4、构建Client客户端(在已有简历微服务基础上)
已有工程中添加依赖坐标
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-config-client</artifactId>
</dependency>
application.yml修改为bootstrap.yml配置文件
bootstrap.yml是系统级别的,优先级比application.yml高,应用启动时会检查这个配置文件,在这个配置文件中指定配置中心的服务地址,会自动拉取所有应用配置并且启用。
(主要是把与统⼀配置中心连接的配置信息放到bootstrap.yml)
注意:需要统一读取的配置信息,从集中配置中心获取
bootstrap.yml
server:
port: 8080
spring:
application:
name: lagou-service-resume
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/lagou?useUnicode=true&characterEncoding=utf8
username: root
password: 123456
jpa:
database: MySQL
show-sql: true
hibernate:
naming:
physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl #避免将驼峰命名转换为下划线命名
# ===========核心配置==========
cloud:
# config客户端配置,和ConfigServer通信,并告知ConfigServer希望获取的配置信息在哪个文件中
config:
name: lagou-service-resume #配置文件名称
profile: dev #后缀名称
label: master #分支名称
uri: http://localhost:9006 #ConfigServer配置中心地址
# ===========核心配置==========
#注册到Eureka服务中心
eureka:
client:
service-url:
# 注册到集群,就把多个Eurekaserver地址使用逗号连接起来即可;注册到单实例(非集群模式),那就写一个就ok
defaultZone: http://LagouCloudEurekaServerA:8761/eureka,http://LagouCloudEurekaServerB:8762/eureka
instance:
prefer-ip-address: true #服务实例中显示ip,而不是显示主机名(兼容老的eureka版本)
# 实例名称: 192.168.1.103:lagou-service-resume:8080,我们可以自定义它
instance-id: ${spring.cloud.client.ip-address}:${spring.application.name}:${server.port}:@project.version@
# 自定义Eureka元数据
metadata-map:
cluster: cl1
region: rn1
management:
endpoints:
web:
exposure:
include: "*"
这种配置完成后,当我们启动项目后,config配置会自动根据我们配置的url,借助于url会去github上,将我们所配置在github上的文件加载到本地配置yml文件中,我们可以定义一个类来尝试获取这个文件。
# Github上文件内容如下
mysql:
url:dev-http://localhost:3306/db600
lagou:
message: hello lagou,600
package com.lagou.edu.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
 * 该类用于模拟,我们要使用共享的那些配置信息做一些事情
 */
@RestController
@RequestMapping("/config")
@RefreshScope
public class ConfigController {
    // 和取本地配置信息一样
    @Value("${lagou.message}")
    private String lagouMessage;
    @Value("${mysql.url}")
    private String mysqlUrl;
    // 内存级别的配置信息
    // 数据库,redis配置信息
    @GetMapping("/viewconfig")
    public String viewconfig() {
        return "lagouMessage==>" + lagouMessage  + " mysqlUrl=>" + mysqlUrl;
    }
}
			
评论(0)