服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - spring cloud学习教程之config修改配置详解

spring cloud学习教程之config修改配置详解

2020-12-25 14:41liaokailin Java教程

这篇文章主要给大家介绍了关于spring cloud学习教程之config修改配置的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧。

之前我们讲过了spring cloud之config配置的相关内容,那么在Git端修改配置后如何让客户端生效?下面来一起看看详细的介绍吧。

访问接口修改

refresh

post方式执行http://localhost/refresh 会刷新env中的配置

restart

如果配置信息已经注入到bean中,由于bean是单例的,不会去加载修改后的配置

需要通过post方式去执行http://localhost/restart,

需要通过application.properties中配置endpoints.restart.enabled=true启动指定的端口

弊端: 通过restart耗时比较长,因此就有了RefreshScope

RefreshScope

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package com.lkl.springcloud.config.client;
 
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
/**
 * Created by liaokailin on 16/4/28.
 */
@EnableAutoConfiguration
@ComponentScan
@RestController
@RefreshScope
public class Application {
 
 @Value("${name:World!}") String name ;
 
 @RequestMapping("/")
 public String home(){
 return "Hello " + name;
 }
 
 
 public static void main(String[] args) {
 SpringApplication.run(Application.class,args);
 }
}

在执行refresh时会刷新bean中变量值。

ok ~ it's work ! more about is here 

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对服务器之家的支持。

原文链接:http://blog.csdn.net/liaokailin/article/details/51307603

延伸 · 阅读

精彩推荐