SpringCloud入门9. SpringCloudConfig

分布式配置中心,分为服务端和客户端,基于Git来保存配置文件,来自动更新配置内容

简单的理解就是由配置服务器与git进行连接,从git上拉取某个配置文件,配置客户端和配置服务器相连接,客户端通过服务器获得git上的配置信息,插入到自己的配置文件中。

Git内容

git上传的配置文件 microservercloud-config-eureka.yml

spring:
  profiles:
    active:
    - dev
---
server:
  port: 7001
spring:
  profiles: dev
  application:
    name: microservercloud-config-eureka
eureka:
  instance:
    hostname: eureka7001.com #eureka服务端的实例名称
  client:
    register-with-eureka: false     #false表示不向注册中心注册自己。
    fetch-registry: false
    service-url:
      defaultZone: http://localhost:7003/eureka/,http://localhost:7002/eureka/
---
server:
  port: 7002
spring:
  profiles: test
  application:
    name: microservercloud-config-eureka
eureka:
  instance:
    hostname: eureka7002.com #eureka服务端的实例名称
  client:
    register-with-eureka: false     #false表示不向注册中心注册自己。
    fetch-registry: false
    service-url:
      defaultZone: http://localhost:7003/eureka/,http://localhost:7001/eureka/

配置服务器

服务端主要是建立和gitHub的联系

添加依赖

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

主配置类

@SpringBootApplication
@EnableConfigServer
public class SpringCloud_Config_App {
    public static void main(String[] args) {
        SpringApplication.run(SpringCloud_Config_App.class,args);
    }
}

配置文件

server:
  port: 3344
spring:
  application:
    name: microservercloud-config
  cloud:
    config: #配置文件所在的git仓库地址
      server:
        git:
          uri: https://github.com/manaski/microservercloud-config.git

配置客户端

客户端主要是建立和服务端的联系,拉取配置内容

eureka模块中添加一个新的配置文件

bootstrap.yml

spring:
  cloud:
    config:
      name: microservercloud-config-eureka #需要从github上读取的资源名称,注意没有yml后缀名
      profile: test   #本次访问的配置类型
      label: master
      uri: http://localhost:3344  #本微服务启动后先去找3344号服务,通过SpringCloudConfig获取GitHub的服务地址

一些固定的配置信息可以继续放在application.yml

扫描二维码关注公众号,回复: 10113316 查看本文章
spring:
  application:
    name: microservercloud-eureka
发布了47 篇原创文章 · 获赞 1 · 访问量 1595

猜你喜欢

转载自blog.csdn.net/chinamen1/article/details/102995317