SpringCloud 进阶之分布式配置中心(SpringCloud Config)

Easter79
• 阅读 520

1. SpringCloud Config

  • SpringCLoud Config 为微服务架构中的微服务提供集中化的外部配置支持,配置服务器为各个不同微服务应用 的所有环境提供了一个中心化的外部配置;
  • SpringCloud Config 分为服务端和客户端:
    • 服务端也称为分布式配置中心,它是一个独立的微服务应用,用来连接配置服务器并为客户端提供获取配置信息, 加密/解密信息等访问接口;
    • 客户端则是通过指定的配置中心来管理应用资源,以及与业务相关的配置内容,并在启动的时候从配置中心获取和 加载配置信息;
  • 配置服务器默认采用Git来存储配置信息;

SpringCloud 进阶之分布式配置中心(SpringCloud Config)

1.1 SpringCloud Config服务端配置

GitHub 准备工作

// GitHub 新建一个名为 microservicecloudd-config 的新Repository
// 获得SSH协议的git地址: git@github.com:Noodlescn/microservicecloud-config.git
// 本地硬盘目录上,新建Git仓库,并clone
git clone git@github.com:Noodlescn/microservicecloud-config.git

// 本地Git仓库,microservicecloud-config 新建 application.yml
spring:
    profiles:
        active:
        - dev
---
spring:
    profiles: dev       # 开发环境
    application:
        name: microservicecloud-config-noodles-dev
---
spring:
    profiles: test      # 测试环境
    application:
        name: microservicecloud-config-noodles-test

# 文件保存为 UTF-8 格式


// 将application.yml推送到github上

新建 microservicecloud-config-3344(Cloud的配置中心模块)

// pom.xml
<!-- springCloud Config -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-config-server</artifactId>
</dependency>
<!-- 避免Config的Git插件报错 -->
<dependency>
    <groupId>org.eclipse.jgit</groupId>
    <artifactId>org.eclipse.jgit</artifactId>
    <version>4.10.0.201712302008-r</version>
</dependency>


// application.yml
server:
  port: 3344

spring:
  application:
    name: microservicecloud-config
  cloud:
    config:
      server:
        git:
          uri: git@github.com:Noodlescn/microservicecloud-config.git # GitHub上面的git仓库名字


// 主启动类
@SpringBootApplication
@EnableConfigServer
public class Config_3344_StartSpringCloudApp {

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

}


// 修改hosts文件,增加映射
// 127.0.0.1      config-3344.com


// 测试启动微服务 3344
// http://config-3344.com:3344/application-dev.yml
// http://config-3344.com:3344/application-test.yml

SpringCloud 进阶之分布式配置中心(SpringCloud Config)

1.2 SpringCloud Config客户端配置

GitHub 准备工作

// 本地 microservicecloud-config 路径下新建文件 microservicecloud-config-client.yml
spring:
    profiles:
        active
        - dev

---
server:
  port: 8201
spring:
  profiles: dev
  application:
    name: microservicecloud-config-client
eureka:
  client:
    service-url:
      defaultZone: http://eureka-dev.com:7001/eureka/
---
server:
  port: 8202
spring:
  profiles: test
  application:
    name: microservicecloud-config-client
eureka:
  client:
    service-url:
      defaultZone: http://eureka-test.com:7001/eureka/


// 提交到GitHub

新建 microservicecloud-config-client-3355

// pom.xml
<!-- springCloud Config 客户端-->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-config</artifactId>
</dependency>


// bootstrap.yml(是系统级配置文件,优先级最高)
spring:
  cloud:
    config:
      name: microservicecloud-config-client # 需要从github上读取的资源名称,注意没有yml后缀名
      profile: dev  # 本次访问的配置项
      label: master
      uri: http://config-3344.com:3344  
      # 本微服务启动后,先去找3344号服务,通过SpringCloudConfig获取GitHub的服务地址


// application.yml(是用户级的资源配置项)
spring:
  application:
    name: microservicecloud-config-client


// 修改hosts文件: 127.0.0.1  client-config.com

// 新建rest类,验证是否能从GitHub上读取配置
@RestController
public class ConfigClientRest {

    @Value("${spring.application.name}$")
    private String applicationName;

    @Value("${eureka.client.service-url.defaultZone}$")
    private String eurekaServers;

    @Value("${server.port}$")
    private String port;

    @RequestMapping("/config")
    private String getConfig() {
        String str = "applicationName:"+applicationName+"\t eurekaServers:"+eurekaServers+"\t port:"+port;
        System.out.println("********str:" + str);
        return str;
    }
}


// 主启动类ConfigClient_3355_StartSpringCloudApp
@SpringBootApplication
public class ConfigClient_3355_StartSpringCloudApp {

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

}


// 测试访问:
// http://client-config.com:8201/config

2. SpringCloud Config 配置实战

GitHub 准备工作

// 本地microservicecloud-config 下新建:
// microservicecloud-config-eureka-client.yml
spring:
  profiles:
    active:
    - dev
---
server:
  port: 7001

spring:
  profiles: dev
  application:
    name: microservicecloud-config-eureka-client

eureka:
  instance:
    hostname: eureka7001.com
  client:
    register-with-eureka: false  # 当前的eureka-server自己不注册进服务列表中
    fetch-registry: false  # 不通过eureka获取注册信息
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka/
---
server:
  port: 7001    # 注册中心占用7001端口

spring:
  profiles: test
  application:
    name: microservicecloud-config-eureka-client

eureka:
  instance:
    hostname: eureka7001.com
  client:
    register-with-eureka: false  # 当前的eureka-server自己不注册进服务列表中
    fetch-registry: false  # 不通过eureka获取注册信息
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka/


// 本地microservicecloud-config 下新建:
// microservicecloud-config-dept-client.yml
spring:
  profiles:
    active:
    - dev
---
server:
  port: 8001
spring:
  profiles: dev
  application:
    name: microservicecloud-config-dept-client
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: org.gjt.mm.mysql.Driver
    url: jdbc:mysql://localhost:3306/cloudDB01
    username: root
    password: root
    dbcp2:
      min-idle: 5
      initial-size: 5
      max-total: 5
      max-wait-millis: 200
mybatis:
    config-location: classpath:mybatis/mybatis.cfg.xml
    type-aliases-package: com.noodles.springcloud.entities
    mapper-locations:
    - classpath:mybatis/mapper/**/*.xml

eureka:
  client:  # 客户端注册进eureka服务列表内
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka
  instance:
    instance-id: dept-8001.com
    prefer-ip-address: true

info:
  app.name: noodles-microservicecloud-springcloudconfig01
  company.name: www.google.com
  build.artifactId: $project.artifactId$
  build.version: $project.version$
---
server:
  port: 8001
spring:
  profiles: test
  application:
    name: microservicecloud-config-dept-client
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: org.gjt.mm.mysql.Driver
    url: jdbc:mysql://localhost:3306/cloudDB02
    username: root
    password: root
    dbcp2:
      min-idle: 5
      initial-size: 5
      max-total: 5
      max-wait-millis: 200
mybatis:
    config-location: classpath:mybatis/mybatis.cfg.xml
    type-aliases-package: com.noodles.springcloud.entities
    mapper-locations:
    - classpath:mybatis/mapper/**/*.xml

eureka:
  client:  # 客户端注册进eureka服务列表内
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka
  instance:
    instance-id: dept-8001.com
    prefer-ip-address: true

info:
  app.name: noodles-microservicecloud-springcloudconfig02
  company.name: www.google.com
  build.artifactId: $project.artifactId$
  build.version: $project.version$


// 上传GitHub

新建 microservicecloud-config-eureka-client-7001 工程 (Config版的eureka服务端)

// pom.xml(参照microservicecloud-eureka-7001)
<!-- springCloud Config -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-config</artifactId>
</dependency>


// bootstrap.yml
spring:
  cloud:
    config:
      name: microservicecloud-config-eureka-client  # 需要从GitHub上读取的资源名称,没有yml后缀名
      profile: dev
      label: master
      uri: http://config-3344.com:3344    # SpringCloudConfig 获取的服务地址


// application.yml
spring:
  application:
    name: microservicecloud-config-eureka-client


// 主启动类
@SpringBootApplication
@EnableEurekaServer  // EurekaServer 服务器端启动类,接收其他微服务注册进来
public class Config_Git_EurekaServerApplication {

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

//测试
// 启动 microservicecloud-config-3344
// 启动 microservicecloud-config-eureka-client-7001
// 访问 http://eureka7001.com:7001/

新建 microservicecloud-config-dept-client-8001(Config版的dept微服务)

// pom.xml(参照8001)
<!-- SpringCloud 配置 -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-config</artifactId>
</dependency>


// bootstrap.yml
spring:
  cloud:
    config:
      name: microservicecloud-config-dept-client  # 需要从GitHub上读取的资源名称
      # profile 配置什么就读取什么,配置 dev 或 test
      profile: test
      label: master
      uri: http://config-3344.com:3344  # SpringCloudConfig 获取的服务地址


// application.yml
spring:
  application:
    name: microservicecloud-config-dept-client


// 主启动类
@SpringBootApplication
@EnableEurekaClient    // 本服务启动后,自动注册进eureka服务中
@EnableDiscoveryClient // 服务发现
public class DeptProvider8001_App {

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


// 测试
// 默认访问 test 配置: http://localhost:8001/dept/list
// 更换 dev 配置: http://localhost:8001/dept/list

SpringCloud 进阶之分布式配置中心(SpringCloud Config)

SpringCloud 进阶之分布式配置中心(SpringCloud Config)

参考资料:

点赞
收藏
评论区
推荐文章
blmius blmius
2年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
Stella981 Stella981
2年前
Spring Cloud Alibaba系列之Nacos分布式配置中心
SpringCloudAlibaba系列之Nacos分布式配置中心1、前言介绍SpringCloudAlibabaNacosConfig提供用于存储配置和其他元数据的key/value存储,为分布式系统中的外部化配置提供服务器端和客户端支持,nacosconfig是SpringCloudconfigServer和
Stella981 Stella981
2年前
Spring Cloud套件
SpringCloud提供的服务  配置管理服务注册服务发现断路器负载均衡智能路由服务间调用一次性令牌微代理思维导图模板全局锁控制总线分布式式会话集群状态领导选举分布式消息  子项目功能说明SpringCloudConfig配置中心,利用git来集
Stella981 Stella981
2年前
Spring Cloud Alibaba系列(二)nacos作为服务配置中心
SpringCloudAlibaba系列(二)nacos作为服务配置中心Nacos提供用于存储配置和其他元数据的key/value存储,为分布式系统中的外部化配置提供服务器端和客户端支持。使用SpringCloudAlibabaNacosConfig,您可以在NacosServer集中管理你SpringCloud应用的外部
Easter79 Easter79
2年前
SpringBoot自定义序列化的使用方式
场景及需求:项目接入了SpringBoot开发,现在需求是服务端接口返回的字段如果为空,那么自动转为空字符串。例如:\    {        "id":1,        "name":null    },    {        "id":2,        "name":"x
Stella981 Stella981
2年前
KVM调整cpu和内存
一.修改kvm虚拟机的配置1、virsheditcentos7找到“memory”和“vcpu”标签,将<namecentos7</name<uuid2220a6d1a36a4fbb8523e078b3dfe795</uuid
Stella981 Stella981
2年前
Spring Cloud Alibaba Nacos 服务配置中心和注册中心
学习在SpringCloud中使用Nacos实现服务配置中心和注册中心,类似SpringCloudConfig和SpringCloudNetflixEureka提供的功能。1概述SpringCloudAlibaba是阿里巴巴提供的一套微服务开发一站式解决方案。主要提供的功能:分布式配置中心
Easter79 Easter79
2年前
SpringCloud配置中心——ConfigServer搭建
_在实际应用中,一个系统的每个微服务都会有相同的配置,如数据库配置信息等等。为了将每个微服务的公共配置可以抽取出来。SpringCloud提供了Config配置中心的配置,作为配置中心(ConfigServer),提供给微服务(ConfigClient)读取并且加载配置。使用git仓库存放配置文件,SpringCloudConfig读取到之后会自
京东云开发者 京东云开发者
6个月前
Java服务总在半夜挂,背后的真相竟然是... | 京东云技术团队
最近有用户反馈测试环境Java服务总在凌晨00:00左右挂掉,用户反馈Java服务没有定时任务,也没有流量突增的情况,Jvm配置也合理,莫名其妙就挂了
linbojue linbojue
1个月前
SpringCloud原理解析与实战技巧
SpringCloud原理详解SpringCloud是一套基于SpringBoot的开源微服务架构构建工具集。它提供了在分布式系统(如配置管理、服务发现、断路器、智能路由、微代理、控制总线、一次性令牌、全局锁、领导选举、分布式会话和集群状态)环境中设计、构
Easter79
Easter79
Lv1
今生可爱与温柔,每一样都不能少。
文章
2.8k
粉丝
5
获赞
1.2k