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

 

目录

远程配置

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

pom.xml

bootstrap.yml

application.yml

ConfigClientController

ConfigServerApplication

测试

启动工程

修改前测试

修改远程配置文件并提交

手动刷新

再访问


 

 

 

远程配置

 

 

 

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

 

 

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

 

 

修改远程配置文件并提交


 

手动刷新

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

 

再访问

http://localhost:8081/profile

 

 

 

 

 

 

 

 

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

QQ群:143522604

群里有相关资源

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

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值