SpringCloud浅尝(五)——Config

Spring Cloud Config为分布式系统中的外部配置提供服务器和客户端支持。使用Config Server,您可以在所有环境中管理应用程序的外部属性。客户端和服务器上的概念映射与Spring EnvironmentPropertySource抽象相同,因此它们与Spring应用程序非常契合,但可以与任何以任何语言运行的应用程序一起使用。随着应用程序通过从开发人员到测试和生产的部署流程,您可以管理这些环境之间的配置,并确定应用程序具有迁移时需要运行的一切。服务器存储后端的默认实现使用git,因此它轻松支持标签版本的配置环境,以及可以访问用于管理内容的各种工具。很容易添加替代实现,并使用Spring配置将其插入。

我们先建一个ConfigServer项目,引入 spring-cloud-config-server,spring-cloud-starter-netflix-eureka-client

plugins {
	id 'org.springframework.boot' version '2.1.3.RELEASE'
	id 'java'
}

apply plugin: 'io.spring.dependency-management'

group = 'com.sl'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'

repositories {
	mavenCentral()
}

ext {
	set('springCloudVersion', 'Greenwich.SR1')
}

dependencies {
	implementation 'org.springframework.cloud:spring-cloud-config-server'
	implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client'
	testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

dependencyManagement {
	imports {
		mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
	}
}

EurekaClient前面我们已经知道怎么使用了,添加@EnableEurekaClient注解,ConfigServer也只需添加@EnableConfigServer注解

@SpringBootApplication
@EnableConfigServer
@EnableEurekaClient
public class ConfigServerApplication {

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

}

下面我们需要配置一下ConfigServer,ConfigServer服务器存储后端的默认实现使用git。我们先创建一个git仓库,本次测试使用的是码云,我在上面创建了一个仓库configrepo,并新建了两个文件。这两个文件就是我们前面eurekdiscovery的两台实例的配置文件。

application.yml

spring:
  application:
    name: configserver
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/tonse/configrepo.git  # 远程git仓库的地址
          username: 相应的账户名
          password: 密码
server:
  port: 8070
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8081/eureka/,http://localhost:8082/eureka/,http://localhost:8083/eureka/

启动ConfigServer实例,我们下面可以测试一下

configserver具有下面格式的资源

/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties

http://localhost:8070/eurekadiscovery/dis02

http://localhost:8070/eurekadiscovery-dis02.yml

其他的大家可以试一试!如果访问.properties、.json格式的,还会自动进行转换。

注:如果配置文件的内容格式有问题的话,访问会报500错误。我们可以利用这个特性,来检查我们的配置文件是否正确

ConfigServer也属于是一个微服务,所以让其高可用很简单,只需要启动多个服务实例即可,并注册到我们前面的EurekaServer实例上。

Config Client

现在ConfigServer我们也已经启动起来了,我们把之前的EurekDiscover项目的配置改一下,让他使用ConfigServer上面的配置。

我们先把EurekDiscover项目引入spring-cloud-config-client.jar。

application.yml重命名为bootstrap.yml。因为启动SpringBoot项目时,会优先读取bootstrap.yml里的配置,然后才会读取application.yml。bootstrap.yml修改为。

spring:
  profiles: dis01
  application:
    name: eurekadiscovery
  cloud:
    config:
      discovery:
        enabled: true
        service-id: configserver  # 注册中心的服务名
        profile: dis01  # 指定配置文件的环境
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8081/eureka/,http://localhost:8082/eureka/,http://localhost:8083/eureka/

---

spring:
  profiles: dis02
  application:
    name: eurekadiscovery
  cloud:
    config:
      discovery:
        enabled: true
        service-id: configserver  # 注册中心的服务名
        profile: dis02  # 指定配置文件的环境
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8081/eureka/,http://localhost:8082/eureka/,http://localhost:8083/eureka/

我们启动一下实例。已经成功注册了上去,使用的端口就是git的配置文件上的端口。日志上也可以看出,实例启动时,从ConfigServer上读到了配置文件。

现在我们已经成功拉取了相应的配置文件。但是这样仍然不够,因为还不能做到自动刷新配置文件,git上更改了配置文件,还需要重启服务才能够读取到最新的配置。或者每台实例提供个刷新的接口我们一每一台实例进行push刷新,当然这是我们不能接受受的。后面我们将使用Spring Cloud Bus实现自动刷新配置。

猜你喜欢

转载自blog.csdn.net/tonsesy/article/details/88666532