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

相关文章

Java中的Cursor使用详解

《Java中的Cursor使用详解》本文介绍了Java中的Cursor接口及其在大数据集处理中的优势,包括逐行读取、分页处理、流控制、动态改变查询、并发控制和减少网络流量等,感兴趣的朋友一起看看吧... 最近看代码,有一段代码涉及到Cursor,感觉写法挺有意思的。注意是Cursor,而不是Consumer

解决java.lang.NullPointerException问题(空指针异常)

《解决java.lang.NullPointerException问题(空指针异常)》本文详细介绍了Java中的NullPointerException异常及其常见原因,包括对象引用为null、数组元... 目录Java.lang.NullPointerException(空指针异常)NullPointer

javaScript在表单提交时获取表单数据的示例代码

《javaScript在表单提交时获取表单数据的示例代码》本文介绍了五种在JavaScript中获取表单数据的方法:使用FormData对象、手动提取表单数据、使用querySelector获取单个字... 方法 1:使用 FormData 对象FormData 是一个方便的内置对象,用于获取表单中的键值

前端知识点之Javascript选择输入框confirm用法

《前端知识点之Javascript选择输入框confirm用法》:本文主要介绍JavaScript中的confirm方法的基本用法、功能特点、注意事项及常见用途,文中通过代码介绍的非常详细,对大家... 目录1. 基本用法2. 功能特点①阻塞行为:confirm 对话框会阻塞脚本的执行,直到用户作出选择。②

mac安装nvm(node.js)多版本管理实践步骤

《mac安装nvm(node.js)多版本管理实践步骤》:本文主要介绍mac安装nvm(node.js)多版本管理的相关资料,NVM是一个用于管理多个Node.js版本的命令行工具,它允许开发者在... 目录NVM功能简介MAC安装实践一、下载nvm二、安装nvm三、安装node.js总结NVM功能简介N

SpringBoot项目注入 traceId 追踪整个请求的日志链路(过程详解)

《SpringBoot项目注入traceId追踪整个请求的日志链路(过程详解)》本文介绍了如何在单体SpringBoot项目中通过手动实现过滤器或拦截器来注入traceId,以追踪整个请求的日志链... SpringBoot项目注入 traceId 来追踪整个请求的日志链路,有了 traceId, 我们在排

Java实战之利用POI生成Excel图表

《Java实战之利用POI生成Excel图表》ApachePOI是Java生态中处理Office文档的核心工具,这篇文章主要为大家详细介绍了如何在Excel中创建折线图,柱状图,饼图等常见图表,需要的... 目录一、环境配置与依赖管理二、数据源准备与工作表构建三、图表生成核心步骤1. 折线图(Line Ch

Spring Boot 3 整合 Spring Cloud Gateway实践过程

《SpringBoot3整合SpringCloudGateway实践过程》本文介绍了如何使用SpringCloudAlibaba2023.0.0.0版本构建一个微服务网关,包括统一路由、限... 目录引子为什么需要微服务网关实践1.统一路由2.限流防刷3.登录鉴权小结引子当前微服务架构已成为中大型系统的标

MySQL zip安装包配置教程

《MySQLzip安装包配置教程》这篇文章详细介绍了如何使用zip安装包在Windows11上安装MySQL8.0,包括下载、解压、配置环境变量、初始化数据库、安装服务以及更改密码等步骤,感兴趣的朋... 目录mysql zip安装包配置教程1、下载zip安装包:2、安装2.1 解压zip包到安装目录2.2

Java集合中的List超详细讲解

《Java集合中的List超详细讲解》本文详细介绍了Java集合框架中的List接口,包括其在集合中的位置、继承体系、常用操作和代码示例,以及不同实现类(如ArrayList、LinkedList和V... 目录一,List的继承体系二,List的常用操作及代码示例1,创建List实例2,增加元素3,访问元