标签:points reporting second 商业 tst 更改 lan dev localhost
Spring Cloud Config 可以为微服务架构中的应用提供集中化的外部配置支持,它分为服务端和客户端两个部分 。
Spring Cloud Config 分为服务端和客户端两个部分。服务端被称为分布式配置中心,它是个独立的应用,可以从配置仓库获取配置信息并提供给客户端使用。客户端可以通过配置中心来获取配置信息,在启动时加载配置。Spring Cloud Config 的配置中心默认采用Git来存储配置信息,所以天然就支持配置信息的版本管理,并且可以使用Git客户端来方便地管理和访问配置信息。
由于Spring Cloud Config 需要一个存储配置信息的Git仓库,这里我们先在Git仓库中添加好配置文件再演示其功能
config-dev.yml:
config:
info: "config info for dev(master)"
复制代码
config-test.yml:
config:
info: "config info for test(master)"
复制代码
config-prod.yml:
config:
info: "config info for prod(master)"
复制代码
config-dev.yml:
config:
info: "config info for dev(dev)"
复制代码
config-test.yml:
config:
info: "config info for test(dev)"
复制代码
config-prod.yml:
config:
info: "config info for prod(dev)"
复制代码
这里我们创建一个config-server模块来演示Spring Cloud Config 作为配置中心的功能。
pom.xml文件
<?xml version="1.0" encoding="UTF-8"?> <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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.2.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.reno</groupId> <artifactId>springcloud.config.server</artifactId> <version>1.0</version> <name>springcloud.config.server</name> <description>Demo project for Spring Boot</description> ? ? <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> <spring-cloud.version>Hoxton.RELEASE</spring-cloud.version> </properties> ? ? <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> ? <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> ? <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> ? <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-netflix-eureka-server</artifactId> <version>2.2.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> ? <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> ? <dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-all</artifactId> <version>4.6.3</version> </dependency> ? </dependencies> ? ? <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> ? ? <repositories> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/milestone</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> ? ? <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> ? </project> ?
yml文件
server:
port: 38901
spring:
application:
name: config-server
cloud:
config:
server:
git: #配置存储配置信息的Git仓库
uri: https://gitee.com/reno/springcloud-config.git
username: 20@126.com
password: 23456
clone-on-start: true #开启启动时直接从git获取配置
# search-paths: ‘{application}‘
security: #配置用户名和密码
user:
name: test
password: 123456
eureka:
instance:
hostname: 192.16.10.208
#hostname: localhost
#设置是否将自己作为客户端注册到注册中心(缺省true)
#这里为不需要,查看@EnableEurekaServer注解的源码,会发现它间接用到了@EnableDiscoveryClient
instance-id: configServerService-${spring.cloud.client.ipaddress}-${server.port}
client:
register-with-eureka: true
fetch-registry: true
service-url:
defaultZone: http://192.16.10.208:38761/eureka/
registry-fetch-interval-seconds: 5
instance-info-replication-interval-seconds: 10
在启动类上添加@EnableConfigServer注解来启用配置中心功能
这里我们通过config-server来演示下如何获取配置信息。
# 获取配置信息 /{label}/{application}-{profile} # 获取配置文件信息 /{label}/{application}-{profile}.yml 复制代码
application:代表应用名称,默认为配置文件中的spring.application.name,如果配置了spring.cloud.config.name,则为该名称;
label:代表分支名称,对应配置文件中的spring.cloud.config.label;
profile:代表环境名称,对应配置文件中的spring.cloud.config.profile。
启动eureka-server、config-server服务;
访问http://localhost:38901/master/config-dev来获取master分支上dev环境的配置信息;
访问http://localhost:38901/master/config-dev.yml来获取master分支上dev环境的配置文件信息,对比上面信息,可以看出配置信息和配置文件信息并不是同一个概念;
我们创建一个config-client模块来从config-server获取配置。
pom文件
<?xml version="1.0" encoding="UTF-8"?> <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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.2.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.reno</groupId> <artifactId>springcloud.config.client</artifactId> <version>1.0</version> <name>springcloud.config.client</name> <description>Demo project for Spring Boot</description> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> <spring-cloud.version>Hoxton.RELEASE</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-netflix-eureka-server</artifactId> <version>2.2.0.RELEASE</version> </dependency> <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>cn.hutool</groupId> <artifactId>hutool-all</artifactId> <version>4.6.3</version> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <repositories> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/milestone</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
yml文件
server:
port: 39001
spring:
application:
name: config-client
cloud:
config: #Config客户端配置
profile: dev #启用配置后缀名称
label: dev #分支名称
uri: http://localhost:38901 #配置中心地址
name: config #配置文件名称
username: test
password: 123456
eureka:
instance:
hostname: 192.16.10.208
#hostname: localhost
#设置是否将自己作为客户端注册到注册中心(缺省true)
#这里为不需要,查看@EnableEurekaServer注解的源码,会发现它间接用到了@EnableDiscoveryClient
instance-id: configServerService-${spring.cloud.client.ipaddress}-${server.port}
client:
register-with-eureka: true
fetch-registry: true
service-url:
defaultZone: http://192.16.10.208:38761/eureka/
registry-fetch-interval-seconds: 5
instance-info-replication-interval-seconds: 10
management:
endpoints:
web:
exposure:
include: ‘refresh‘
启动config-client服务;
访问http://localhost:39001/configInfo,可以获取到dev分支下dev环境的配置;
config info for dev(dev)
我们不仅可以把每个项目的配置放在不同的Git仓库存储,也可以在一个Git仓库中存储多个项目的配置,此时就会用到在子目录中搜索配置信息的配置。
首先我们需要在config-server中添加相关配置,用于搜索子目录中的配置,这里我们用到了application占位符,表示对于不同的应用,我们从对应应用名称的子目录中搜索配置,比如config子目录中的配置对应config应用;
spring: cloud: config: server: git: search-paths: ‘{application}‘
访问http://localhost:39001/configInfo进行测试,可以发现获取的是config子目录下的配置信息。
config info for config dir dev(dev)
当Git仓库中的配置信息更改后,我们可以通过SpringBoot Actuator的refresh端点来刷新客户端配置信息,以下更改都需要在config-client中进行。
在pom.xml中添加Actuator的依赖:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> 复制代码
在bootstrap.yml中开启refresh端点:
management: endpoints: web: exposure: include: ‘refresh‘
本文参考自MacroZheng链接 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
标签:points reporting second 商业 tst 更改 lan dev localhost
原文地址:https://www.cnblogs.com/reno2020/p/14398997.html