springcloud----config分布式配置中心

分布式配置中心分为客户端与服务端:

服务端

 创建服务端的工程:

添加一下依赖

<dependencies>
		<!-- springCloud Config -->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-config-server</artifactId>
		</dependency>
		<!-- 避免Config的Git插件报错:org/eclipse/jgit/api/TransportConfigCallback -->
		<dependency>
			<groupId>org.eclipse.jgit</groupId>
			<artifactId>org.eclipse.jgit</artifactId>
			<version>4.10.0.201712302008-r</version>
		</dependency>
		<!-- 图形化监控 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-actuator</artifactId>
		</dependency>
		<!-- 熔断 -->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-hystrix</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-eureka</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-config</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-jetty</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-test</artifactId>
		</dependency>
		<!-- 热部署插件 -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>springloaded</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-devtools</artifactId>
		</dependency>
	</dependencies>

配置文件配置如下


server:
  port: 7104
spring:
  application:
    name: config-server
  cloud:
    config:
      server:
        git:
          uri: https://github.com/AvaJbuHtiG/springcloud-config
          search-paths: repo
          default-label: master
          username:
          password:

 主启动类上添加@EnableConfigServer注解,就可以启动了

git仓库里面有三个文件

http请求地址和资源文件映射如下:

application在这里对应config,profile指dev,pro,uat,label指git仓库分支,不传默认是master分支

客户端

config客户端和config  server项目结构基本相似。

客户端启动类上不要加@EnableConfigServer

客户端配置文件需要命名为bootstrap.yml或bootstrap.properties,因为bootstrap项目启动时,名bootstrap的配置文件会先于application的配置文件

简单具体配置如下

server.port=7105  
spring.application.name=config-client
spring.cloud.config.label=master #指定要读取文件所在的git分支
spring.cloud.config.profile=dev  #指定使用的构件环境,即git仓库文件的-dev/-pro/-uat
spring.cloud.config.uri=http://localhost:7104/ #config server 暴露的地址

然后创建接口测试下

@RestController
public class ConfigController {

    @Value("${spring.application.name}")
    String appName;

    @RequestMapping("/getName")
    public String getName() {
        return appName;
    }
}

访问该接口就会获取到该值

刷新配置中心信息

手动刷新

手动刷新需要做两步

1在config-server和config-client中加入以下依赖,该依赖是springboot的系统状态监控依赖

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

 config-clent中另加security的依赖

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

 并在config-clent配置文件中添加如下配置:

management.endpoints.web.exposure.include=*
management.endpoint.health.show-details=always

由于加了security依赖,需要关闭下端点的安全校验

@Configuration
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
    @Override
    public void configure(HttpSecurity http) throws Exception {
        http.csrf().disable();
    }
}

2在获取config-server信息接口上加@RefreshScope注解 。加上该注解的bean都会延迟加载,只有在第一次访问时在会被初始化。刷新bean后,在下次访问时会再次创建一个新的对象

然后修改下git上的配置文件,访问localhost:7002/actuator/refresh就刷新了信息。

结合spring cloud bus热刷新

bus的热刷新原理是:当git上文件被修改时,检查到Git Hook变化,触发Hook配置地址的调用,config server接收到请求并发布消息,Bus将消息发送到config cilent,当client接受到消息后会重新发送请求加载配置信息。这里用到了rabbitMQ

首先在config-server和config-client中都加入mq的依赖,

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

 然后在condig-server配置文件中配置下mq,

spring.rabbitmq.host=localhost
spring.rabbitmq.port=15672
spring.rabbitmq.user=guest
spring.rabbitmq.password=guest

 

高可用的配置中心

把config Server注册到Eureka去,然后,config 客户端从注册到Eureka的服务中读取配置信息

需要在config Server 和config Client中添加 Eurek-client的依赖,把这两个服务都注册到Eureka中去。

config server 配置文件中需要添加注册Eureka的注册信息

eureka:
  client:
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka/
    instance-id: config-server-7104
    prefer-ip-address: true

config Client的bootstrap配置文件中添加如下配置 

eureka.client.service-url.defaultZone=http://eureka7001.com:7001/eureka/ #注册到Eureka的地址
spring.cloud.config.discovery.enabled=true   # 是否可以发现注册在Eureka的其他服务。和@EnableDiscoveryClient类似
spring.cloud.config.discovery.service-id=config-server  #config Server在Eureka注册服务名称

启动Config Client之后,可以看到这条日志

config Client没注册到Eureka是,该条日志的地址是直接配置的Config Server 地址

猜你喜欢

转载自blog.csdn.net/zgq_hw/article/details/84492969