springCloud微服务系列——配置中心第四篇——手动刷新

一、简介

       配置文件如果改变了,已经启动的服务如何获得最新的配置,当然可以重新启动服务,但是这样速度太慢了,还需要到服务器上去操作,增加了运维成本,spring cloud提供了一个手动刷新的解决方案。

二、端点配置

       spring cloud在actuator中提供了一个refresh端点,通过该端点可以进行刷新。

       引入actuator

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

       引入spring security

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>

        配置management并暴露端点

management: 
  server: 
    port: 8101
    servlet: 
      context-path: /demo/configClient/admin    
  endpoints: 
    web: 
      exposure:
        include: "*"

        在启动类上加@RefreshScope注解

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

三、安全配置

       对refresh端点不进行csrf防护

@Configuration
@EnableWebSecurity
public class EurekaClientWebSecurityConfig extends WebSecurityConfigurerAdapter {

	@Override
	protected void configure(HttpSecurity http) throws Exception {
		
		 http
         .authorizeRequests()
         // 普通的接口不需要校验
         .antMatchers("/*api/**").permitAll()
         // swagger页面需要添加登录校验
         .antMatchers("/swagger-ui.html").authenticated()
         // 监控节点需要添加登录校验
         .requestMatchers(EndpointRequest.toAnyEndpoint()).authenticated()
         .and()
         // 允许刷新服务
         .csrf().ignoringAntMatchers("/**/refresh/**");
		 
		 super.configure(http);
		 
	}
	
}

四、刷新

       通过POST调用refresh端点,如果配置有改变,返回结果如下

猜你喜欢

转载自blog.csdn.net/guduyishuai/article/details/81705487