欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

SpringCloud-Spring Cloud Config配置属性刷新之手动刷新

程序员文章站 2022-06-12 23:33:35
...

 

目录

远程配置

创建工程-microservice-config-client-refresh

pom.xml

bootstrap.yml

application.yml

ConfigClientController

ConfigServerApplication

测试

启动工程

修改前测试

修改远程配置文件并提交

手动刷新

再访问


 

 

 

远程配置

SpringCloud-Spring Cloud Config配置属性刷新之手动刷新

SpringCloud-Spring Cloud Config配置属性刷新之手动刷新

 

 

 

创建工程-microservice-config-client-refresh

SpringCloud-Spring Cloud Config配置属性刷新之手动刷新

 

 

pom.xml

config-learning/microservice-config-client-refresh/pom.xml

<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>com.itmuch.cloud</groupId>
		<artifactId>microservice-spring-cloud</artifactId>
		<version>0.0.1-SNAPSHOT</version>
	</parent>

	<artifactId>microservice-config-client-refresh</artifactId>
	<packaging>jar</packaging>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>

	<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.boot</groupId>
			<artifactId>spring-boot-starter-actuator</artifactId>
		</dependency>
	</dependencies>
</project>

 

 

bootstrap.yml

spring:
  cloud:
    config:
      uri: http://localhost:8080
      profile: dev
      label: master   # 当configserver的后端存储是Git时,默认就是master 
  application:
    name: foobar

 

 

 

application.yml

server:
  port: 8081

 

 

ConfigClientController

com.itmuch.cloud.ConfigClientController

package com.itmuch.cloud;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RefreshScope
public class ConfigClientController {

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

  @GetMapping("/profile")
  public String getProfile() {
    return this.profile;
  }
}

说明:

    1)这里加上了@RefreshScope注解,支持配合更改刷新;

 

 

 

ConfigServerApplication

com.itmuch.cloud.ConfigServerApplication

package com.itmuch.cloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class ConfigServerApplication {
  public static void main(String[] args) {
    SpringApplication.run(ConfigServerApplication.class, args);
  }
}

 

 

测试

 

 

启动工程

1)microservice-config-server

2)microservice-config-client-refresh

 

修改前测试

http://localhost:8081/profile

SpringCloud-Spring Cloud Config配置属性刷新之手动刷新

 

 

修改远程配置文件并提交

SpringCloud-Spring Cloud Config配置属性刷新之手动刷新


 

手动刷新

C:\Users\shangwu>curl -X POST http://localhost:8081/refresh
["config.client.version","profile"]
C:\Users\shangwu>

 

再访问

http://localhost:8081/profile

SpringCloud-Spring Cloud Config配置属性刷新之手动刷新

 

 

 

 

 

 

 

 

==============================

QQ群:143522604

群里有相关资源

欢迎和大家一起学习、交流、提升!

==============================