700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > SpringCloud config 配置中心介绍与基本配置使用

SpringCloud config 配置中心介绍与基本配置使用

时间:2021-04-06 12:31:12

相关推荐

SpringCloud config 配置中心介绍与基本配置使用

一、SpringCloud Config 介绍

出现背景:在微服务架构中,在没有配置中心出现时,我们每个应用的配置信息都在其配置文件application.properties中维护。加入整个系统中有很多应用,那么我们在调整起配置信息来将会非常繁琐。另外不同的环境、如开发环境、测试环境、生产环境等不同环境还需要调整不同的配置参数。在运行中的系统中当有需要调整起配置文件信息时,不能立即生效,仍需要我们进行重启生效。。等等问题迫切需要一个能够集中维护整个系统中不同应用的一个配置服务功能。来为我们解决如下问题:

集中配置管理。一个大型微服务架构可能有成百上千服务,所以集中配置管理非常重要。不同环境不通配置。比如不同环境下的服务器端口,数据源信息、注册中心地址、redis、es等等配置信息都是不同的。运行期间可以动态调整。运行中可以根据微服务负载情况,动态的调整数据源资源池大小等。配置修改后可以自动更新。当配置发生变化后,在不重启应用的情况下微服务可以自动更新配置。

为了解决如上问题,SpringCloud为我们提供了分布式配置中心组件SpringCloud Config。SpringCloud Config 是一个解决分布式系统配置管理方案。它包含了Client 和 Server 两部分。Server端提供所有配置文件的存储(git或其他工具)、以及将配置文件以接口的形式提供出去。Client 端及我们的微服务端通过配置服务端信息,从接口获取其配置文件信息初始化自身应用。

二、SpringCloud congfig 基本的配置使用

在基本的配置使用中,我们使用三个工程一个 Eureka注册中心,一个product_service 工程,一个注册中心server端工程config_server。

预期效果:对老的微服务将其配置文件内容从项目剥离,交由配置中心server端维护。服务端修改配置文件后,通过调用客户端应用刷新配置接口,使配置信息立刻在当前应用生效。

1,服务端主要步骤:

将配置文件按照命名规则命名并上传到git中创建config_server 工程添加依赖编写配置文件编写启动类

步骤1、将配置文件按照命名规则命名并上传到git中。

次过程省略,根据各自情况在git上创建工程并分别上传两个不同的配置文件。文件命名规则如下(application 为应用名,profile为后缀,通常用来区分开发测试生产环境):

{application}-{profile}.yml{application}-{profile}.properties

步骤2、创建config_server 工程

创建maven model子工程config_server

步骤3、添加依赖 spring-cloud-config-server

<?xml version="1.0" encoding="UTF-8"?><project xmlns="/POM/4.0.0"xmlns:xsi="/2001/XMLSchema-instance"xsi:schemaLocation="/POM/4.0.0 /xsd/maven-4.0.0.xsd"><parent><artifactId>SpringCloud</artifactId><groupId>com.xiaohui.springCloud</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>config_server</artifactId><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-config-server</artifactId></dependency></dependencies></project>

步骤4、编写配置文件

server:port: 9999spring:application:name: config-server #服务名称cloud:config:server:git:uri: http://admin@127.0.0.1:10001/r/SpringCloud/config_server.gitusername: adminpassword: xxxxx

上面中 uri 为我们存放配置文件 git的地址,下面的为用户名以及密码。

步骤5、编写启动类

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

注意:在启动类上需要添加@EnableConfigServer注解。

测试:启动服务端后,我们通过 ip+port+配置文件名可以正常打开及服务端配置启动成功。

2,客户端主要步骤:

添加配置中心依赖改造配置文件启动类controller 编写

我们的客户端主要在现有工程上进行改造,原始项目为《生产者、消费者工程搭建与调用》中的生产者工程。

步骤一、添加配置中心依赖

<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-config</artifactId></dependency>

步骤二、改造配置文件

我们将配置文件进行重新命名为application-prd.yml、以及复制一份命名为application-dev.yml,并调整部分信息如配置项name值。分别表示 生产和测试环境配置文件。并上传在git服务器中(同服务端一步骤)。

在SpringBoot项目中,bootstrap.yml 配置文件加载优先级高于application.yml。我们需要通过bootstrap.yml 配置文件来配置配置中心服务端信息,来获取我们要初始化加载的应用配置信息。

spring:cloud:config:name: application #对应git上文件名的前部分profile: prd #git 上配置文件名的后部分label: master #git分支uri: http://127.0.0.1:9999#开启动态刷新的请求路径端点management:endpoints:web:exposure:include: refresh

上面中不一部分为配置中心服务端相关信息。包含服务端地址 uri、以及配置文件的前后名称以及代码分支。下面部分为实现调用post请求动态实时的刷新配置数据,此处实现动态刷新需要有spring-boot-starter-actuator依赖。不使用开启动态刷新,则修改配置文件提交git后需要重新启动该应用才能生效。

步骤三、启动类

package com.xiaohui.springcloud;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.flix.eureka.EnableEurekaClient;@SpringBootApplication@EnableEurekaClient //激活Eureka 服务注册拉取功能public class ProductApplication {public static void main(String[] args) {SpringApplication.run(ProductApplication.class,args);}}

步骤四、controller 编写

package com.xiaohui.springcloud.controller;import com.xiaohui.monResult;import com.xiaohui.springcloud.entities.Payment;import com.xiaohui.springcloud.service.IPaymentService;import lombok.extern.slf4j.Slf4j;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Value;import org.springframework.cloud.context.config.annotation.RefreshScope;import org.springframework.web.bind.annotation.*;import java.util.concurrent.TimeUnit;@RestController@Slf4j@RefreshScopepublic class PaymentController {@AutowiredIPaymentService paymentService;@Value("${spring.cloud.client.ip-address}")private String ip;@Value("${name}")private String name;@Value("${server.port}")private String port;@GetMapping("/payment/get/{id}")public CommonResult getPaymentById(@PathVariable("id") Long id){Payment payment = paymentService.getPaymentById(id);log.info(payment.toString());if(payment != null){return new CommonResult(0,"查询成功 server node:"+ip+":"+port,payment);}else{return new CommonResult(-9999,"查询失败,不存在信息 server node:"+ip+":"+port,null);}}@GetMapping("/payment/get/name")public CommonResult getPaymentById(){return new CommonResult(0,"name:"+name);}}

在controller 中我们要实现通过调用post接口实现配置动态生效,需要给controller添加类注解@RefreshScope 注解。(查询数据库service成dao层代码省略)

测试:我们先启动复制注册中心Eureka,再启动config_server 工程,在启动微服务product_service工程。首先测试访问http://127.0.0.1:8002/payment/get/name

在修改配置文件application-prd.yml 文件name值后提交代码,测试和上面一样,当我们使用postman 进行post请求 127.0.0.1:8002/actuator/refresh 后。我们在请求打印name的请求后,我们看到我们修改的配置信息已经生效。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。