SpringCloud——Config

一、概述
  分布式系统面临的一个问题:微服务意味着要将单体应用中的业务拆分成一个个子服务,每个服务的粒度相对较小,因此系统中会出现大量的服务。由于每个服务都需要必要的配置信息才能运行,所以一套集中式的、动态的配置管理设施是必不可少的。SpringCloud提供了ConfigServer来解决这个问题,这样我们就不必每一个服务自己带着一个application.yml,而是通过ConfigServer来完成对这些配置文件的管理。
  分布式配置中心:
在这里插入图片描述
  SpringCloud Config为微服务架构中的微服务提供集中化的外部配置支持,配置服务器为各个不同微服务应用的所有环境提供了一个中心化的外部配置。
  SpringCloud Config分为服务端和客户端两部分:
   服务端也称为分布式配置中心,它是一个独立的微服务应用,用来连接配置服务并为客户端提供获取配置信息,加密/解密信息等访问接口。
   客户端则是通过指定的配置中心来管理应用资源,以及与业务相关的配置内容,并在启动的时候从配置中心获取和加载配置信息。配置服务器默认采用git来存储配置信息,这样就有助于对环境配置进行版本管理,并且可以通过git客户端工具来方便的管理和访问配置内容。
  由于SpringCloudConfig默认使用Git来存储配置文件(也有其他方式,比如SVN和本地文件,但是推荐使用Git,使用的是http/https访问的形式),因此需要与GitHub配置整合。

二、作用:
 1、集中管理配置文件
 2、不同环境不同配置,动态化的配置更新,分环境部署比如dev/test/prod/beta/release
 3、运行期间动态调整配置,不再需要在每个服务部署的机器上编写配置文件,服务会向配置中心统一拉取自己的配置信息
 4、当配置发生变动时,服务不需要重启即可感知到配置的变化并应用新的配置
 5、将配置信息以REST接口的形式暴露

三、服务端与GitHub通信
 1、在GitHub上新建一个Repository:microservicecloud-config
 2、获取该GitHub仓库的SSH协议地址:git@github.com:xxx/microservicecloud-config.git
 3、在本地硬盘新建git仓库并clone:在D:\WorkSpace\git\myspringcloud(任意目录)下执行git命令

git clone git@github.com:xxx/microservicecloud-config.git

在这里插入图片描述
 4、在拉取下来的microservicecloud-config文件夹下新建application.yml配置文件:注意该文件一定要以UTF-8的编码格式保存

spring:
 profiles:
  active:
  - dev
---
spring:
 profiles: dev #开发环境
 application:
  name: microservicecloud-config-atguigu-dev
---
spring:
 profiles: test
 application:
  name: microservicecloud-config-atguigu-test

 5、将application.yml推送至GitHub
在这里插入图片描述
  上面的工作可以看做是运维工程师将配置文件上传至GitHub上,下面就应该是开发工程师从GitHub上读取到配置文件的信息。一定要注意上传到GitHub的这些文件不要有制表符(/Tab),且格式要完全正确,否则会报错。
 6、新建配置中心模块:microservicecloud-config-3344
  pom文件:增加如下两个依赖

<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.8.0.201706111038-r</version>
</dependency>

  完整的依赖:不是所有的都必须

<dependencies>
	<!-- springCloud Config -->
	<dependency>
		<groupId>org.springframework.cloud</groupId>
		<artifactId>spring-cloud-config-server</artifactId>
	</dependency>
	<!-- 避免Config的Git插件报错:org/eclipse/jgit/api/TransportConfigCallback -->
	<dependency>
		<groupId>org.eclipse.jgit</groupId>
		<artifactId>org.eclipse.jgit</artifactId>
		<version>4.10.0.201712302008-r</version>
	</dependency>
	<!-- 图形化监控 -->
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-actuator</artifactId>
	</dependency>
	<!-- 熔断 -->
	<dependency>
		<groupId>org.springframework.cloud</groupId>
		<artifactId>spring-cloud-starter-hystrix</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-config</artifactId>
	</dependency>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-jetty</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>
	</dependency>
	<!-- 热部署插件 -->
	<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>springloaded</artifactId>
	</dependency>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-devtools</artifactId>
	</dependency>
</dependencies>

  yml文件:

server: 
  port: 3344 
  
spring:
  application:
    name:  microservicecloud-config
  cloud:
    config:
      server:
        git:
          uri: git@github.com:xxx/microservicecloud-config.git #GitHub上面的git仓库SSH地址

  该微服务启动后会访问git@github.com:xxx/microservicecloud-config.git去获取配置文件信息
  主启动类:使用@EnableConfigServer启用SpringCloud的配置中心的服务端功能

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

 7、启动测试:启动3344,访问http://localhost:3344/application-test.yml和http://localhost:3344/application-dev.yml就会看到从GitHub上请求下来的配置文件
在这里插入图片描述
  访问http://localhost:3344/application-xxx.yml,由于application.yml中并没有这个xxx的配置,会出现如下配置:
在这里插入图片描述
  也可以指明获取哪个分支上的配置文件:
在这里插入图片描述
四、Config客户端通过Config服务端获得Github上的配置
 1、新建模块microservicecloud-config-client-3355
  pom.xml:

<dependencies>
	<!-- SpringCloud Config客户端 -->
	<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>
	<dependency>
		<groupId>org.springframework.cloud</groupId>
		<artifactId>spring-cloud-starter-hystrix</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-config</artifactId>
	</dependency>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-jetty</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>
	</dependency>
	<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>springloaded</artifactId>
	</dependency>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-devtools</artifactId>
	</dependency>
</dependencies>

  bootstrap.yml:该名称的文件为系统级别的,优先级比application.yml要高

spring:
  cloud:
    config:
      name: microservicecloud-config-client
      profile: test
      label: master
      uri: http://localhost:3344

  注意此处的config.name属性的值一定要和GitHub上的yml文件的名称完全一致,该属性的意思就是通过http://localhost:3344从GitHub上找以config.name属性的值为名称的yml配置文件作为自己的主配置文件。
  application.yml:该文件可以没有,因为在bootstrap.yml中定义了

spring:
  application:
    name: microservicecloud-config-client

  主启动类:

@SpringBootApplication
public class ConfigClient_3355 {

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

  Controller:

@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")
	public String getConfig() {
		String str = applicationName + "\t" + eurekaServers + "\t" + port;
		return str;
	}
}

 2、启动测试:启动3344和3355,访问3355的接口即可,由于在bootstrap.yml中将profiles的值置为了test,因此该应用的端口就是GitHub上对应的8202端口
在这里插入图片描述
五、配置演示与策略切换
 目的:做一个Eureka服务 + 一个Dept微服务,两个微服务的配置统一由GitHub获得,实现统一配置分布式管理
 Ⅰ、搭建Config的客户端Eureka
  1、在本地新建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
  fetch-registry: false #不通过eureka获取注册信息
  service-url:
   defaultZone: http://eureka7001.com/eureka/
---
server:
 port: 7001
 
spring:
 profiles: test
 application:
  name: microservicecloud-config-eureka-client
eureka:
 instance:
  hostname: eureka7001.com
 client:
  register-with-eureka: false
  fetch-registry: false
  service-url:
   defaultZone: http://eureka7001.com:7001/eureka/

  2、在本地新建microservicecloud-config-dept-client.yml:

spring:
 profiles:
  active:
  - dev
---
server:
 port: 8001 
spring:
 profiles: dev
 application:
  name: microservicecloud-config-eureka-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: 123456
  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.atguigu.springcloud.entities
 mapper-locations:
 - classpath:mybatis/mapper/**/*.xml
 
eureka:
 client:
  service-url:
   defaultZone: http://eureka7001.com:7001/eureka
 instance:
  instance-id: dept-8001.com
  prefer-ip-address: true
info:
 app.name: atguigu-microservicecloud-springcloudconfig01
 company.name: www.atguigu.com
 build.artifactId: $project.artifactId$
 build.version: $project.version$
---
server:
 port: 8001 
spring:
 profiles: test
 application:
  name: microservicecloud-config-eureka-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: 123456
  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.atguigu.springcloud.entities
 mapper-locations:
 - classpath:mybatis/mapper/**/*.xml
 
eureka:
 client:
  service-url:
   defaultZone: http://eureka7001.com:7001/eureka
 instance:
  instance-id: dept-8001.com
  prefer-ip-address: true
info:
 app.name: atguigu-microservicecloud-springcloudconfig02
 company.name: www.atguigu.com
 build.artifactId: $project.artifactId$
 build.version: $project.version$

   dev和test环境连接的数据库不同,app.name不同,其他是一样的。
  3、将上面新建的两个文件上传至GitHub:
在这里插入图片描述
  4、新建工程模块microservicecloud-config-eureka-client-7001,作为配置版的eureka
   pom.xml:

<dependencies>
	<!-- SpringCloudConfig的客户端 -->
	<dependency>
		<groupId>org.springframework.cloud</groupId>
		<artifactId>spring-cloud-starter-config</artifactId>
	</dependency>
	<dependency>
		<groupId>org.springframework.cloud</groupId>
		<artifactId>spring-cloud-starter-eureka-server</artifactId>
	</dependency>
	<!-- 热部署插件 -->
	<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>springloaded</artifactId>
	</dependency>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-devtools</artifactId>
	</dependency>
</dependencies>

   bootstrap.yml:

spring: 
  cloud: 
    config: 
      name: microservicecloud-config-eureka-client     #需要从github上读取的资源名称
      profile: dev 
      label: master 
      uri: http://localhost:3344      #SpringCloudConfig获取的服务地址

   application.yml:

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

   主启动类:

/**
 * EurekaServer服务器端启动类,接受其它微服务注册进来
 */
@SpringBootApplication
@EnableEurekaServer
public class Config_Git_EurekaServerApplication {
	public static void main(String[] args) {
		SpringApplication.run(Config_Git_EurekaServerApplication.class, args);
	}
}

  5、启动测试:先启动3344Config服务端,再启动microservicecloud-config-eureka-client-7001,访问http://eureka7001.com:7001/
在这里插入图片描述
 Ⅱ、搭建Config的客户端Dept微服务
  1、新建工程模块microservicecloud-config-dept-client-8001
   pom.xml:

<dependencies>
	<!-- SpringCloudConfig配置 -->
	<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>
	<dependency>
		<groupId>org.springframework.cloud</groupId>
		<artifactId>spring-cloud-starter-eureka</artifactId>
	</dependency>
	<dependency>
		<groupId>com.atguigu.springcloud</groupId>
		<artifactId>microservicecloud-api</artifactId>
		<version>${project.version}</version>
	</dependency>
	<dependency>
		<groupId>junit</groupId>
		<artifactId>junit</artifactId>
	</dependency>
	<dependency>
		<groupId>mysql</groupId>
		<artifactId>mysql-connector-java</artifactId>
	</dependency>
	<dependency>
		<groupId>com.alibaba</groupId>
		<artifactId>druid</artifactId>
	</dependency>
	<dependency>
		<groupId>ch.qos.logback</groupId>
		<artifactId>logback-core</artifactId>
	</dependency>
	<dependency>
		<groupId>org.mybatis.spring.boot</groupId>
		<artifactId>mybatis-spring-boot-starter</artifactId>
	</dependency>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-jetty</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>
	</dependency>
	<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>springloaded</artifactId>
	</dependency>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-devtools</artifactId>
	</dependency>
</dependencies>

   bootstrap.yml:

spring:
  cloud:
    config:
      name: microservicecloud-config-dept-client #需要从github上读取的资源名称
      #profile配置是什么就取什么配置dev or test
      profile: dev
      #profile: test
      label: master
      uri: http://localhost:3344  #SpringCloudConfig的服务地址

   application.yml:

spring:
  application:
    name: microservicecloud-config-dept-client

   业务代码:将microservicecloud-provider-dept-8001模块的所有业务代码(包括Controller、Service、Dao等)和mapper配置文件拷贝过来
   主启动类:

@SpringBootApplication
@EnableEurekaClient //本服务启动后会自动注册进eureka服务中
@EnableDiscoveryClient //服务发现
public class DeptProvider8001_Config_App
{
	public static void main(String[] args)
	{
		SpringApplication.run(DeptProvider8001_Config_App.class, args);
	}
}

  2、启动测试:启动3344和配置版的7001,通过修改配置版的8001的bootstrap.yml的profile属性的值可以观察到数据库的切换
在这里插入图片描述

 总结:实现了eureka服务和该服务提供者都通过3344从GitHub获取配置信息
TIP:在开发和生产中,我们也可以使用Nacos作为配置中心,阿里云也提供了Nacos相关的服务。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值