spring cloud :统一管理配置文件config

已有框架的缺点:目前每个微服务配置参数都放在每个项目的application.yml或者application.properties,如果要切换环境要设置多个Profile 比如eureka server 集群模拟,启动两个eureka sever要指定两个—spring.profiles.active=?

Config:在统一的地方动态配置环境参数(热更新)

 

 

架构图如下:

1.     添加统一配置文件目录

tcloud-commons-configrepo

添加如下将

子文件夹命名规则是:service-微服务名

开发环境配置文件命名规则是:微服务名-dev.yml

注意:不要有任何中文注释!

tcloud-user-provider-dev.yml内容是:

server:

  port: 8000

spring:

  datasource:

    url: jdbc:mysql://10.122.7.114:3306/svw_account?useUnicode=true

    username: root

    password: root

    driverClassName: com.mysql.jdbc.Driver

    max-active: 20

    max-idle: 8

    min-idle: 8

    initial-size: 10

 

mybatis:

  configuration:

    mapUnderscoreToCamelCase:true   

 

eureka:

  client:

    serviceUrl:

      defaultZone: http://localhost:8100/eureka/

  instance:

    prefer-ip-address:true  

文件application-dev.yml内容是:

profile: dev-1.0

文件application.yml内容是:

profile: default-1.0

2.     创建config server项目

1.     添加依赖

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>

 

    <groupId>com.svw.tbox.tcloud.commons</groupId>

    <artifactId>tcloud-commons-configserver</artifactId>

    <version>0.0.1-SNAPSHOT</version>

    <name>tcloud-commons-configserver</name>

    <url>http://maven.apache.org</url>

 

    <parent>

        <groupId>org.springframework.boot</groupId>

        <artifactId>spring-boot-starter-parent</artifactId>

        <version>1.4.3.RELEASE</version>

    </parent>

 

    <properties>

        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

        <java.version>1.8</java.version>

    </properties>

 

    <dependencies>

        <dependency>

            <groupId>org.springframework.cloud</groupId>

            <artifactId>spring-cloud-config-server</artifactId>

        </dependency>

    </dependencies>

    <!-- 引入spring cloud的依赖 -->

    <dependencyManagement>

        <dependencies>

            <dependency>

                <groupId>org.springframework.cloud</groupId>

                <artifactId>spring-cloud-dependencies</artifactId>

                <version>Camden.SR4</version>

                <type>pom</type>

                <scope>import</scope>

            </dependency>

        </dependencies>

    </dependencyManagement>

</project>

 

2.     启动类开启Config server

package com.svw.tbox.tcloud.commons.configserver;

 

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.cloud.config.server.EnableConfigServer;

 

@SpringBootApplication

@EnableConfigServer

publicclass ConfigServerApplication {

  publicstaticvoid main(String[] args) {

    SpringApplication.run(ConfigServerApplication.class, args);

  }

}

 

3.     配置文件指定config-repo文件仓库

server:

  port: 8300

spring:

  application:

    name: tcloud-commons-configserver

  cloud:

    config:

      server:

        git:

          clone-on-start:true    #启动快速识别错误的配资源

          uri: http://10.122.7.81/TBoxGroup/POC/tcloud-commons-configrepo.git          #uri: 对应gitlab地址

          search-paths: conf-{application}    # 扫描tcloud-commons-configrepo/<conf-指定微服务名>文件夹下的文件

          username:  huruifeng                                                       # Git仓库的账号

          password:  hurf10hurf10

##快速定位问题

#logging:

level:

#    org.springframework.cloud: DEBUG

#    org.springframework.boot:  DEBUG                                         

效果:

启动tcloud-commons-configserver

访问http://localhost:8300/application/dev如下:

访问http://localhost:8300/application/default如下:


访问http://localhost:8300/tcloud-user-provider/dev出现如下






======================================================

1.     微服务配置成config client

修改tcloud-user-provider 和tcloud-user-consumer

1.     添加依赖

        <dependency>

            <groupId>org.springframework.boot</groupId>

            <artifactId>spring-boot-starter-web</artifactId>

        </dependency>

        <dependency>

            <groupId>org.springframework.cloud</groupId>

            <artifactId>spring-cloud-starter-config</artifactId>

        </dependency>

        <dependency>

            <groupId>org.springframework.boot</groupId>

            <artifactId>spring-boot-starter-actuator</artifactId>

        </dependency>

 

2.     bootstrap.yml

spring:

  application:

    name: tcloud-user-provider    # 注册应用名{application}

  cloud:

    bus:

      trace:

        enabled:true

    config:

      failFast:true

      profile: local                # 对应config server所获取的配置文件中的{profile}

      label: develop          

      discovery:

        enabled:true

        serviceId: tcloud-commons-config-server

 

3.     @Value 注入

tcloud-user-provider内容如下:

@RestController

publicclass UserController {

   

    /**

     * 版本号

     */

    @Value("${profile}")

    private String profile;

   

    /**

     * 数据库连接地址

     */

    @Value("${spring.Datasource.url}")

    private String url;

   

    @GetMapping("/getValue")

    public String getConfigValue(){

        return"版本号是:"+profile+数据库连接地址是:"+url;

    }

……

注意:bootstrap.yml 是父目录文件优先(详细微服务中的bootstrap.yml参数不能覆盖config-repo中的bootstrap.yml参数,和application.*文件正好相反)

 

4.     效果

=》启动tcloud-commons-configserver

=》启动tcloud-base-eurekaserver

=》启动tcloud-gateway-zuulserver

=》启动tcloud-user-provider

=》访问http://localhost:8000/getValue

或者http://localhost:8200/tcloud-user-provider/getValue获得如下结果:



1.     配置文件动态更新

现在有个问题,每次更新tcloud-commons-configrepo/下的文件内容参数,对应的微服务又要重启获取变更后的内容

=》使用bus 设置自动更新(修改文件=>请求一个url=>自动更新到多有微服务)

架构:


1.     安装kafka 参考4.1
2.     改造config server

将config server 改造成eureka server发现的服务

将config server 注册到对应的 zk 和 kafka上

1.      添加依赖

       <dependency>

           <groupId>org.springframework.cloud</groupId>

           <artifactId>spring-cloud-config-server</artifactId>

       </dependency>

       <dependency>

           <groupId>org.springframework.cloud</groupId>

           <artifactId>spring-cloud-starter-eureka</artifactId>

       </dependency>

       <dependency>

           <groupId>org.springframework.cloud</groupId>

           <artifactId>spring-cloud-starter-bus-kafka</artifactId>

       </dependency>

2.      配置文件application.yml注册

## 注册到eureka

eureka:

  client:

    serviceUrl:

      defaultZone: http://localhost:8100/eureka/

  instance:

    prefer-ip-address:true   #注册IP,而不是默认hostname

spring:

  application:

    name: tcloud-commons-config-server

  cloud:

stream:

      default-binder: kafka

      kafka:

        binder:

          zk-nodes: 10.122.7.120:2181  # kafka zk节点

          brokers: 10.122.7.120:9092   # kafka server节点

#关闭健康检查需要用户名密码的访问的/bus/refresh(可以直接访问bus/refresh

management:

  security:

    enabled:false

3.      启动类添加声明eureka 可以发现

@SpringBootApplication

@EnableConfigServer

@EnableDiscoveryClient

publicclass ConfigServerApplication {

  publicstaticvoid main(String[] args) {

    SpringApplication.run(ConfigServerApplication.class, args);

  }

}

 

3.     改造config client
1.      添加依赖 (在两个微服务)

<dependency>

           <groupId>org.springframework.retry</groupId>

           <artifactId>spring-retry</artifactId>

       </dependency>

       <dependency>

           <groupId>org.springframework.boot</groupId>

           <artifactId>spring-boot-starter-aop</artifactId>

       </dependency>

<dependency>

           <groupId>org.springframework.boot</groupId>

           <artifactId>spring-boot-starter-web</artifactId>

       </dependency>

       <dependency>

           <groupId>org.springframework.cloud</groupId>

           <artifactId>spring-cloud-starter-eureka</artifactId>

       </dependency>

       <dependency>

           <groupId>org.springframework.cloud</groupId>

           <artifactId>spring-cloud-starter-feign</artifactId>

       </dependency>

       <dependency>

           <groupId>org.springframework.cloud</groupId>

           <artifactId>spring-cloud-starter-config</artifactId>

       </dependency>

       <dependency>

           <groupId>org.springframework.boot</groupId>

           <artifactId>spring-boot-actuator</artifactId>

       </dependency>

       <dependency>

           <groupId>org.springframework.cloud</groupId>

           <artifactId>spring-cloud-starter-bus-kafka</artifactId>

       </dependency>

       <dependency>

           <groupId>org.springframework.cloud</groupId>

           <artifactId>spring-cloud-starter-hystrix</artifactId>

       </dependency>

 

2.      在application-dev.yml中指定连接的kafka地址

spring:

  cloud:

stream:

      default-binder: kafka

      kafka:

        binder: 

          zk-nodes: 10.122.7.120:2181

          brokers: 10.122.7.120:9092

3.      Controller类添加@RefreshScope刷新注解(两个微服务都获取配置文件内容)

@RefreshScope

@RestController

@Api(description= "用户服务接口")

publicclass UserController {

    @Value("${profile}")

    private String profile;

    publicvoid setProfile(String profile) {

        this.profile = profile;

    }

    public String getProfile() {

        returnprofile;

    }

    @RequestMapping("/profile")

    public String profile() {

        returnthis.profile;

}

……

@RefreshScope

@RestController

publicclass TboxController {

……

    @Value("${profile}")

    private String profile;

    publicvoid setProfile(String profile) {

        this.profile = profile;

    }

    public String getProfile() {

        returnprofile;

    }

    @RequestMapping("/profile")

    public String profile() {

        returnthis.profile;

}

……

4.     效果

依次启动


Consumer 控制台出现

Mapped"{[/bus/refresh],methods=[POST]}" … 说明可以自动刷新

1.      Kafka查看是否存在对应队列

[root@T-kafka ~]# kafka-topics.sh --list --zookeeper localhost:2181

__consumer_offsets

atopic

springCloudBus

test

2.      查看和修改对应参数内容

访问两个微服务客户端地址

修改 config server中的参数 : profile: dev-2.0

 

Post请求 http://localhost:8200/tcloud-commons-config-server/bus/refresh  ,此处建议使用postman模拟post请求:

再次访问:

这两个地址

1.     问题总结

1.     当在config-repo中的app.xml中和本地bootstarp.xml中都有根节点的时候,会优先读取bootstarp.xml中的数据,比如:





  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值