侧边栏壁纸
博主头像
落叶人生博主等级

走进秋风,寻找秋天的落叶

  • 累计撰写 130562 篇文章
  • 累计创建 28 个标签
  • 累计收到 9 条评论
标签搜索

目 录CONTENT

文章目录

Spring Cloud Config 基础示例

2023-12-10 星期日 / 0 评论 / 0 点赞 / 32 阅读 / 13882 字

Spring Cloud Config 简介什么是Srping Cloud Config?Spring Cloud Config 是一种分布式配置中心框架, 为分布式系统中的外部化配置提供服务器和客户

Spring Cloud Config 简介

什么是Srping Cloud Config?

  • Spring Cloud Config 是一种分布式配置中心框架, 为分布式系统中的外部化配置提供服务器和客户端支持。(同类技术还有vaultzookeeperConsul)
  • 使用Config Server,可以在所有环境中管理应用程序的外部属性。客户端和服务器上的概念映射与Spring Environment和PropertySource抽象,因此它们非常适合Spring应用程序,但可以与任何语言运行的任何应用程序一起使用。当应用程序通过部署管道从开发到测试并进入生产时,您可以管理这些环境之间的配置,并确保应用程序具有迁移时需要运行的所有内容。服务器存储后端的默认实现使用git,因此它可以轻松支持配置环境的标记版本,以及可用于管理内容的各种工具。
  • Spring Cloud Config也主要由两部分组成:
  • Config-Server: 用于配置外部的资源文件,支持对属性值进行加解密
  • Config-client:绑定到config server使用远程配置文件初始化spring,支持对属性值进行加解密

本文示例说明

  • 为了更直观的理解spring cloud config,本文通过server获取整个配置,效果上与通过配置springboot的active 来切换环境一致;通过接口查询数据库以查看效果;
  • 采用高可用架构;此处使用eureka去做服务注册发现(暂时也只会这个。。),eureka配置说明可以查看springCloud之eureka

新建ConfigServer

为了减少文章长度,尽量干货,创建过程就不截说明了,建议使用Intellij idea进行创建;

  • 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 http://maven.apache.org/xsd/maven-4.0.0.xsd">	<modelVersion>4.0.0</modelVersion>	<groupId>com.lc.springcloud</groupId>	<artifactId>config-server</artifactId>	<version>0.0.1-SNAPSHOT</version>	<packaging>jar</packaging>	<name>config-server</name>	<description>Demo project for Spring Boot</description>	<parent>		<groupId>org.springframework.boot</groupId>		<artifactId>spring-boot-starter-parent</artifactId>		<version>2.1.0.RELEASE</version>		<relativePath/> <!-- lookup parent from repository -->	</parent>	<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>Greenwich.M2</spring-cloud.version>	</properties>	<dependencies>		<dependency>			<groupId>org.springframework.cloud</groupId>			<artifactId>spring-cloud-config-server</artifactId>		</dependency>		<dependency>			<groupId>org.springframework.cloud</groupId>			<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>		</dependency>		<dependency>			<groupId>org.springframework.boot</groupId>			<artifactId>spring-boot-starter-test</artifactId>			<scope>test</scope>		</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>	<build>		<plugins>			<plugin>				<groupId>org.springframework.boot</groupId>				<artifactId>spring-boot-maven-plugin</artifactId>			</plugin>		</plugins>	</build>	<repositories>		<repository>			<id>spring-milestones</id>			<name>Spring Milestones</name>			<url>https://repo.spring.io/milestone</url>			<snapshots>				<enabled>false</enabled>			</snapshots>		</repository>	</repositories></project>
  • application.yml配置如
server:	port: 8100spring:	application:		name: config-server	cloud:    	config:			server:        		git:          		 uri: https://github.com/lvchaogit/SpringCloudeureka:   client:    service-url:      defaultZone: http://localhost:8081/eureka/ # 服务注册中心地址

通过配置不难理解,该配置是从git上获取配置,更多配置后续详解;

  • application.java
@SpringBootApplication@EnableConfigServer@EnableDiscoveryClientpublic class ConfigServerApplication {	public static void main(String[] args) {		SpringApplication.run(ConfigServerApplication.class, args);	}}

主要是加上** @EnableConfigServer**注解,开启configserver功能

搭建 Config Client

  • 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 http://maven.apache.org/xsd/maven-4.0.0.xsd">	<modelVersion>4.0.0</modelVersion>	<groupId>com.lc.springcloud</groupId>	<artifactId>config-client</artifactId>	<version>0.0.1-SNAPSHOT</version>	<packaging>jar</packaging>	<name>config-client</name>	<description>Demo project for Spring Boot</description>	<parent>		<groupId>org.springframework.boot</groupId>		<artifactId>spring-boot-starter-parent</artifactId>		<version>2.1.0.RELEASE</version>		<relativePath/> <!-- lookup parent from repository -->	</parent>	<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>Greenwich.M2</spring-cloud.version>	</properties>	<dependencies>		<!-- config starter begin-->		<dependency>			<groupId>org.springframework.cloud</groupId>			<artifactId>spring-cloud-starter-config</artifactId>		</dependency>		<!-- config starter end-->		<dependency>			<groupId>org.springframework.cloud</groupId>			<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>		</dependency>		<dependency>			<groupId>org.springframework.boot</groupId>			<artifactId>spring-boot-starter-web</artifactId>		</dependency>		<dependency>			<groupId>mysql</groupId>			<artifactId>mysql-connector-java</artifactId>		</dependency>		<dependency>			<groupId>org.apache.commons</groupId>			<artifactId>commons-pool2</artifactId>			<version>2.5.0</version>		</dependency>		<dependency>			<groupId>com.alibaba</groupId>			<artifactId>druid-spring-boot-starter</artifactId>			<version>1.1.10</version>		</dependency>		<!-- mybatis-plus begin -->		<dependency>			<groupId>com.baomidou</groupId>			<artifactId>mybatis-plus-boot-starter</artifactId>			<version>2.1.9</version>			<exclusions>				<exclusion>					<artifactId>tomcat-jdbc</artifactId>					<groupId>org.apache.tomcat</groupId>				</exclusion>			</exclusions>		</dependency>		<!--config监控模块  -->		<dependency>			<groupId>org.springframework.boot</groupId>			<artifactId>spring-boot-starter-actuator</artifactId>		</dependency>		<dependency>			<groupId>org.springframework.boot</groupId>			<artifactId>spring-boot-starter-test</artifactId>			<scope>test</scope>		</dependency>		<dependency>			<groupId>log4j</groupId>			<artifactId>log4j</artifactId>			<version>1.2.13</version>			<scope>runtime</scope>		</dependency>		<dependency>			<groupId>org.projectlombok</groupId>			<artifactId>lombok</artifactId>		</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>	<build>		<plugins>			<plugin>				<groupId>org.springframework.boot</groupId>				<artifactId>spring-boot-maven-plugin</artifactId>			</plugin>		</plugins>	</build>	<repositories>		<repository>			<id>spring-milestones</id>			<name>Spring Milestones</name>			<url>https://repo.spring.io/milestone</url>			<snapshots>				<enabled>false</enabled>			</snapshots>		</repository>	</repositories></project>
  • bootstrap.yml
spring:  application:    name: config-client  cloud:    config:      profile: dev      label: master      discovery:        enabled: true        service-id: config-serverserver:  port: 2001eureka:  client:      register-with-eureka: false     #因此处只是消费,不提供服务,所以不需要向eureka server注册      service-url:        defaultZone: http://localhost:8081/eureka/ # 服务注册中心地址
  • 首先注意配置文件名称为:bootstartp.yml,并不是~~application.yml~~
  • 通过配置discovery,并设置enabled为true,使client通过服务发现去获取server,server-id为注册中心里配置的服务名称
  • label=git的标签;profile=配置文件版本(类似于spring boot中的active)

常用configserver配置

采用URI占位符

spring:  cloud:    config:      server:        git:          uri: https://github.com/lvchaogit/{application}
  • 使用{application} 和 {profile}(如果使用{label},请记住它是使用在git标签中的)。因此你可以轻松的支持“一个应用一个仓库”的原则

模式匹配和多仓库

spring:  cloud:    config:      server:        git:          uri: https://github.com/lvchaogit/SpringCloud          repos:            simple: https://github.com/simple/config-repo            special:              pattern: special*/dev*,*special*/dev*              uri: https://github.com/special/config-repo            local:              pattern: local*              uri: file:/home/configsvc/config-repo
  • 在{application}和{profile}参数中使用模式匹配可以支持更多复杂的需求。模式的格式是一组逗号分隔的{application}/{profile},其中的参数可以使用通配符.
  • 如果{application}/{profile}没有匹配到任何模式,它将使用默认的仓库地址:spring.cloud.config.server.git.uri。上面的例子中,"simple"仓库匹配的是“simple/”(它仅仅匹配一个仓库simple,在所有的环境下)。"local"仓库将匹配所有{application}的名字以“local”开头的,并且也是在所有的环境下。“/”前缀自动添加到所有没有设置{profile}的模式中。

匹配仓库子目录

spring:  cloud:    config:      server:        git:          uri: https://github.com/spring-cloud-samples/config-repo          searchPaths: foo,bar*
  • 在foo和以bar开头的目录中,搜索配置文件。

克隆远程的仓库

spring:  cloud:    config:      server:        git:          uri: https://git/common/config-repo.git          repos:            team-a:                pattern: team-a-*                cloneOnStart: true                uri: http://git/team-a/config-repo.git            team-b:                pattern: team-b-*                cloneOnStart: false                uri: http://git/team-b/config-repo.git            team-c:                pattern: team-c-*                uri: http://git/team-a/config-repo.git
  • 服务器默认在第一次请求配置文件时克隆远程的仓库,也可以配置在启动的时候克隆仓库
  • team-a的仓库将在服务端启动时进行克隆,其他的仓库将在第一次请求时克隆。

仓库认证

spring:  cloud:    config:      server:        git:          uri: https://github.com/spring-cloud-samples/config-repo          username: trolley          password: strongpassword
  • 输入用户名密码

常用配置参考:configServer常用配置

文中示例代码:SpringCloudConfig 示例

广告 广告

评论区