Spring-Cloud-config配置中心

首先得要有github,或者gitlab,itee的账号

config配置中心

概述

Spring-Cloud-Config:

  1. 做项目, 那么就少不了配置 微服务架构中,配置文件众多,各个服务的配置文件也有可能不一样,
  2. Spring为我们提供了相应的配置中心组件--Spring Cloud config
  3. 他是一个配置管理中心,用于集中管理程序中各个环境下的配置,我们可以将配置通过git或svn等方式推送到我们的应用程序
  4. 同Eureka一样,他也分为server端与client端

优点

  1. 提供 服务端 和 客户端 支持
  2. 集中式 管理分布式环境下的应用配置
  3. 基于 Spring 环境,无缝 与 Spring 应用集成
  4. 可用于 任何 语言开发的程序
  5. 默认实现基于 git 仓库,可以进行 版本管理
  6. 可替换 自定义实现

Spring Cloud Config Server 作为配置中心服务端

  1. 拉取配置时更新 git 仓库副本,保证是最新结果
  2. 支持数据结构丰富,yml, json, properties 等
  3. 配合 eureke 可实现服务发现,配合 cloud bus 可实现配置推送更新
  4. 配置存储基于 git 仓库,可进行版本管理
  5. 简单可靠,有丰富的配套方案

Spring Cloud Config Client 默认客户端实现

  1. SpringBoot 项目不需要改动任何代码 加入一个启动配置文件指明使用
    ConfigServer 上哪个配置文件即可

config-server服务端配置

工程搭建

  1. 创建一个config-server工程管理添加依赖
    在这里插入图片描述
    在这里插入图片描述
dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
    </dependencies>
  1. 创建启动类,添加注解
    在这里插入图片描述
@SpringBootApplication
@EnableConfigServer
public class configApp {
    public static void main(String[] args) {
        SpringApplication.run(configApp.class, args);
    }
}
  1. 上传github的配置文件
    新创建一个仓库,克隆到本地
    在这里插入图片描述

在这里插入图片描述
创建testConfigServer.yml添加如下配置

spring:
  profiles:
    active: dev
---
# 开发环境
spring:
  profiles: dev

server:
  port:
    1000

---
#测试环境
spring:
  profiles: stg

server:
  port:
    1001

在这里插入图片描述
把创建的复制到克隆的地方
在这里插入图片描述
提交并上传到github仓库
在这里插入图片描述
4. 创建application.yml核心配置文件
在这里插入图片描述

server:
  port: 2000
spring:
  application:
    name: testConfigServer

  cloud:
    config:
      server:
        git: #仓库地址
          uri: 
  1. 启动访问http://localhost:2000/testConfigServer-dev.yml
    在这里插入图片描述
    访问规则:

/{application}/{profile}[/{label}] /{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties

解释:
application: 配置文件的名字
profile:对应的环境
label:不同的分支

  1. 如果配置文件放入了github仓库中的某个目录组需要添加以下配置
    在这里插入图片描述

Config Client配置

1. 在user或goods工程中添加依赖

		<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>

2. 把user或goods的application配置文件上传到github仓库更改名称防止文件重名

在这里插入图片描述

3. 在要使用配置文件的微服务当中添加一个bootstrap.yml的配置文件

在这里插入图片描述

spring:
  cloud:
    config:
      name: goods #读取github的goods配置文件
      uri: http://localhost:2000/ #config server的地址
      label: master #分支名称

删除原来的goods的application.yml

4.启动Eureka服务和goods服务,如果能起启动成功,并且注册到了Eureka表示已经config配置完成

在这里插入图片描述
启动成功,并且端口号是我们自己配置的端口
浏览器访问:http://localhost:5001/getGoods.do
在这里插入图片描述

Config集群配置

1.新创建一个configserver工程,注意修改端口号

在这里插入图片描述
2. 在原有的configserver和新创建的configServer添加如下依赖
在这里插入图片描述

 <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

2. 在两个configserver的启动类添加@EnableEurekaClient注解

在这里插入图片描述

3. 在两个工程的配置文件中添加

在这里插入图片描述

eureka:
  client:
    serviceUrl:
      #eureka服务端提供的注册地址 参考服务端配置的这个路径
      defaultZone: http://eureka:3000/eureka,http://eureka1:3001/eureka,http://eureka2:3002/eureka2
  instance:
    instance-id: config-server-0 #此实例注册到eureka服务端的唯一的实例ID
    prefer-ip-address: true #是否显示IP地址
    #eureka客户需要多长时间发送心跳给eureka服务器,表明它仍然活着,默认为30 秒 (与下面配置的单位都是秒)
    leaseRenewalIntervalInSeconds: 1
    #Eureka服务器在接收到实例的最后一次发出的心跳后,需要等待多久才可以将此实例删除,默认为90秒
    leaseExpirationDurationInSeconds: 3

4. 在goods或者user工程中添加依赖

在这里插入图片描述

5. 在bootstrap.yml添加配置

spring:
  cloud:
    config:
      name: user #这是我们要读取的配置文件名 对应获取规则的{application}
      #profile: dev   #这个是要获取的环境 对应的便是{profile}
      label: master #这个就是获取的节点 对应的是{label}
      discovery:
        enabled: true
        service-id: testConfigServer #client-server的名称
   

eureka:
  client:
    serviceUrl:
      defaultZone:  http://eureka:3000/eureka,http://eureka1:3001/eureka,http://eureka2:3002/eureka2

在这里插入图片描述

6. 启动configServerEureka和user或goods

浏览器访问Eureka
在这里插入图片描述
Spring-Cloud-Netflix完结

猜你喜欢

转载自www.cnblogs.com/joker-dj/p/12678841.html