第五篇: 分布式配置中心(Spring Cloud Config)

在上一篇文章讲述zuul的时候,已经提到过,使用配置服

一、简介

在分布式系统中,由于服务数量巨多,为了方便服务配置文件统一管理,实时更新,所以需要分布式配置中心组件。在Spring Cloud中,有分布式配置中心组件spring cloud config ,它支持配置服务放在配置服务的内存中(即本地),也支持放在远程Git仓库中。在spring cloud config 组件中,分两个角色,一是config server,二是config client。

务来保存各个服务的配置文件。它就是Spring Cloud Config。

二、构建Config Server

继承之前第一篇所建立的parent-pom,创建一个spring-boot项目,取名为leopard-config,其pom.xml如下:

<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.leopard</groupId>
		<artifactId>leopard-parent</artifactId>
		<version>0.0.1-SNAPSHOT</version>
	</parent>
	<groupId>com.leopard</groupId>
	<artifactId>leopard-config</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>
	<name>leopard-config</name>
	<url>http://maven.apache.org</url>


	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>3.8.1</version>
			<scope>test</scope>
		</dependency>

		<!-- eureka -->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-eureka</artifactId>
		</dependency>

		<!-- 上边引入 parent,因此 下边无需指定版本 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<!-- config -->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-config-server</artifactId>
		</dependency>

	</dependencies>

	

</project>

在程序的入口Application类加上@EnableConfigServer注解开启配置服务器的功能,代码如下:

package com.leopard.config;

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

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

此处使用 @EnableEurekaClient 将配置服务注册到服务中心,也可以不引入,配置服务可以单独运行。

需要在程序的配置文件application.properties文件配置以下:

#spring.application.name=leopard-config
#server.port=8881
#配置git仓库地址
#spring.cloud.config.server.git.uri=
#配置仓库路径
#spring.cloud.config.server.git.searchPaths=
#配置仓库的分支
#spring.cloud.config.label=master
#访问git仓库的用户名
#spring.cloud.config.server.git.username=
#访问git仓库的密码
#spring.cloud.config.server.git.password=

#不读取线上git配置,直接获取本地配置
spring.profiles.active=native
spring.cloud.config.server.native.searchLocations=classpath:/config

一般是连接线上git / gitlab 服务的配置路径,不过此处采用本地引用,而且因为个人喜好,端口-服务名比较习惯写在application.yml上,配置如下:

spring:
  application:
    name: leopard-config

server:
  port: 8881

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
      

不习惯的朋友也可以将两份合在一起,个人喜好把固定不变的配置在 yml文件,需要添加或是改动的配置放在 properties文件。

接着,因为我们指定访问的本地目录在 classpath:/config 下, 所以在config文件夹下创建一个文件 leopard-service-dev.properties ,内容如下:

config.name=leopardName

因为有引入 服务中心 eureka,所以先启动 leopard-eureka , 后启动 leopard-config 。

然后我们在浏览器访问链接: http://localhost:8881/leopard-service/dev

{"name":"leopard-service","profiles":["dev"],"label":null,"version":null,"state":null,"propertySources":[{"name":"classpath:/config/leopard-service-dev.properties","source":{"config.name":"leopardName"}}]}

证明配置服务中心可以从远程程序获取配置信息。

http请求地址和资源文件映射如下:

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

三、构建config client 进行访问

在第一篇已经创建了一个基础服务  leopard-service ,在pom 中加入配置client 依赖

        <!-- config client -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>

在 resources 资源文件夹下 创建 bootstrap.properties 配置如下:

#服务名称
spring.application.name=leopard-service

#指明远程仓库的分支
spring.cloud.config.label=master

#环境配置文件 dev-开发 test-测试 pro-正式
spring.cloud.config.profile=dev

#指明配置服务中心的网址
spring.cloud.config.uri= http://localhost:8881/

因为配置文件会优先读取 bootstrap.properties 信息(即bootstrap和application有相同的参数名存在,只读取bootstrap内的参数),所以如果存在application.yml 和 bootstrap.properties 尽量避免参数冲突,引起参数读取覆盖。

在 TestController 中加入测试代码:

	@Value("${config.name}")
	String name;
	
	/**
	 * 获取信息
	 * @return
	 */
	@RequestMapping(value="/getName" , method = RequestMethod.GET)
	public String getName(){
		return "hi leopard,this name  is :" + name;
	}

启动 service 服务进行访问  : http://localhost:8081/test/getName

hi leopard,this name is :leopardName

这就说明,leopard-service从leopard-config 获取了config.name的属性,而leopard-config是从本地的配置仓库读取的。

文章转载: https://blog.csdn.net/forezp/article/details/81041028

猜你喜欢

转载自blog.csdn.net/u014799292/article/details/81489520
今日推荐