spring cloud config 配置git远程仓库

原来的配置中心都是基于本地配置文件的方式,现改用git远程仓库的形式

一:本文所用boot版本 2.0.3.RELEASE,cloud 版本 Finchley.SR4

二:config配置中心代码

1:主要依赖

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

2:yml文件

#eureka注册中心地址
eureka:
  instance:
    prefer-ip-address: true
  client:
    service-url:
      defaultZone: http://127.0.0.1:8060/eureka/
    register-with-eureka: true

server:
  port: 8888

#本地仓库
#spring:
#  application:
#    name: config-server
#  profiles:
#    active: native  #本地配置必须有的
#  cloud:
#    config:
#      server:
#        native:
#          searchLocations: classpath:/config #本地配置的路径 读取/resources/config下的配置 默认读取文件夹叫config下的资源 客户端调用的配置文件的名字config.name即为config下的文件名的前缀

#远程仓库
spring:
  application:
    name: config-server  # 应用服务名称
  cloud:
    config:
      server:
        git:
          uri: https://github.com/tomducky/hdys-config-respo #git仓库地址
          username: #公共仓库可以不需要用户名、密码
          password:
          default-label: master #配置文件分支、默认master
          search-paths: config  #配置文件所在根目录 https://github.com/tomducky/hdys-config-respo/config/xxxxx.yml

3:入口类,主要添加 @EnableConfigServer注解

package com.carry.www.config;

import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.SpringCloudApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

/**
 * 类描述:
 *
 * @author :carry
 * @version: 1.0  CreatedDate in  2020年05月08日
 * <p>
 * 修订历史: 日期			修订者		修订描述
 */
@EnableConfigServer
@EnableEurekaClient
@SpringBootApplication
public class ConfigApplication implements CommandLineRunner {
    public static void main(String[] args) {
        SpringApplication.run(ConfigApplication.class, args);
    }

    @Override
    public void run(String... args) throws Exception {
        System.out.println("###################### config 服务启动完成!######################");
    }
}

以上代码配置后即完成了config 配置中心服务

三:其他服务读取配置中心

  百度其他的文章里说读取方服务名(application的名字)必须和仓库中心的yml前缀名一样,其实这里不用的,这里可以显性的指定需要config的哪些文件

先上代码,gateway服务的yml文件

#注册中心
eureka:
  instance:
    prefer-ip-address: true #将IP注册到Eureka Server上 http://127.0.0.1:8088/
  client:
    service-url:
      defaultZone: http://127.0.0.1:8060/eureka/
    register-with-eureka: true
    fetch-registry: true
#端口
server:
  port: 8088
spring:
  application:
      name: api-gateway
  main:
    allow-bean-definition-overriding: true #后发现的bean会覆盖之前相同名称的bean
  cloud:
    config:
      fail-fast: true #是否启动快速失败功能,功能开启则优先判断config server是否正常
      name: api-druid-hdys  #配置中心Git仓库config文件夹里的文件名字
      label: master         #默认分支master
      profile: dev          #不加此属性直接获取api-druid-hdys.yml,加了后符合config的名字规则api-druid-hdys-dev.yml 
      discovery:
        enabled: true
        service-id: config-server  #spring cloud 配置中心服务名
    gateway:
       discovery:
         locator:
           lowerCaseServiceId: true  # eureka服务名为大写  配置之后访问时无需大写
           enabled: false  # 是否与服务发现组件进行结合,通过serviceId转发到具体的服务实例。默认为false,为true代表开启基于服务发现的路由规则,为每一个服务创建了一个router,这个router将以服务名开头的请求路径转发到对应的服务。
                           # 如果此参数为true,并且下面用了path匹配,则为每个服务创建了2个router,一个是Path匹配 一个是服务名匹配
       routes:
        - id: api-auth #路由id 唯一
          uri: lb://api-auth # lb代表从注册中心获取服务 转发到此服务上
          predicates:  # Path Route Predicate Factory 模式
          - Path=/api-auth/** # 应用匹配路径 满足/api-auth/** 的请求将会匹配并被路由
          filters:
          - StripPrefix=1   #去掉第一个前缀api-auth  从二级url路径转发 http://localhost:aa/bbb  将会转发到http://服务名/test 和resTemplte类似
          - name: Hystrix  #熔断机制
            args:
              name : fallbackcmd
              fallbackUri: forward:/defaultfallback  # 可以为不同的服务走不同的熔断回调

hystrix:
  command:
      fallbackcmd:
          execution:
            isolation:
              thread:
                timeoutInMilliseconds: 1*5000  #超时时间,若不设置超时时间则有可能无法触发熔断

这里的服务名字叫api-gateway,而需要的配置中心的文件名字是api-druid-hdys,即api-druid-hdys-dev.yml,所以这样就解决了多个服务同时需要同一个配置文件的问题,都显性的指定spring.cloud.config.name:xxxx即可

这里支持Spring Cloud Config 的访问规则

/{name}/{profile}[/{label}]

/{name}-{profile}.yml

/{label}/{name}-{profile}.yml

上述如果加了 profile: dev 那么文件就是api-druid-hdys-dev.yml,不加即为api-druid-hdys-dev.yml

主要的代码:

spring:
  application:
      name: api-gateway
  main:
    allow-bean-definition-overriding: true #后发现的bean会覆盖之前相同名称的bean
  cloud:
    config:
      fail-fast: true #是否启动快速失败功能,功能开启则优先判断config server是否正常
      name: api-druid-hdys  #配置中心Git仓库config文件夹里的文件名字
      discovery:
        enabled: true
        service-id: config-server  #spring cloud 配置中心服务名

本人的git仓库

猜你喜欢

转载自blog.csdn.net/CarryBest/article/details/107315205