首页 > 其他分享 >SpringCloud Config

SpringCloud Config

时间:2023-04-05 17:11:49浏览次数:32  
标签:http Config SpringCloud 配置 cloud spring config localhost

为什么需要配置中心

单体应用,配置写在配置文件中,没有什么大问题。如果要切换环境 可以切换不同的profile(2种方式),但在微服务中。

  1. 微服务比较多。成百上千,配置很多,需要集中管理。

  2. 管理不同环境的配置。

  3. 需要动态调整配置参数,更改配置不停服。

配置中心介绍

分布式配置中心包括3个部分:

  1. 存放配置的地方:git ,本地文件 等。
  2. config server。从 1 读取配置。
  3. config client。是 config server 的客户端 消费配置。

例子

新建ConfigServer模块,加入依赖:

     <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>

 

在application.properties配置:

server.port=7010

spring.application.name=configServer

eureka.client.service-url.defaultZone=http://localhost:8761/eureka/

spring.cloud.config.server.git.uri=https://gitee.com/xxxx/config-center.git
spring.cloud.config.server.git.username=你的用户名
spring.cloud.config.server.git.password=你的密码
spring.cloud.config.label=master

spring.cloud.config.server.bootstrap=true

这里用gitee演示。在上面配置的git仓库中加入配置文件consumer-dev.properties:

test1=123

 
配置文件名规则为:

/{label}/{name}-{profiles}.yml

文件后缀可以是properties,yml等。label是仓库分支、默认master分支,name是服务名称,profile是环境名称,比如开发,测试,生产。consumer-dev.properties配置文件是consumer服务在开发环境所使用的。
 
启动类加@EnableConfigServer注解,启动后访问http://localhost:7010/master/consumer-dev.properties,看到浏览器展示test1: 123

 
 

在ConsumerByRibbon加入配置中心客户端依赖:

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

 

在bootstrap.properties配置:

#直接URL方式查找配置中心
spring.cloud.config.uri=http://localhost:7010/
#通过注册中心查找
#spring.cloud.config.discovery.enabled=true
#spring.cloud.config.discovery.service-id=configServer
spring.cloud.config.profile=dev
spring.cloud.config.label=master

 

在controller中加入:

 @Value("${test1}")
  private String test1;

 @GetMapping("/getConfig")
  public String getConfig() {
      return test1;
  }

启动后访问http://localhost:8003/getConfig,看到展示123。将bootstrap.properties修改为:

#直接URL方式查找配置中心
#spring.cloud.config.uri=http://localhost:7010/
#通过注册中心查找
spring.cloud.config.discovery.enabled=true
spring.cloud.config.discovery.service-id=configServer
spring.cloud.config.profile=dev
spring.cloud.config.label=master

重启后访问http://localhost:8003/getConfig,可看到和上面相同的结果。

 
 

将git仓库上的 consumer-dev.properties文件内容改为:

test1=1234

访问http://localhost:8003/getConfig,看到123,不是git仓库最新配置。

手动配置热更新

  1. 开启actuator中的refresh端点
  2. Controller中添加@RefreshScope注解
  3. 向客户端 url http://localhost:8003/actuator/refresh发送Post请求

 
Controller中添加@RefreshScope注解重启ConsumerByRibbon访问http://localhost:8003/getConfig看到最新配置。将git仓库上的 consumer-dev.properties文件内容改为:

test1=12345

 

通过postman向客户端 url http://localhost:8003/actuator/refresh发送Post请求后访问http://localhost:8003/getConfig,看到最新配置12345。

标签:http,Config,SpringCloud,配置,cloud,spring,config,localhost
From: https://www.cnblogs.com/shigongp/p/17289690.html

相关文章

  • vue项目启动时 `webpack-dev-server –inline –progress –config build/webpack.dev
    vue项目在npmrundev时报错[email protected]:webpack-dev-server--inline--progress--configbuild/webpack.dev.conf.js解决这类问题主要分两种情况这个项目已经构建好的项目,你只是从git、snv或者其他地方引入,别人能运行你不能运行这是一个新构建的vue项目第一......
  • 简单的CMakePresets.json解析 -- configurePresets
    ----CMake官方文档-----CMakeLists.txt是通用的c++项目管理文件,在不同的设备中,环境变量,编译器等都可能不同,将这些设置都交给CMakeLists.txt,并不是一个好办法。为了降低CMakeLists.txt的臃肿程度,简化其判断,可以针对不同设备,配置不同的CMakePresets.json.使得项目可以在......
  • How to Configure Nginx reverse proxy the domain
    未测试过,自己记录待用http{resolver8.8.8.8;upstreamexample{serverhttp://example.comresolve[use_last]...;keepalive1024;}第二种负载均衡upstreammytarget{serveraaa.tar.com:443max_fails=3fail_timeout=60s;serverbbb.tar.com:443backup;}server......
  • SpringCloud——SpringCloud Alibaba Sentinel原理与实战
    摘要在微服务架构中,我们将系统拆分成了很多服务单元,各单元的应用间通过服务注册与订阅的方式互相依赖。由于每个单元都在不同的进程中运行,依赖通过远程调用的方式执行,这样就有可能因为网络原因或是依赖服务自身问题出现调用故障或延迟而这些问题会直接导致调用方的对外服务也出现延......
  • SpringCloud——SpringCloud Sleuth原理与实战
    摘要SpringCloudEureka是SpringCloudNetflix微服务套件中的一部分,它基于NetflixEureka做了二次封装,主要负责完成微服务架构中的服务治理功能。SpringCloud通过为Eureka增加了SpringBoot风格的自动化配置,我们只需通过简单引入依赖和注解配置就能让SpringBoot构建的微服务......
  • SpringBoot——注解@SpringBootConfiguration源码分析
    摘要SpringMVC是一个MVC开源框架,用来代替Struts。它是Spring项目里面的一个重要组成部分,能与SpringIOC容器紧密结合,以及拥有松耦合、方便配置、代码分离等特点,让JAVA程序员开发WEB项目变得更加容易。SpringMVC的异常处理?1.web.xml中异常处理通常为了给用户提供良好......
  • 7-springcloud-eureka-3-搭建与配置eureka服务注册中心
    SpringCloud要使用Eureka注册中心非常简单和方便,SpringCloud中的Eureka服务注册中心实际上也是一个SpringBoot工程,我们只需通过引入相关依赖和注解配置就能让SpringBoot构建的微服务应用轻松地与Eureka进行整合。具体步骤如下:1、创建一个SpringBoot项目,并且添......
  • dash-board的kube-config文件怎么设置 就是比kube-proxy类似多了一个token选项
    https://kubernetes.io/zh/docs/reference/access-authn-authz/rbac/#使用RBAC鉴权RBAC是基于角色的访问控制(Role-BasedAccessControl)https://kubernetes.io/zh/docs/reference/access-authn-authz/authorization/#鉴权概述1.1:在指定namespace创建账户:#kubectlcre......
  • 3-springcloud整体架构及调用举例
    SpringCloud的整体架构  ServiceProvider:暴露服务的服务提供方。ServiceConsumer:调用远程服务的服务消费方。EureKaServer:服务注册中心和服务发现中心。   ......
  • SpringCloud Admin健康检查
    1.什么是SpringBootAdmin?SpringBootAdmin是codecentric公司开发的一款开源社区项目,目标是让用户更方便的管理以及监控SpringBoot®应用。应用可以通过我们的SpringBootAdmin客户端(通过HTTP的方式)或者使用SpringCloud(比如Eureka,consul的方式)注册。而前端UI则......