Spring Cloud Config介绍+案例

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zhou920786312/article/details/85015355

测试中使用到的代码到在这里https://download.csdn.net/download/zhou920786312/10853300

介绍

是一个解决分布式系统的配置管理方案

包含两个部分

  • server提供配置文件的存储、以接口的形式将配置文件的内容提供出去,
  • client通过接口获取数据、并依据此数据初始化自己的应用。

Spring cloud使用git或svn存放配置文件,默认情况下使用git

一个配置中心提供的核心功能

  • 提供服务端和客户端支持
  • 集中管理各环境的配置文件
  • 配置文件修改之后,可以快速的生效
  • 可以进行版本管理
  • 支持大的并发查询
  • 支持各种语言

图解

案例

步骤

1在github上创建一个仓库microservicecloud-config,

得到[email protected]:zhou920786312/microservicecloud-config.git

2复制仓库到本地

 git clone [email protected]:zhou920786312/microservicecloud-config.git

3将配置文件application.yml上传到远程仓库

application.yml

spring: 
 profiles:
    active:
      - dev

---
spring: 
  profiles: dev  #开发环境
  application: 
    name: microservicecloud-config-dev
    
---
spring: 
  profiles: test  #测试环境
  application: 
    name: microservicecloud-config-test

$ git status
 
$ git add .
 
$ git commit -m "init file"
 
$ git push origin master

4创建microservicecloud-config-3344(相当于第一幅图的config-server)

<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>

	<parent>
		<groupId>com.atguigu.springcloud</groupId>
		<artifactId>microservicecloud</artifactId>
		<version>0.0.1-SNAPSHOT</version>
	</parent>

	<artifactId>microservicecloud-config-3344</artifactId>


	<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>

</project>

server: 
  port: 3344 
  
spring:
  application:
    name:  microservicecloud-config
  cloud:
    config:
      server:
        git:
          uri: [email protected]:zhou920786312/microservicecloud-config.git #GitHub上面的git仓库名字

package com.atguigu.springcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;

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

host添加

127.0.0.1       config-3344.com

启动microservicecloud-config-3344测试

http://config-3344.com:3344/application-dev.yml

读取文件的方式

5将配置文件microservicecloud-config-client.yml上传到远程仓库

spring: 
 profiles:
    active:
      - dev

---
server: 
 port: 8201
spring: 
  profiles: dev   
  application: 
    name: microservicecloud-config-client 
    
eureka:
  client: #客户端注册进eureka服务列表内
    service-url: 
      defaultZone: http://eureka-dev.com:7001/eureka/

---
server: 
 port: 8202
spring: 
  profiles: test   
  application: 
    name: microservicecloud-config-client 
    
eureka:
  client: #客户端注册进eureka服务列表内
    service-url: 
      defaultZone: http://eureka-test.com:7001/eureka/

6创建microservicecloud-config-client-3355(相当于第一幅图的客户端)

<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>

	<parent>
		<groupId>com.atguigu.springcloud</groupId>
		<artifactId>microservicecloud</artifactId>
		<version>0.0.1-SNAPSHOT</version>
	</parent>

	<artifactId>microservicecloud-config-client-3355</artifactId>


	<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>
</project>
 

spring:
  application:
    name: microservicecloud-config-client

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

#上面的功能是:通过uri获取配置中心的所有内容,通过name获取具体的内容,通过profile获取哪个环境的内容

Host配置

127.0.0.1       client-config.com

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

package com.atguigu.springcloud.rest;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@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()
	{
		return "服务名称: " + applicationName + "---- eureka服务名称:" + eurekaServers + "---- 端口: " + port;
	}
}

测试

启动3344

http://config-3344.com:3344/microservicecloud-config-client-test.yml

启动3355

http://client-config.com:8202/config

注意:如果需要改配置,只要bootstrap.yml改

测试

猜你喜欢

转载自blog.csdn.net/zhou920786312/article/details/85015355