10.微服务全局配置中心之Config

什么是Config

Spring Cloud Config Server又称为统一配置中心。顾名思义就是将配置统一管理;配置统一管理的好处是在日后大规模集群部署服务应用时相同的服务配置一致,日后再修改配置只需要统一修改后进行同步而不需要一个一个服务手动维护。
在这里插入图片描述

  1. Spring Cloud Config Server基于Netfix config进行二次封装而来,分为Server端和Client端。Server端作为单个的Springboot项目存在,对其而言其余的微服务就是client
  2. 实际开发中Config Server作为微服务的一部分,为了保证系统的可用性,Config Server同样需要集群部署。为了避免挨个项目修改配置文件以及实现特殊情况下对配置文件的回滚操作,所以我们的Config Server采用从远程仓库读取配置文件并自动同步的方式

Config Server开发

上面我们说了Config Server采取从远程仓库读取配置文件的方式进行开发。那么在gitee\gitlab\github中我们选择国内的gitee(码云)作为我们的远程仓库。首先我们需要在远程仓库中新建一个仓库并获取仓库地址以供Config Server读取配置文件使用。

gitee仓库

登录|注册

在进行这一步的前提是我们需要注册一个gitee账号,登录后我们直接在首页点击立即创建仓库
在这里插入图片描述

创建仓库

按照下图提示填写仓库信息,最后点击创建
在这里插入图片描述

拷贝地址

在这里插入图片描述
仓库创建完毕后页面会自动跳转到仓库详情页,我们点击上方的克隆/下载->HTTPS->复制,然后就可以得到仓库的地址:https://gitee.com/tide001/config-server.git。这个后面我们要使用到

Server端开发

1.新建Module

我们在springcloud_parent项目中新建Module - 07.springcloud_config_server,点击Finish
在这里插入图片描述

2.pom.xml
<?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>springcloud_parent</artifactId>
        <groupId>com.christy</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>07.springcloud_config_server</artifactId>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- 引入consul client依赖 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-consul-discovery</artifactId>
        </dependency>

        <!-- 引入健康检查依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

        <!--引入统一配置中心-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>

    </dependencies>
</project>
3.ConfigServerApplication.java
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;

/**
 * @Author Christy
 * @Date 2021/7/9 13:35
 *
 * @EnableConfigServer注解开启统一配置中心
 **/
@SpringBootApplication
@EnableDiscoveryClient
@EnableConfigServer
public class ConfigServerApplication {
    public static void main(String[] args){
        SpringApplication.run(ConfigServerApplication.class, args);
    }
}
4.application.properties
server.port=8910
spring.application.name=CONFIGSERVER

#注册服务中心
spring.cloud.consul.host=localhost
spring.cloud.consul.port=8500

# 配置远程仓库
# 指定仓库的url
spring.cloud.config.server.git.uri=https://gitee.com/tide001/config-server.git
# 指定访问的分支
spring.cloud.config.server.git.default-label=master

# 如果是私有仓库 需要配置用户名和密码
#spring.cloud.config.server.git.username=
#spring.cloud.config.server.git.password=
5.启动

我们点击启动按钮,服务如果正常启动并且成功注册到consul的话,说明我们的config server就成功了
在这里插入图片描述

Config Client开发

我们上面说了,这里的client在实际开发中其实是一个个的微服务。相对于Config Server他们就是一个个的Client。我们这里新建一个Module作为client来进行演示效果

1.新建Module

我们在springcloud_parent项目中新建Module - 07.springcloud_config_client,点击Finish
在这里插入图片描述

2.pom.xml

<?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>springcloud_parent</artifactId>
        <groupId>com.christy</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>07.springcloud_config_client</artifactId>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

    <dependencies>
        <!-- 引入springboot依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- 引入consul client依赖 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-consul-discovery</artifactId>
        </dependency>

        <!-- 引入健康检查依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

        <!-- 引入统一配置中心客户端依赖 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
    </dependencies>
</project>

引入统一配置中心客户端依赖后(微服务)系统就会自动将其当做统一配置中心的客户端而无需像Config Server在启动类上添加额外的注解

3.ConfigClientApplication.java

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

/**
 * @Author Christy
 * @Date 2021/7/13 9:42
 **/
@SpringBootApplication
@EnableDiscoveryClient
public class ConfigClientApplication {
    public static void main(String[] args){
        SpringApplication.run(ConfigClientApplication.class, args);
    }
}

4.TestController.java

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author Christy
 * @Date 2021/7/13 9:48
 **/
@RestController
@RequestMapping("/test")
public class TestController {
    private static final Logger log = LoggerFactory.getLogger(TestController.class);

    @Value("${username}")
    private String username;

    @GetMapping("/hello")
    public String sayHello(){
        log.info("Hello " + username + ", Welcome!");
        return "Hello " + username + ", Welcome!";
    }
}

这个Controller没有实际意义,仅仅作为测试使用;username的值来源于配置文件

5.bootstrap.properties

# 配置注册中心
spring.cloud.consul.host=localhost
spring.cloud.consul.port=8500

# 告诉当前Config Client统一配置中心在注册中心的服务id
spring.cloud.config.discovery.service-id=CONFIGSERVER
# 开启当前Config Client 根据服务id去注册中心获取
spring.cloud.config.discovery.enabled=true

# 获取那个配置文件 1.确定分支  2.确定文件名  3.确定环境
spring.cloud.config.label=master
spring.cloud.config.name=configclient
spring.cloud.config.profile=prod

我们上面说了client的配置文件都是放在远程仓库上面的,但不是说client的所有配置都放在远程仓库。client端启动的时候通过注册中心上的Server端来加载远程仓库的配置文件,并且还要告诉Server端加载哪一个分支下面的哪个client的哪一个环境的配置文件。

实际开发中会有很多个client需要将配置文件放在远程仓库

每个client的配置文件可能会细分为测试、开发、正式等

每个仓库有可能细分为master、test等多个分支

综上可以知道,上述配置文件中的配置缺一不可,而且只能放在项目中不能放在远程仓库。

相信小伙伴们也注意到了这次的配置文件名称bootstrap.properties和以前的application.properties不一样了

bootstrap.(properties|yml)作为预配置文件,在启动项目时会优先拉取远程配置,远程配置拉取成功之后根据远程配置启动当前应用。

application.(properties|yml)作为已确定的配置文件,在启动项目时不会等待远程配置拉取,直接根据配置文件中的内容启动。因此当需要注册中心、服务端口等信息时远程配置还没有拉取到,这个时候启动会直接报错

在这里插入图片描述

6.配置远程仓库

这里所谓的配置远程仓库其实是在远程仓库新建configclient的配置文件

1.新建文件

在这里插入图片描述

2.configclient.properties
server.port=8911
spring.application.name=CONFIGCLIENT

spring.profile.active=dev

在这里插入图片描述

点击提交后返回仓库详情页面可以看到我们刚才新建的文件

在这里插入图片描述

3.configclient-dev.properties

按照上述步骤2的操作方法添加文件configclient-dev.properties,内容只有一个username,如下

username=dev
4.configclient-prod.properties

按照上述步骤2的操作方法添加文件configclient-prod.properties,内容只有一个username,如下

username=prod

至此们client端的三个配置文件就都配置好了

7.加载配置文件

我们上面提到过Config Client自己不加载配置文件,而是通过服务注册中心的Config Server进行加载,Config Server加载到远程仓库的配置文件后会拉取到本地进行保存,然后通过负载均衡策略通过选取的服务节点将配置信息返回给Config Client。Config Server拉取配置文件可以通过下面的方式进行拉取

http://serverhost:port/branch/serverid-type.properties|yml|json

  • serverhost:port指的是Config Server的路径
  • branch指的是加载远程仓库的哪个分支下面的配置文件
  • serverid指的是加载哪个微服务的配置文件
  • type指的是加载哪个分支(dev|test|prod)的配置文件

比如我们需要访问dev的配置文件,可以在浏览器输入http://localhost:8910/master/configclient-dev.properties
在这里插入图片描述

我们可以看到他将我们的configclient.propertiesconfigclient-dev.properties自动合并了。这里有个地方需要注意,如果我们想直接访问configclient.properties的话不能直接在浏览器输入http://localhost:8910/master/configclient.properties,否则你将得到如下结果
在这里插入图片描述
我们上面说了Config Server拉取配置文件是通过固定的格式http://serverhost:port/branch/serverid-type.properties|yml|json来拉取的,如果我们仅仅想要访问configclient.properties的话,直接在浏览器输入http://localhost:8910/master/configclient-xxx.properties即可
在这里插入图片描述

Config Server通过约定的方式拉取配置文件,比如我们需要拉取configclient-dev.properties这个文件,他会自动将configclient.propertiesconfigclient-dev.properties两个文件都拉取并合并成一个文件返回给client。但是如果没有找到configclient-dev.properties这个文件,他就仅仅返回configclient.properties这个文件

8.测试

我们现在启动Config Client(注意先将配置文件修改为bootstrap.properties),在Config Server的控制台中可以看到远程仓库的配置文件已经被拉取到了本地
在这里插入图片描述
在这里插入图片描述
现在我们去访问一下Config Client的测试页面,浏览器输入http://localhost:8911/test/hello
在这里插入图片描述
我们现在修改bootstrap.properties中的内容为spring.cloud.config.profile=dev,重新启动Config Client后再次访问http://localhost:8911/test/hello
在这里插入图片描述

本系列专题源码已经上传至gitee:https://gitee.com/tide001/springcloud_parent,欢迎下载交流

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
Spring Cloud是一个基于Spring Boot的微服务框架,它提供了一整套用于构建和管理微服务的组件。下面是Spring Cloud微服务架构图的介绍: 1. 配置管理(Config Server):用于集中管理微服务的配置信息,可以将配置信息存储在Git、SVN等版本控制系统中,并通过Config Server进行统一管理和分发。 2. 服务注册与发现(Service Discovery):微服务架构中的各个服务需要能够自动注册和发现,以便实现服务之间的通信。Spring Cloud提供了服务注册与发现的组件,如Eureka、Consul等。 3. 断路器(Circuit Breaker):在微服务架构中,当某个服务出现故障或不可用时,为了避免级联故障,需要使用断路器模式进行故障隔离。Spring Cloud中的Hystrix组件提供了断路器的实现。 4. 智能路由(Intelligent Routing):微服务架构中的服务可能会有多个实例,智能路由可以根据负载均衡策略将请求分发到不同的实例上,以实现负载均衡。Spring Cloud中的Ribbon和Zuul组件提供了智能路由的功能。 5. 微代理(Micro Proxy):微服务架构中的服务之间需要进行通信,为了简化通信过程,可以使用微代理来处理请求和响应。Spring Cloud中的Feign组件提供了微代理的功能。 6. 控制总线(Control Bus):微服务架构中的各个服务需要能够实时通信和协调,控制总线可以提供消息传递和事件驱动的机制。Spring Cloud中的Spring Cloud Bus组件提供了控制总线的功能。 7. 全局锁(Distributed Lock):在分布式环境中,为了保证数据的一致性和并发控制,需要使用全局锁。Spring Cloud中的Zookeeper和Redis等组件提供了全局锁的实现。 8. 决策竞选(Leader Election):在分布式环境中,为了保证系统的高可用性,需要选举出一个主节点来处理请求。Spring Cloud中的Zookeeper和Election等组件提供了决策竞选的功能。 9. 分布式会话(Distributed Session):在微服务架构中,用户的会话信息可能需要在多个服务之间共享。Spring Cloud中的Spring Session组件提供了分布式会话的支持。 10. 集群状态管理(Cluster State Management):在微服务架构中,需要对集群中的各个服务进行监控和管理。Spring Cloud中的Spring Cloud Cluster组件提供了集群状态管理的功能。 以上是Spring Cloud微服务架构图的介绍,希望对你有帮助。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

潮汐先生

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值