zoukankan      html  css  js  c++  java
  • SpringCloud-config使用SVN作为配置中心

    前言


    重构成微服务后,每个服务都需要部署很多个实例,在修改配置时不可能每个实例手动去修改,因此使用springboot-config。本想使用git,还是由于公司内部问题只能使用svn,记录下搭建配置中心的过程

    创建SVN目录上传配置


    这步骤就不细说...最终的目录为http://....../config-repo/trunk/**/**--dev.yml,http://....../config-repo/trunk/**/**--prod.yml

    这里的trunk就用于分辨线上和开发的目录,最后后缀的dev和prod也用于分辨环境(因为svn默认就是trunk,目录上必须有这一层,实际上这层是没有必要的)

    config-server


    配置中心的也是一个服务,pom文件中需要引入

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

    application.yml文件如下, 使用svn的话spring.profiles.active: subversion这项必须指定

    server:
      port: 8888
    spring:
      application:
        name: service-config
      profiles:
        active: subversion # 使用svn
      cloud:
        config:
          server:
            svn:
              uri: http://** #svn地址
              search-paths: "{application}" #使用{application}占位符  必须加" " 否则 不识别文件夹搜索
              username: user
              password: pwd
              default-label: trunk
    eureka:
      client:
        service-url:
          defaultZone: http://0.0.0.0:8761/eureka/
    
    # 允许/actuator/bus-refresh接口被外部调用, 手动刷新config
    management:
      endpoints:
        web:
          exposure:
            include: "*"

    config-server服务的启动类需要加上@EnableConfigServer注解

    到此为止服务端就搭建好了,可以启动后尝试访问http://0.0.0.0:8888/(配置文件名)查看是否可以正常访问

    仓库中的配置文件会被转换成web接口,访问可以参照以下的规则:

    • /{application}/{profile}[/{label}]
    • /{application}-{profile}.yml
    • /{label}/{application}-{profile}.yml
    • /{application}-{profile}.properties
    • /{label}/{application}-{profile}.properties

    以neo-config-dev.properties为例子,它的application是neo-config,profile是dev。client会根据填写的参数来选择读取对应的配置

    configserver每次都是从svn的最新版本读取内容

    客户端(微服务端)


    添加依赖

     <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

    每个服务的配置文件都需要名为bootstrap.yml, springboot在启动时会优先读取这个名字的配置文件

    spring:
      application:
        name: entry-service
      profiles:
        active: subversion
      cloud:
        config:
          name: {application}
          label: trunk
          discovery:
            enabled: true
            service-id: service-config  # 注册中心的服务名
          profile: dev  # 指定配置文件的环境
    eureka:
      client:
        serviceUrl:
          defaultZone: http://0.0.0.0:8761/eureka/

    配置文件如上,需要指定配置中心的id,实际就是config-server的application.name,并且使用svn的话spring.profiles.active: subversion这项也必须指定

    配置完成后启动即可从配置中心读取,在使用@Value注入配置的类上加上@RefreshScope, 然后访问/actuator/refresh就可以刷新配置

    遇到的两个问题:

    1. 因为项目是多数据源,所以使用的是自定义数据源配置的DataSource,用@Bean注入。
    SpringBoot 2.0以上默认使用Hikari连接池,一旦连接池启动,就无法再修改HikariDataSource,所以刷新配置时连带数据源一起刷新,于是会报错。

    Caused by: java.lang.IllegalStateException: The configuration of the pool is sealed once started. Use HikariConfigMXBean for runtime changes.

    解决方法: 在自定义的DataSource上加入注解@RefreshScope,或者使用spring.scloud.refresh.extra-refreshable配置指定classname列表即可。

    2.使用/refresh报404,SpringBoot 2.0以后接口为/actuator/refresh, 且必须为post请求

  • 相关阅读:
    Webpack笔记(三)——一款破产版脚手架的开发
    Google C++命名规范
    视觉词袋模型(BOVW)
    机器学习之四:决策树
    Zernike不变矩
    互联网产品各阶段的标准流程文档
    机器学习之三:logistic回归(最优化)
    Python
    机器学习之二:K-近邻(KNN)算法
    Python学习之二:Python 与 C 区别
  • 原文地址:https://www.cnblogs.com/gtblog/p/11250513.html
Copyright © 2011-2022 走看看