SpringCloud-Config的总结

SpringCloud-Config学习

一,概述

一图胜千言.


二,实战

    1,远程配置中心文件项目

  • 项目名称:config-repo
  • 配置文件:application.properties    

eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/

  • dev 环境的配置文件
demo.env=dev
  • test 环境的配置文件
demo.env=test
  •  说明:该项目本质是git项目,只有上面的配置文件其他不需要配置,只是简单的配置文件仓库,目的是提供公共配置中心.

    2,配置服务器项目

  •        项目名称:config-server
  •         端口号:8091
  •         pom文件中的依赖
	<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-config-server</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-config</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
  •         application.properties
server.port=8091
spring.application.name=springcloud_config3
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/
#服务的git仓库地址
spring.cloud.config.server.git.uri=https://git.yomob.com/wangwg/config-repo.git
#spring.cloud.config.server.git.uri=https://github.com/ziwenwang356/gitskills

#配置文件所在的目录
spring.cloud.config.server.git.search-paths=/config-repo
#配置文件所在的分支
spring.cloud.config.label=master
#git仓库的用户名
spring.cloud.config.server.git.username=wangwgxyzwwww.com
#git仓库的密码
spring.cloud.config.server.git.password=123456789
  •         程序入口
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@EnableConfigServer
@EnableEurekaClient
@SpringBootApplication
public class SpringcloudConfig3Application {

	public static void main(String[] args) {
		SpringApplication.run(SpringcloudConfig3Application.class, args);
	}
}
  •         可以这么测试
http://localhost:8091/master/config-client.properties
or
http://localhost:8091/config-client-dev.properties

    3,获取配置配置中心客户端



  •     项目名称:config-client
  •     端口:8083
  •     pom文件中的依赖
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-config</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
  •      application.properties
server.port=8083
spring.application.name=config-client
spring.profiles.active=dev
spring.cloud.config.uri=http://localhost:8091
  •         bootstrap.yml        
spring:
  application:
    name: config-client
  profiles:
    active: dev
  cloud:
    config:
      uri: http://localhost:8091
  •       程序入口
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class ConfigClientApplication {

	public static void main(String[] args) {
		SpringApplication.run(ConfigClientApplication.class, args);
	}
}
  •         测试代码
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author Wengang Wang
 * @since May 22, 2018
 */
@RestController
public class DemoController {

    @Value("${demo.env}")
    private String env;

    @GetMapping("/hello")
    public String hello() {
        return "Hello! It's " + env;
    }
}
  •      按照如下的方式可以看到效果,明白其中的原理
按照如下的方式进行测试:(它就首先去访问springcloud_config3的服务,然后springcloud_config3去访问config-repo的文件中的参数)

http://localhost:8083/hello
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要实现Spring Cloud Config的热加载,可以使用以下步骤: 1. 配置Spring Cloud Config Server连接私有GitLab repository。可以通过在配置文件中设置spring.cloud.config.uri来指定Config Server的地址,同时还要设置spring.cloud.config.label来指定Git分支。例如: ``` spring.cloud.config.uri: http://localhost:8888 spring.application.name: gateway spring.profiles.active: dev spring.cloud.config.label: dev ``` [1] 2. 启动Spring Cloud Config Server,并确保它能够成功连接到GitLab repository。 3. 在应用程序中添加Spring Cloud Config Client依赖,以便能够从Config Server获取配置信息。 4. 在应用程序的配置文件中设置以下属性来启用热加载: ``` spring.cloud.config.allowOverride: true spring.cloud.config.refreshable: true ``` 这样配置后,应用程序会定期轮询Config Server以获取最新的配置信息,并在配置更新时重新加载配置。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [总结:Spring Cloud Config](https://blog.csdn.net/w2009211777/article/details/127692342)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [springcloud config实现配置热加载(简易版本)](https://blog.csdn.net/lk569696322/article/details/106240420)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值