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实现检查多个时间段是否有重合

《Java实现检查多个时间段是否有重合》这篇文章主要为大家详细介绍了如何使用Java实现检查多个时间段是否有重合,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录流程概述步骤详解China编程步骤1:定义时间段类步骤2:添加时间段步骤3:检查时间段是否有重合步骤4:输出结果示例代码结语作

Java中String字符串使用避坑指南

《Java中String字符串使用避坑指南》Java中的String字符串是我们日常编程中用得最多的类之一,看似简单的String使用,却隐藏着不少“坑”,如果不注意,可能会导致性能问题、意外的错误容... 目录8个避坑点如下:1. 字符串的不可变性:每次修改都创建新对象2. 使用 == 比较字符串,陷阱满

Java判断多个时间段是否重合的方法小结

《Java判断多个时间段是否重合的方法小结》这篇文章主要为大家详细介绍了Java中判断多个时间段是否重合的方法,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录判断多个时间段是否有间隔判断时间段集合是否与某时间段重合判断多个时间段是否有间隔实体类内容public class D

IDEA编译报错“java: 常量字符串过长”的原因及解决方法

《IDEA编译报错“java:常量字符串过长”的原因及解决方法》今天在开发过程中,由于尝试将一个文件的Base64字符串设置为常量,结果导致IDEA编译的时候出现了如下报错java:常量字符串过长,... 目录一、问题描述二、问题原因2.1 理论角度2.2 源码角度三、解决方案解决方案①:StringBui

Java覆盖第三方jar包中的某一个类的实现方法

《Java覆盖第三方jar包中的某一个类的实现方法》在我们日常的开发中,经常需要使用第三方的jar包,有时候我们会发现第三方的jar包中的某一个类有问题,或者我们需要定制化修改其中的逻辑,那么应该如何... 目录一、需求描述二、示例描述三、操作步骤四、验证结果五、实现原理一、需求描述需求描述如下:需要在

Java中ArrayList和LinkedList有什么区别举例详解

《Java中ArrayList和LinkedList有什么区别举例详解》:本文主要介绍Java中ArrayList和LinkedList区别的相关资料,包括数据结构特性、核心操作性能、内存与GC影... 目录一、底层数据结构二、核心操作性能对比三、内存与 GC 影响四、扩容机制五、线程安全与并发方案六、工程

JavaScript中的reduce方法执行过程、使用场景及进阶用法

《JavaScript中的reduce方法执行过程、使用场景及进阶用法》:本文主要介绍JavaScript中的reduce方法执行过程、使用场景及进阶用法的相关资料,reduce是JavaScri... 目录1. 什么是reduce2. reduce语法2.1 语法2.2 参数说明3. reduce执行过程

如何使用Java实现请求deepseek

《如何使用Java实现请求deepseek》这篇文章主要为大家详细介绍了如何使用Java实现请求deepseek功能,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1.deepseek的api创建2.Java实现请求deepseek2.1 pom文件2.2 json转化文件2.2

Java调用DeepSeek API的最佳实践及详细代码示例

《Java调用DeepSeekAPI的最佳实践及详细代码示例》:本文主要介绍如何使用Java调用DeepSeekAPI,包括获取API密钥、添加HTTP客户端依赖、创建HTTP请求、处理响应、... 目录1. 获取API密钥2. 添加HTTP客户端依赖3. 创建HTTP请求4. 处理响应5. 错误处理6.

Spring AI集成DeepSeek的详细步骤

《SpringAI集成DeepSeek的详细步骤》DeepSeek作为一款卓越的国产AI模型,越来越多的公司考虑在自己的应用中集成,对于Java应用来说,我们可以借助SpringAI集成DeepSe... 目录DeepSeek 介绍Spring AI 是什么?1、环境准备2、构建项目2.1、pom依赖2.2