Spring Cloud(Finchley.RELEASE版本)微服务学习实践:7.1分布式配置中心

本文主要是介绍Spring Cloud(Finchley.RELEASE版本)微服务学习实践:7.1分布式配置中心,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

环境:

jdk1.8;spring boot2.0.3;spring cloud(Finchley.RELEASE版本);Maven3.3

摘要说明:

Spring Cloud Config:Spring Cloud Config为分布式系统中的外部化配置提供服务器和客户端支持。通过配置服务器,您可以在所有环境中管理应用程序的外部属性。客户端和服务器映射上的概念与Spring环境和PropertySource抽象完全一致,因此它们非常适合Spring应用程序,但可以用于任何语言中运行的任何应用程序。当应用程序通过从dev到测试和生产的部署管道时,您可以管理这些环境之间的配置,并确保应用程序拥有在迁移时需要运行的所有内容。服务器存储后端的默认实现使用git,因此它可以轻松地支持带标签的配置环境版本,并且可以访问各种工具来管理内容。很容易添加替代实现并将它们与Spring配置插入。可参考;

特征

Spring Cloud Config Server功能:

  • 用于外部配置的HTTP,基于资源的API(名称 - 值对或等效的YAML内容)
  • 加密和解密属性值(对称或不对称)
  • 使用可轻松嵌入Spring Boot应用程序 @EnableConfigServer

Config Client功能(适用于Spring应用程序):

  • 绑定到配置服务器并Environment使用远程属性来初始化Spring
  • 加密和解密属性值(对称或不对称)

总结:配置中心为每个服务统一配置;各个服务可直接从配置中心获取信息;

步骤:

1.创建configServerconfigServer配置中心)子项目

通过SPRING INITIALIZR工具选择Cloud Config:Config Server模块构建configServer子项目引入依赖(pom.xnl):

<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>pers.cc</groupId><artifactId>springCloud</artifactId><version>0.0.1-SNAPSHOT</version></parent><artifactId>configServer</artifactId><name>configServer</name><description>配置中心-服务端</description><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></dependencies>
</project>

2.配置configServer

使用@EnableConfigServer注解开启configServer的服务中心配置:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.config.server.EnableConfigServer;@SpringBootApplication
@EnableConfigServer
@EnableDiscoveryClient
public class ConfigServerApplication {public static void main(String[] args) {SpringApplication.run(ConfigServerApplication.class, args);}
}

配置application.properties:

这里面需要先配置一个git仓库,当然这里也可以使用其他仓库如svn;如有分支,用户,密码及目录都可以配置;此次示例仓库为public且为更路径下故不设置;

#配置服务名称及端口
spring.application.name=config-server
server.port=9001
#将服务注册到注册中心
eureka.client.serviceUrl.defaultZone=http://localhost:1001/eureka/#服务的git仓库地址
spring.cloud.config.server.git.uri=https://gitee.com/yepiaoling/spring-could-config
#配置文件所在的目录
#spring.cloud.config.server.git.search-paths=/**
#配置文件所在的分支
spring.cloud.config.label=master
#git仓库的用户名
#spring.cloud.config.username=********
#git仓库的密码
#spring.cloud.config.password=********

仓库指定目录下的配置文件格式如下:

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

其中label为分支默认为master;application为服务名称;profile为环境;

故在仓库中添加配置文件config-client.properties,指的是给config-client服务默认环境的配置文件

test=config-client-test

启动服务:

服务注册中心(eurekaServer):eureka-server(1001)

配置中心(configServer):config-server(9001)

通过访问http://localhost:9001/config-client/master;获得配置信息:

{"name":"config-client","profiles":["master"],"label":null,"version":"e68941966cfb7f870643074ade0b27a9f752510d","state":null,"propertySources":[{"name":"https://gitee.com/yepiaoling/spring-could-config/config-client.properties","source":{"test":"config-client-test"}}]}

或者访问http://localhost:9001/master/config-client.properties(http://localhost:9001/config-client.properties)直接访问配置文件的信息:

test=config-client-test

3.创建configClientconfigClient配置客户端)子项目

 

通过SPRING INITIALIZR工具选择Cloud Config:Config Client模块构建configClient子项目引入依赖(pom.xnl):

<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>pers.cc</groupId><artifactId>springCloud</artifactId><version>0.0.1-SNAPSHOT</version></parent><artifactId>configClient</artifactId><name>configClient</name><description>服务配置中心-客户端</description><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-config</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></dependencies>
</project>

4.配置configClient

配置application.properties:

#配置服务名称及端口
spring.application.name=config-client
server.port=10001

但想获取配置中心的信息必须配置到bootstrap.properties中:

此次使用的是服务配置来进行与服务中心的交互;也可以直接使用spring.cloud.config.uri来进行配置;

#将服务注册到注册中心
eureka.client.serviceUrl.defaultZone=http://localhost:1001/eureka/
#开启配置服务发现
spring.cloud.config.discovery.enabled=true
#配置服务实例名称
spring.cloud.config.discovery.service-id=config-server#配置服务中心
#spring.cloud.config.uri=http://localhost:9001/#配置文件所在分支
spring.cloud.config.label=master
#配置文件所指环境
spring.cloud.config.profile=default

开发一个测试类(TestController)进行测试:

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
public class TestController {@Value("${test}")private String test;@GetMapping("/test")public String test() {return test;}
}

启动服务:

服务注册中心(eurekaServer):eureka-server(1001)

配置中心(configServer):config-server(9001)

配置客户端(configClient):config-client(10001)

通过访问http://localhost:10001/test来进行验证可直接获取配置的值:

config-client-test

5.配置手动刷新

我们接着上面将git仓库中添加config-client-dev.properties:

test=config-dev

更新bootstrap.properties中的:

#配置文件所指环境
spring.cloud.config.profile=dev

重新启动服务:

配置客户端(configClient):config-client(10001)
通过访问http://localhost:10001/test来进行验证可直接获取配置的值:

config-dev

接着我们修改config-client-dev.properties中的test值为config-client-dev;重新访问http://localhost:10001/test;但值未变,这说明配置修改但客户端未进行变更;

我们在pom.xml中添加spring-boot-starter-actuator依赖:

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency>

更新bootstrap.properties:

management.endpoints.web.exposure.include=*
#springboot 1.5.X 以上默认开通了安全认证,所以需要在配置文件application.properties添加以下配置,以post请求的方式来访问http://localhost:8081/refresh 就会更新修改后的配置文件
management.security.enabled=false

TestController中添加注解@RefreshScope;

重新启动服务:

配置客户端(configClient):config-client(10001)
通过访问http://localhost:10001/test来进行验证可直接获取配置的值:

config-client-dev

再修改config-client-dev.properties中的test值为config-client-dev1;

用post方式:http://localhost:10001/actuator/refresh进行任务更新;再访问会获得最新值;

6.源码地址

github地址:https://github.com/cc6688211/springCloud.git

这篇关于Spring Cloud(Finchley.RELEASE版本)微服务学习实践:7.1分布式配置中心的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/999839

相关文章

SpringBoot请求参数接收控制指南分享

《SpringBoot请求参数接收控制指南分享》:本文主要介绍SpringBoot请求参数接收控制指南,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Spring Boot 请求参数接收控制指南1. 概述2. 有注解时参数接收方式对比3. 无注解时接收参数默认位置

SpringBoot基于配置实现短信服务策略的动态切换

《SpringBoot基于配置实现短信服务策略的动态切换》这篇文章主要为大家详细介绍了SpringBoot在接入多个短信服务商(如阿里云、腾讯云、华为云)后,如何根据配置或环境切换使用不同的服务商,需... 目录目标功能示例配置(application.yml)配置类绑定短信发送策略接口示例:阿里云 & 腾

SpringBoot项目中报错The field screenShot exceeds its maximum permitted size of 1048576 bytes.的问题及解决

《SpringBoot项目中报错ThefieldscreenShotexceedsitsmaximumpermittedsizeof1048576bytes.的问题及解决》这篇文章... 目录项目场景问题描述原因分析解决方案总结项目场景javascript提示:项目相关背景:项目场景:基于Spring

Spring Boot 整合 SSE的高级实践(Server-Sent Events)

《SpringBoot整合SSE的高级实践(Server-SentEvents)》SSE(Server-SentEvents)是一种基于HTTP协议的单向通信机制,允许服务器向浏览器持续发送实... 目录1、简述2、Spring Boot 中的SSE实现2.1 添加依赖2.2 实现后端接口2.3 配置超时时

Spring Boot读取配置文件的五种方式小结

《SpringBoot读取配置文件的五种方式小结》SpringBoot提供了灵活多样的方式来读取配置文件,这篇文章为大家介绍了5种常见的读取方式,文中的示例代码简洁易懂,大家可以根据自己的需要进... 目录1. 配置文件位置与加载顺序2. 读取配置文件的方式汇总方式一:使用 @Value 注解读取配置方式二

一文详解Java异常处理你都了解哪些知识

《一文详解Java异常处理你都了解哪些知识》:本文主要介绍Java异常处理的相关资料,包括异常的分类、捕获和处理异常的语法、常见的异常类型以及自定义异常的实现,文中通过代码介绍的非常详细,需要的朋... 目录前言一、什么是异常二、异常的分类2.1 受检异常2.2 非受检异常三、异常处理的语法3.1 try-

Java中的@SneakyThrows注解用法详解

《Java中的@SneakyThrows注解用法详解》:本文主要介绍Java中的@SneakyThrows注解用法的相关资料,Lombok的@SneakyThrows注解简化了Java方法中的异常... 目录前言一、@SneakyThrows 简介1.1 什么是 Lombok?二、@SneakyThrows

Java中字符串转时间与时间转字符串的操作详解

《Java中字符串转时间与时间转字符串的操作详解》Java的java.time包提供了强大的日期和时间处理功能,通过DateTimeFormatter可以轻松地在日期时间对象和字符串之间进行转换,下面... 目录一、字符串转时间(一)使用预定义格式(二)自定义格式二、时间转字符串(一)使用预定义格式(二)自

如何为Yarn配置国内源的详细教程

《如何为Yarn配置国内源的详细教程》在使用Yarn进行项目开发时,由于网络原因,直接使用官方源可能会导致下载速度慢或连接失败,配置国内源可以显著提高包的下载速度和稳定性,本文将详细介绍如何为Yarn... 目录一、查询当前使用的镜像源二、设置国内源1. 设置为淘宝镜像源2. 设置为其他国内源三、还原为官方

Spring 请求之传递 JSON 数据的操作方法

《Spring请求之传递JSON数据的操作方法》JSON就是一种数据格式,有自己的格式和语法,使用文本表示一个对象或数组的信息,因此JSON本质是字符串,主要负责在不同的语言中数据传递和交换,这... 目录jsON 概念JSON 语法JSON 的语法JSON 的两种结构JSON 字符串和 Java 对象互转