基于Spring Boot 2的Spring Cloud Config搭建,整合Spring Security

前言:SpringBoot2 中不少的配置相对于之前的版本都作了修改,导致很多以前的配置都不生效,花费了将近两天才折腾出来,以此记录一下。

一、搭建config server

1、pom文件引入相关依赖。

		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-config-server</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-security</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-actuator</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-bus-amqp</artifactId>
		</dependency>

2、启用ConfigServer

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

3、配置application.yml文件

spring:
  profiles:
     active: git
  cloud:
    config:
      enabled: true
      server:
        git:
          uri: https://e.coding.net/xxx/xxx.git
          username: xxxxxx
          password: xxxxxx
  rabbitmq:
    host: 127.0.0.1
    port: 5672
    username: guest
    password: guest
  security:  
    user:
      name: user
      password: 123456

management:
  endpoints:
    web:
      exposure:
        include: bus-refresh  #必须手动配置,暴露刷新端口

server:
  port: 8868

4、之前以为这样配置就完事了,事实上config server的功能也确实实现了,但是调用http://localhost:8868/actuator/bus-refresh时,怎么也刷新不了,没报任何错,很快就返回了。后面猜测是这个接口受到security的影响,所以需要配置,把它暴露出来。因此增加了一个WebSecurityConfigurerAdapter的配置类。

@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {


	@Override
	public void configure(HttpSecurity http) throws Exception {
		http.authorizeRequests()
				.antMatchers("/actuator/bus-refresh").permitAll()  //让/actuator/bus-refresh可以直接访问
				.anyRequest().authenticated()	//其它接口都需要认证
				.and().csrf().disable()
				.httpBasic()
				;

	}


}

 

二、Config Client搭建

1、Pom文件引入。

		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-config</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-bus-amqp</artifactId>
		</dependency>

2、创建bootstrap.yml

spring:
  application:
    name: config-client
  cloud:
    config:
      uri: http://localhost:8868/
      fail-fast: true
      username: user
      password: 123456
      label: master
      profile: test

server:
  port: 9001

3、其它地方不用作额外配置,启动时会先加载bootstrap.yml文件,然后根据配置中的spring.cloud.config.uri中的地址,去获取加载application.yml文件,之后再根据自身的spring.application.name以及spring.cloud.config.profile获取到config-client-test.yml文件。个人感觉这样配置最大的好处是将一些公共的配置,放在application.yml中,服务自定义配置放在config-client-test.yml这样的文件中。

4、使用@Value赋值需要在类上加@RefreshScope注解,才会将值刷新

三,自动刷新

在修改配置文件后,只需要调用http://localhost:88686/actuator/bus-refresh ,配置了消息总线的服务,都会刷新配置文件。使用git或者svn作为文件仓库都可以用它们自带的触发事件,在提交代码之后,请求一下http://localhost:88686/actuator/bus-refresh 就实现了自动刷新机制。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值