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

一 新建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 来定义环境 即可

猜你喜欢

转载自www.cnblogs.com/pangyangqi/p/9446291.html