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

相关文章

Spring Boot集成Druid实现数据源管理与监控的详细步骤

《SpringBoot集成Druid实现数据源管理与监控的详细步骤》本文介绍如何在SpringBoot项目中集成Druid数据库连接池,包括环境搭建、Maven依赖配置、SpringBoot配置文件... 目录1. 引言1.1 环境准备1.2 Druid介绍2. 配置Druid连接池3. 查看Druid监控

Java中读取YAML文件配置信息常见问题及解决方法

《Java中读取YAML文件配置信息常见问题及解决方法》:本文主要介绍Java中读取YAML文件配置信息常见问题及解决方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要... 目录1 使用Spring Boot的@ConfigurationProperties2. 使用@Valu

创建Java keystore文件的完整指南及详细步骤

《创建Javakeystore文件的完整指南及详细步骤》本文详解Java中keystore的创建与配置,涵盖私钥管理、自签名与CA证书生成、SSL/TLS应用,强调安全存储及验证机制,确保通信加密和... 目录1. 秘密键(私钥)的理解与管理私钥的定义与重要性私钥的管理策略私钥的生成与存储2. 证书的创建与

浅析Spring如何控制Bean的加载顺序

《浅析Spring如何控制Bean的加载顺序》在大多数情况下,我们不需要手动控制Bean的加载顺序,因为Spring的IoC容器足够智能,但在某些特殊场景下,这种隐式的依赖关系可能不存在,下面我们就来... 目录核心原则:依赖驱动加载手动控制 Bean 加载顺序的方法方法 1:使用@DependsOn(最直

SpringBoot中如何使用Assert进行断言校验

《SpringBoot中如何使用Assert进行断言校验》Java提供了内置的assert机制,而Spring框架也提供了更强大的Assert工具类来帮助开发者进行参数校验和状态检查,下... 目录前言一、Java 原生assert简介1.1 使用方式1.2 示例代码1.3 优缺点分析二、Spring Fr

java使用protobuf-maven-plugin的插件编译proto文件详解

《java使用protobuf-maven-plugin的插件编译proto文件详解》:本文主要介绍java使用protobuf-maven-plugin的插件编译proto文件,具有很好的参考价... 目录protobuf文件作为数据传输和存储的协议主要介绍在Java使用maven编译proto文件的插件

Jenkins分布式集群配置方式

《Jenkins分布式集群配置方式》:本文主要介绍Jenkins分布式集群配置方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1.安装jenkins2.配置集群总结Jenkins是一个开源项目,它提供了一个容易使用的持续集成系统,并且提供了大量的plugin满

Java中的数组与集合基本用法详解

《Java中的数组与集合基本用法详解》本文介绍了Java数组和集合框架的基础知识,数组部分涵盖了一维、二维及多维数组的声明、初始化、访问与遍历方法,以及Arrays类的常用操作,对Java数组与集合相... 目录一、Java数组基础1.1 数组结构概述1.2 一维数组1.2.1 声明与初始化1.2.2 访问

Javaee多线程之进程和线程之间的区别和联系(最新整理)

《Javaee多线程之进程和线程之间的区别和联系(最新整理)》进程是资源分配单位,线程是调度执行单位,共享资源更高效,创建线程五种方式:继承Thread、Runnable接口、匿名类、lambda,r... 目录进程和线程进程线程进程和线程的区别创建线程的五种写法继承Thread,重写run实现Runnab

Java 方法重载Overload常见误区及注意事项

《Java方法重载Overload常见误区及注意事项》Java方法重载允许同一类中同名方法通过参数类型、数量、顺序差异实现功能扩展,提升代码灵活性,核心条件为参数列表不同,不涉及返回类型、访问修饰符... 目录Java 方法重载(Overload)详解一、方法重载的核心条件二、构成方法重载的具体情况三、不构