spring cloud config configures git remote warehouse

The original configuration center is based on the local configuration file, now it uses the git remote warehouse form

One: Boot version 2.0.3.RELEASE used in this article, cloud version Finchley.SR4

Two: config configuration center code

1: Mainly rely on

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

 

2: yml file

#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: Entry class, mainly add @EnableConfigServer annotation

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 服务启动完成!######################");
    }
}

After the above code is configured, the config configuration center service is completed

Three: Other services read configuration center

  Baidu's other articles say that the service name of the reader (application name) must be the same as the yml prefix name of the warehouse center. In fact, it is not used here. Here you can explicitly specify which files need to be config.

First upload the code, the yml file of the gateway service

#注册中心
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  #超时时间,若不设置超时时间则有可能无法触发熔断

The service name here is api-gateway, and the file name of the required configuration center is api-druid-hdys, that is, api-druid-hdys-dev.yml, so this solves the problem that multiple services need the same configuration file at the same time questions are explicit designation spring.cloud.config.name:xxxx to

The access rules of Spring Cloud Config are supported here

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

/{name}-{profile}.yml

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

If profile: dev is added to the above, then the file is api-druid-hdys-dev.yml, otherwise it is api-druid-hdys-dev.yml

The main code:

spring:
  application:
      name: api-gateway
  main:
    allow-bean-definition-overriding: true #The bean found later will overwrite the bean with the same name before
  cloud:
    config:
      fail-fast: true #Whether to start the fast failure function, the function is turned on Then
      firstly determine whether the config server is normal name: api-druid-hdys #Configuration center Git warehouse config folder file name
      discovery:
        enabled: true
        service-id: config-server #spring cloud configuration center service name

My git repository

 

Guess you like

Origin blog.csdn.net/CarryBest/article/details/107315205