(二)Spring Cloud实践:使用Spring Cloud Config实现分布式配置管理

Spring Cloud Config为微服务应用提供了统一的分布式配置管理,将配置文件放到git上,所有的微服务应用均从git上获取这些配置文件。  该种情况下,如果将配置文件放在第三方提供的版本控制器上,需要网络可访问,另外,也可以自己搭建gitlab私服,来存放自己的 配置文件(这种可能更好一些)。

 模块4 configserver

完整的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.news</groupId>
	<artifactId>configserver</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>configserver</name>
	<description>spring config</description>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.6.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>Finchley.SR2</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>


</project>

修改configserver启动类,添加注释@EnableConfigServer,表明这是一个Config  Server,添加注释@EnableEurekaClient 将其注册到服务中心。具体代码如下:

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;

/**
 *@Description:
 *@Author: Young
 *@@CreateDate:
 */
@SpringBootApplication
@EnableConfigServer
@EnableEurekaClient
public class ConfigserverApplication {

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

修改application.yml配置,具体内容如下:

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8080/eureka/  #Eureka Server服务器
server:
  port: 8090

spring:
  cloud:
    config:
      server:
        git:
          uri: http://192.168.56.109:9999/avatar/config.git #配置git仓库的地址
          searchPaths: config  #git仓库地址下的相对地址,可以配置多个,用,分割。
          username: root
          password: centos1234
  application:
    name: config-server

该模块正常启动前提:git创库能够正常访问,并且searchPaths存在。启动后,就可以看到该服务注册到了服务中心。

在config项目下创建config-client-dev.yml文件,将test=config test写入文件。在浏览器输入:http://localhost:8090/config-client-dev.yml,显示如下:

Config Client端的配置

这里需要修改一下模块2 eurekaclient,添加spring-cloud-starter-config依赖,完整的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.news</groupId>
	<artifactId>eurekaclient</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>eurekaclient</name>
	<description>学习Spring Cloud微服务框架</description>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.6.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>Finchley.SR2</spring-cloud.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-actuator</artifactId>
		</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-starter-config</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>


</project>

修改配置文件,Config client需要一个名为bootstrap.yml的配置文件,用来配置config Server的URL和要访问的git具体分支。

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8080/eureka/
spring:
  cloud:
    config:
      label: master  #默认git分支
      profile: client-dev #文件名字的后半部分,一般用来区分开发环境和正式环境
      #uri: http://localhost:8090/  #通过uri指定config服务端
      discovery: #分布式环境下自动发现配置服务
         enabled: true
         serviceId: config-server
      name: config #配置文件名字的前半部分,可以自定义

依据bootstrap.yml文件中的配置,在gitlab上配置文件config-client-dev.yml的路径为/avator/config/config-client-dev.yml。

修改HelloController,代码如下:

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

/**
 * @program: springlearn
 * @description: 学习EurekaClient
 * @author: Mr.Young
 * @create: 2018-11-14 11:13
 **/
@RestController
public class HelloController {
    @RequestMapping("/")
    public String home() {
        return "hello! This is Spring Cloud Eureka Client!";
    }

    @Value("${test}")
    String confighello;

    @RequestMapping(value = "/configtest")
    public String configtest(){
        return confighello;
    }
}

通过@Value可以读取gitlab服务器上配置文件中对应标签的值。在浏览器 输入:http://localhost:8081/configtest,如下图所示:

配置自动更新

配置自动更新目的是在更改配置文件后,不重启正在运行的微服务的情况下,让各个微服务感知到配置文件内容的变化。

在上面的实例中,手动更改config-client-dev.yml文件内容后,比如将test=config test修改为test=config test 2。 在浏览器输入:http://localhost:8081/configtest,结果并没有变化。

Spring cloud通过在客户端提交POST请求,可以刷新配置内容,让微服务可以感知到配置文件中修改的内容。

下面来修改模块2 eurekaclient,让其能够获取到配置文件内容的更新。

首先,在pom.xml中添加依赖项spring-boot-starter-actuator,该模块提供了一套监控的功能,可监控程序的运行时状态。

其次,修改HelloController.java文件,在主类上添加注解@RefreshScope,修改后的文件内容如下:

package com.news.eurekaclient;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @program: springlearn
 * @description: 学习EurekaClient
 * @author: Mr.Young
 * @create: 2018-11-14 11:13
 **/
@RestController
@RefreshScope
public class HelloController {
    @RequestMapping("/")
    public String home() {
        return "hello! This is Spring Cloud Eureka Client!";
    }

    @Value("${test}")
    String confighello;

    @RequestMapping(value = "/configtest")
    public String configtest(){
        return confighello;
    }
}

通过postman模拟发送post 请求:http://localhost:8081/actuator/refresh(查到很多参考资料中,都写http://localhost:8081/refresh,呵呵),贴个图吧!

表示变量test的值,已经 刷新。

在浏览器输入:http://localhost:8081/configtest,结果如下图:

哈哈,写博客还是有帮助的,之前“配置自动更新”一直没有调通,又查了一些技术文档,加上猜测,终于可以了。另外,还有人提到通过Webhook自动触发配置更新, 由于本人也不是专业搞 web开发的,没有进行实际的使用,感兴趣的小伙伴可以试试。

又完成一篇,一天过去了大半, 总结真是比较耗时间!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值