1. 程式人生 > >第五篇 高可用配置中心config-server(SVN版)

第五篇 高可用配置中心config-server(SVN版)

try cat 註意 address con mat user 環境 文件

一 新建module引入config-server依賴

<dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.tmatesoft.svnkit/svnkit 此處是從svn上讀取配置文件要引入的依賴
--> <dependency> <groupId>org.tmatesoft.svnkit</groupId> <artifactId>svnkit</artifactId> </dependency> </dependencies>

當然還有eureka-client的依賴 我已經在父pom裏引用過 此處就不再重復引用

二 新建application.yml 內容如下

server:
  port: 8064
spring:
  application:
    name: config-server
  profiles:
    active: subversion #從svn上讀取時必加
  cloud:
    config:
      server:
        svn:
          uri: svn://XXXXX/tms/tms-callcenter/config
          search-paths: "{application}" #使用{application}占位符 踩坑點 必須加" " 否則 不識別文件夾搜索
default-label: trunk username: ** password: ** eureka: instance: prefer-ip-address: true instance-id: ${spring.cloud.client.ip-address}:${spring.application.name}:${server.port} client: serviceUrl: defaultZone: http://**:**@127.0.0.1:8060/eureka/

此處有需要註意的幾個地方 :

  1.spring.profiles.active=subversion

  2.svn上的default-label的默認是 trunk

  3.要想分環境來讀取配置文件 當search-paths 為 {application} 時 它是不識別文件夾的 只有加上“{application}"這樣才會根據文件夾去搜索

三 在啟動類上加@EnableConfigServer

package com.hmzj.configserver;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.config.server.EnableConfigServer;

@EnableConfigServer
@EnableDiscoveryClient
@SpringBootApplication
public class ConfigserverApplication {

    public static void main(String[] args) {
        SpringApplication.run(ConfigserverApplication.class, args);
    }
}

四 去svn的路徑中添加配置文件

技術分享圖片

文件的大概路徑如圖所示 並把它傳到svn上

支持的配置文件格式有

五 啟動config-server 檢驗是否能讀取到配置文件

瀏覽器訪問192.168.31.103:8061/{項目名稱}/{環境}

技術分享圖片

六 客戶端從svn上讀取配置文件

技術分享圖片

必須新建bootstrap.yml文件 不然讀取不到

內容如下

spring:
  application:
    name: callcenter-user
  profiles:
    active: subversion,dev
  cloud:
    config:
      name: {application}
      profile: test
      label: trunk
      retry:
        initial-interval: 2000
        max-attempts: 5
      discovery:
        enabled: true
        service-id: config-server
eureka:
  client:
    serviceUrl:
      defaultZone: http://hmzj:[email protected]:8060/eureka/
logging:
  path: D:\log
  file: ${spring.application.name}
  pattern:
    level: INFO

根據profile 來定義環境 即可

第五篇 高可用配置中心config-server(SVN版)