zoukankan      html  css  js  c++  java
  • Spring Cloud Config 分布式配置中心

    Spring Cloud Config 简介

    在分布式系统中,由于服务数量巨多,为了方便服务配置文件统一管理,实时更新,所以需要分布式配置中心组件。

    Spring Cloud 中,有分布式配置中心组件 Spring Cloud Config ,它支持配置服务放在配置服务的内存中(即本地),也支持放在远程 Git 仓库中。

    Spring Cloud Config 组件中,分两个角色,一是服务端 Config Server ,二是客户端 Config Client

    Spring Cloud Config 服务端

    引入依赖

    pom.xml 中主要添加 spring-cloud-starter-netflix-eureka-serverspring-cloud-config-server 依赖

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

    相关配置

    需要在 bootstrap.yml 中配置,原因是可以优先加载配置。

    spring:
      cloud:
        config:
          label: {git仓库标签}
          server:
            git: 
              uri: {git仓库地址}
              search-paths: {仓库内文件存放路径}
              username: {用户名}
              password: {密码}
    

    配置说明:

    • spring.cloud.config.label:配置仓库的分支
    • spring.cloud.config.server.git.uri:配置 Git 仓库地址(GitHub、GitLab、Gitee …)
    • spring.cloud.config.server.git.search-paths:配置仓库路径(存放配置文件的目录)
    • spring.cloud.config.server.git.username:访问 Git 仓库的账号
    • spring.cloud.config.server.git.password:访问 Git 仓库的密码

    注意:如果使用 GitLab 作为仓库的话,spring.cloud.config.server.git.uri 需要在结尾加上 .git,GitHub 则不用。

    在 Git 仓库中创建配置文件存放目录

    在Git 仓库中创建文件夹,用于存放各个服务的资源配置文件。

    一般在开发环境中,都会独立出一个 Git 项目用来存放资源配置配置文件。

    HTTP 请求配置文件示例

    • http://{ip}:{port}/{applicationName}/{profile}[/{label}]
    • http://{ip}:{port}/{applicationName}-{profile}.yml
    • http://{ip}:{port}/{label}/{applicationName}-{profile}.yml

    Spring Cloud Config 客户端

    引入依赖

    hello-spring-cloud-netflix-eureka 工程的 pom.xml 中添加 spring-cloud-starter-config 依赖

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

    创建云配置文件

    在 Git 仓库中存放配置文件的目录下创建 hello-spring-cloud-netflix-eureka-dev.yml

    spring:
      application:
        name: hello-spring-cloud-netflix-eureka
    
    server:
      port: 8761
    
    eureka:
      instance:
        hostname: localhost
      client:
        registerWithEureka: false
        fetchRegistry: false
        serviceUrl:
          defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
    

    本地配置

    修改 hello-spring-cloud-netflix-eureka 工程的 applicaiton.ymlbootstrap.yml 并清空文件

    bootstrap.yml 中添加 spring-cloud-config 客户端的配置

    spring:   
      cloud:     
        config:       
          uri: http://localhost:8888       
          name: hello-spring-cloud-netflix-eureka-dev       
          label: master       
          profile: dev
    

    配置说明:

    • spring.cloud.config.uri:配置服务中心的网址
    • spring.cloud.config.name:配置资源配置文件名称(不含后缀)
    • spring.cloud.config.label:配置仓库的分支
    • spring.cloud.config.profile:配置文件的环境标识
      • dev:表示开发环境
      • test:表示测试环境
      • prod:表示生产环境

    测试云配置

  • 相关阅读:
    hdu 1199 Color the Ball 离散线段树
    poj 2623 Sequence Median 堆的灵活运用
    hdu 2251 Dungeon Master bfs
    HDU 1166 敌兵布阵 线段树
    UVALive 4426 Blast the Enemy! 计算几何求重心
    UVALive 4425 Another Brick in the Wall 暴力
    UVALive 4423 String LD 暴力
    UVALive 4872 Underground Cables 最小生成树
    UVALive 4870 Roller Coaster 01背包
    UVALive 4869 Profits DP
  • 原文地址:https://www.cnblogs.com/antoniopeng/p/12687628.html
Copyright © 2011-2022 走看看