SpringCloud分布式配置中心Config

统一管理所有配置。

1、微服务下的分布式配置中心
  简介:讲解什么是配置中心及使用前后的好处
     什么是配置中心:
     一句话:统一管理配置, 快速切换各个环境的配置

  相关产品:
    百度的disconf
    地址:https://github.com/knightliao/disconf

    阿里的diamand
    地址:https://github.com/takeseem/diamond

    springcloud的configs-server:
    地址:http://cloud.spring.io/spring-cloud-config/

    推荐干货文章:http://jm.taobao.org/2016/09/28/an-article-about-config-center/

2、SpringCloud的配置中心组件config-server

  简介:讲解SpringCloud配置中心config-server
    1、新建项目,创建config-server,pom依赖

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    2、启动类增加注解
    @EnableConfigServer

@SpringBootApplication
@EnableConfigServer
public class ConfigServerApplication {

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

    yml 配置:

#服务名称
spring:
  application:
    name: config-server

#服务的端口号
server:
  port: 9100

#指定注册中心地址
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/

以上配置启动会出现错误 Caused by: java.lang.IllegalStateException: You need to configure a uri for the git repository, 还需制定url

3、使用git服务器结合Config搭建分布式配置中心

  简介:讲解使用git服务器结合Config搭建分布式配置中心

  1、默认使用git存储配置中心
    使用git服务器,可以自己搭建gitlab服务器
    或者使用github、开源中国git、阿里云git

    [email protected]
    xxx.net123

    https://gitee.com/waitforxy/config_cloud.git

   2、配置文件添加配置
    spring:
      application:
        name: config-server
    #git配置
    cloud:
      config:
        server:
          git:
           uri: https://gitee.com/waitforxy/config_cloud
           username: [email protected]
           password: xxxx.net123
           #超时时间
           timeout: 5
           #分支
              default-label: master


  3、访问方式(一定要注意语法,如果有问题,会出错)
    多种访问路径,可以通过启动日志去查看
    例子 http://localhost:9100/product-service.yml

    /{name}-{profiles}.properties
    /{name}-{profiles}.yml
    /{name}-{profiles}.json
    /{label}/{name}-{profiles}.yml

    name 服务器名称
    profile 环境名称,开发、测试、生产
    lable 仓库分支、默认master分支

4、分布式配置中心客户端使用

  简介:微服务里面客户端接入配置中心
    官方文档:http://cloud.spring.io/spring-cloud-config/single/spring-cloud-config.html#_spring_cloud_config_client

  1、加入依赖
    <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-config-client</artifactId>
    </dependency>

  2、修改对应服务的配置文件,把application.yml 改为 bootstrap.yml(优先级问题)
    #指定注册中心地址
    eureka:
       client:
          serviceUrl:
        defaultZone: http://localhost:8761/eureka/

    #服务的名称
    spring:
      application:
        name: product-service
      #指定从哪个配置中心读取
      cloud:
        config:
          discovery:
            service-id: CONFIG-SERVER
            enabled: true
          profile: test
          #建议用lable,而不用profile去区分环境,默认是lable是master分支
          #label: test

    注意点:
      1.配置文件要用bootstrap.yml
      2.默认读取文件名是 服务名称

猜你喜欢

转载自www.cnblogs.com/daxiong225/p/13173552.html