springcloud分布式配置中心

1.分布式架构

1.1传统架构

image01.png

1.2分布式架构

image02.png

1.3项目架构

springcloud 版本 Hoxton.SR6

├── config-sever
│   ├── pom.xml
│   ├── src
│   │   ├── main
│   │   │   ├── java
│   │   │   │   └── com
│   │   │   │       └── zzm
│   │   │   │           └── zcd
│   │   │   │               └── ConfigServerApplication.java
│   │   │   └── resources
│   │   │       └── application.yml
├── eureka-server
│   ├── eureka-server.iml
│   ├── pom.xml
│   ├── src
│   │   ├── main
│   │   │   ├── java
│   │   │   │   └── com
│   │   │   │       └── zzm
│   │   │   │           └── zcd
│   │   │   │               └── ConfigEurekaServer.java
│   │   │   └── resources
│   │   │       └── application.yml
├── pom.xml
├── spring-cloud-config-demo.iml
└── system-service
    ├── pom.xml
    ├── src
    │   ├── main
    │   │   ├── java
    │   │   │   └── com
    │   │   │       └── zzm
    │   │   │           └── zcd
    │   │   │               ├── SystemServiceApplication.java
    │   │   │               ├── config
    │   │   │               │   └── ConfigInfoProperties.java
    │   │   │               └── controller
    │   │   │                   └── HelloController.java
    │   │   └── resources
    │   │       ├── application.yml
    │   │       └── bootstrap.yml


2.Spring Cloud 配置服务器

  • Spring Cloud Config Server

    Spring Cloud 配置服务器器提供分布式、动态化集中管理理应⽤用配置信息的能⼒

  • 构建 Spring Cloud 配置服务器

@EnableConfigServer

启动类

```java

/**

  • 服务配置中心

*/
@EnableDiscoveryClient
@EnableConfigServer
@SpringBootApplication
public class ConfigServerApplication {

public static void main(String[] args) {
    SpringApplication.run(ConfigServerApplication.class, args);
}

}

```

配置文件

server:
  port: 7071

spring:
  cloud:
    config:
      server:
       git:
         #git仓库文件地址
         uri: https://gitee.com/develop-alan-knowledge/zcd-config-file.git
         username: [email protected]
         password: zcd123456
         #搜索文件夹配置文件
         search-paths: demo-config
  application:
    name: config-server
eureka:
  client:
    service-url:
      defaultZone: http://localhost:7070/eureka/
    healthcheck:
      enabled: true


3.Spring Cloud 配置客户端

  • Spring Cloud Config Client

​ Spring Cloud 配置客户端提供连接 Spring Cloud 服务端,并且获取订阅的配置信息

  • 配置Spring Cloud 配置客户端

    • 创建 bootstrap.yml 或者 bootstrap.properties

    配置文件服务器地址一定要放在bootstrap.yml

    spring:
      cloud:
        config:
          #配置服务器地址
          uri: http://localhost:7071
          label: master
          name: application
          profile: dev
    
  • 配置 spring.cloud.config.* 信息

4.动态配置属性Bean

  • @RefreshScope

    rest层需要加入@RefreshScope注解才能获取最新配置

    @RefreshScope
    @RestController
    public class HelloController {
    
    
        @Autowired
        private ConfigInfoProperties configInfoProperties;
    
    
        @GetMapping("getInfo")
        public String getInfo(){
            return configInfoProperties.getConfig();
        }
    
    }
    
    

配置信息

@Component
@ConfigurationProperties(prefix = "com.springcloud")
public class ConfigInfoProperties {
    private String config;

    public String getConfig() {
        return config;
    }

    public void setConfig(String config) {
        this.config = config;
    }
}

  • /refresh Endpoint

    ​ git文件中配置文件修改需要更新到客户端需要执行下面 Endpoint

    http://localhost:端口/actuator/refresh

    执行上面Endpoint需要打开Endpoint配置

    management:
      endpoints:
        web:
          exposure:
            include: '*'
      endpoint:
        health:
          show-details: always
    
  • ContextRefresher

    org.springframework.cloud.endpoint.RefreshEndpoint源码

    @Endpoint(id = "refresh")
    public class RefreshEndpoint {
    
      private ContextRefresher contextRefresher;
    
      public RefreshEndpoint(ContextRefresher contextRefresher) {
          this.contextRefresher = contextRefresher;
      }
    
      @WriteOperation
      public Collection refresh() {
          Set keys = this.contextRefresher.refresh();
          return keys;
      }
    
    }
    
    

actuator/refresh执行是this.contextRefresher.refresh()方法

你可能感兴趣的:(springcloud分布式配置中心)