SpringCloud Config配置中心

SpringCloud Config

1.微服务意味着要将单体应用中的业务拆分成一个个子服务,每个服务的粒度相对较小,因此系统中会出现大量的服务。由于每个服务都需要必要的配置信息才能运行,所以一套集中式的、动态的配置管理设施是必不可少的。
2.SpringCloud Config为微服务架构中的微服务提供集中化的外部配置支持,配置服务器为各个不同的微服务应用的所有环节提供了一个中心化的外部配置。
3.流程在这里插入图片描述
4.当配置中心启动之后,会从Github或Gitee仓库中读取相关的配置文件,并读取到本地,然后其他的服务从配置中心中获取配置。

使用步骤

1.准备一个服务中心,这里不再赘述
2.添加相关的依赖

<!--config-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
        <!--eureka-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>

3.向服务中心进行注册,同时设置与Gitee相关的参数

server.port=3344

spring.application.name=cloud-config-center

#false表示不向注册中心注册自己
eureka.client.register-with-eureka=true
#false表示自己就是注册中心,仅需维护服务实例,不需要去检索服务
eureka.client.fetch-registry=true
#设置与Eureka server交互的地址查询服务和注册服务都需要依赖这个地址
eureka.client.service-url.defaultZone=http://eureka7001.com:7001/eureka/
#eureka.client.service-url.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/

#Config 配置中心 访问方式 http://config-3344.com:3344/${label}/${filename}.${type}
#仓库地址
spring.cloud.config.server.git.uri=
# 用户
spring.cloud.config.server.git.username=
# 密码
spring.cloud.config.server.git.password=
# 操作时长
spring.cloud.config.server.git.timeout=
# 分支
spring.cloud.config.label=
# 目录
spring.cloud.config.server.git.search-paths[0]=

3在Gitee仓库中创建一个项目,并添加相关文件,格式为${filename} - ${type}
在这里插入图片描述
4.依次启动服务注册中心和配置中心,进行配置文件的访问,访问方式:访问方式 http://config-3344.com:3344/ ${label}/ ${filename}- ${type}.properties
在这里插入图片描述
5.创建两个服务端添加依赖

		<!--config-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
        <!--eureka-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
        <!--监控-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-actuator</artifactId>
        </dependency>

6.在两个服务上创建配置文件bootstrap.properties,并添加以下配置

server.port=3366

spring.application.name=cloud-config-client

eureka.client.service-url.defaultZone=http://eureka7001.com:7001/eureka/

#读取那个分支
spring.cloud.config.label=master
#配置文件名称
spring.cloud.config.name=config
#配置文件的后缀
spring.cloud.config.profile=dev
#配置中心地址
spring.cloud.config.uri=http://localhost:3344

#暴露监控端点
management.endpoints.web.exposure.include=*

7.在两个服务上创建controller

@RestController
public class ConfigClientController {

    @Value("${config.info}")
    private String configInfo;

    @Value("${server.port}")
    private String serverPort;

    @GetMapping("/configInfo")
    public String getConfigInfo(){
        return "serverPort:"+serverPort+"     configInfo:"+configInfo;
    }
}

8.启动两台服务器,对服务进行访问,访问成功

在这里插入图片描述在这里插入图片描述
9.如果仓库的配置发生更改,可通过post请求访问http://localhost:3355/actuator/refresh进行刷新

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值