springcloud微服务(七)分布式配置中心

目录

Config架构

Git环境搭建

服务端详解

Maven依赖信息

application.properties配置

项目启动

效果

客户端详解 

Maven依赖信息

bootstrap.properties

读取配置文件

启动类

效果:

动态刷新数据

actuator端点刷新数据

Maven依赖信息

Bootstrap.xml新增

生效前提

 Controller

手动刷新接口


Config架构

当一个系统中的配置文件发生改变的时候,我们需要重新启动该服务,才能使得新的配置文件生效,spring cloud config可以实现微服务中的所有系统的配置文件的统一管理,而且还可以实现当配置文件发生变化的时候,系统会自动更新获取新的配置。

Git环境搭建

使用码云环境搭建git服务器端 

码云环境地址: https://gitee.com/awave/config

服务端详解

项目名称:springboot2.0-config_server

Maven依赖信息

     <parent>

          <groupId>org.springframework.boot</groupId>

          <artifactId>spring-boot-starter-parent</artifactId>

          <version>2.0.1.RELEASE</version>

     </parent>

     <!-- 管理依赖 -->

     <dependencyManagement>

          <dependencies>

               <dependency>

                    <groupId>org.springframework.cloud</groupId>

                    <artifactId>spring-cloud-dependencies</artifactId>

                    <version>Finchley.M7</version>

                    <type>pom</type>

                    <scope>import</scope>

               </dependency>

          </dependencies>

     </dependencyManagement>

     <dependencies>

          <!--spring-cloud 整合 config-server -->

          <dependency>

               <groupId>org.springframework.cloud</groupId>

               <artifactId>spring-cloud-config-server</artifactId>

          </dependency>

          <!-- SpringBoot整合eureka客户端 -->

          <dependency>

               <groupId>org.springframework.cloud</groupId>

               <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>

          </dependency>

 

     </dependencies>

     <!-- 注意: 这里必须要添加, 否者各种依赖有问题 -->

     <repositories>

          <repository>

               <id>spring-milestones</id>

               <name>Spring Milestones</name>

               <url>https://repo.spring.io/libs-milestone</url>

               <snapshots>

                    <enabled>false</enabled>

               </snapshots>

          </repository>

     </repositories>

 

application.properties配置

###服务启动端口号

  server.port= 9003

  ###服务名称(服务注册到eureka名称)

  spring.application.name= eureka-client-config-server

  ###git环境地址

  spring.cloud.config.server.git.uri= https://gitee.com/awave/config.git

  ####搜索目录

  spring.cloud.config.server.git.search-paths= config

  ####读取分支

  spring.cloud.config.server.git.label= master

  

  ###服务注册到eureka地址

  eureka.client.service-url.defaultZone= http://localhost:8000/eureka,http://localhost:8001/eureka
  eureka.client.register-with-eureka= true
  eureka.client.fetch-registry= true

 

 

项目启动

@EnableConfigServer

@SpringBootApplication

public class ConfigServerApplication {

    public static void main(String[] args) {

         SpringApplication.run(ConfigServerApplication.class, args);

    }

}

@EnableConfigServer 开启分布式配置中心服务器端

效果

启动服务,接下来测试一下。

Spring Cloud Config 有它的一套访问规则,我们通过这套规则在浏览器上直接访问就可以。

/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties

{application} 就是应用名称,对应到配置文件上来,就是配置文件的名称部分,例如我上面创建的配置文件。

{profile} 就是配置文件的版本,我们的项目有开发版本、测试环境版本、生产环境版本,对应到配置文件上来就是以 application-{profile}.yml 加以区分,例如application-dev.properties、application-sit.properties、application-prod.properties

{label} 表示 git 分支,默认是 master 分支,如果项目是以分支做区分也是可以的,那就可以通过不同的 label 来控制访问不同的配置文件了。

 

读取配置文件信息 http://127.0.0.1:9003/eureka-client-config-client-dev.properties

客户端详解 

项目名称:springboot2.0-config_client

 

Maven依赖信息

<parent>

         <groupId>org.springframework.boot</groupId>

         <artifactId>spring-boot-starter-parent</artifactId>

         <version>2.0.1.RELEASE</version>

    </parent>

    <!-- 管理依赖 -->

    <dependencyManagement>

         <dependencies>

             <dependency>

                  <groupId>org.springframework.cloud</groupId>

                  <artifactId>spring-cloud-dependencies</artifactId>

                  <version>Finchley.M7</version>

                  <type>pom</type>

                  <scope>import</scope>

             </dependency>

 

         </dependencies>

    </dependencyManagement>

    <dependencies>

         <!-- SpringBoot整合Web组件 -->

         <dependency>

             <groupId>org.springframework.boot</groupId>

             <artifactId>spring-boot-starter-web</artifactId>

         </dependency>

         <dependency>

             <groupId>org.springframework.cloud</groupId>

             <artifactId>spring-cloud-config-client</artifactId>

         </dependency>

         <!-- SpringBoot整合eureka客户端 -->

         <dependency>

             <groupId>org.springframework.cloud</groupId>

             <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>

         </dependency>

    </dependencies>

    <!-- 注意: 这里必须要添加, 否者各种依赖有问题 -->

    <repositories>

         <repository>

             <id>spring-milestones</id>

             <name>Spring Milestones</name>

             <url>https://repo.spring.io/libs-milestone</url>

             <snapshots>

                  <enabled>false</enabled>

             </snapshots>

         </repository>

    </repositories>

 

bootstrap.properties

###服务启动端口号

  server.port= 9004

  ###服务名称(服务注册到eureka名称) ####注册中心应用名称 要和git服务器的文件名称保持一致

  spring.application.name= eureka-client-config-client

  ###读取后缀

  spring.cloud.config.profile= dev

  

  ####读取config-server注册地址

  spring.cloud.config.discovery.service-id= eureka-client-config-server

  #开启读取权限

  spring.cloud.config.discovery.enabled= true


  ###服务注册到eureka地址

  eureka.client.service-url.defaultZone= http://localhost:8000/eureka,http://localhost:8001/eureka

  eureka.client.register-with-eureka= true

  eureka.client.fetch-registry= true

 

 

 

读取配置文件

package com.example.configclient;

  

  import org.springframework.beans.factory.annotation.Value;

  import org.springframework.web.bind.annotation.RequestMapping;

  import org.springframework.web.bind.annotation.RestController;

  

  @RestController

  public class IndexController {

   @Value("${env}")

   private String env;

  

   @RequestMapping("/env")

   private String env() {

      return env;

   }

  

}

 

启动类

package com.example.configclient;

  

  import org.springframework.boot.SpringApplication;

  import org.springframework.boot.autoconfigure.SpringBootApplication;

  import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

  

  @EnableEurekaClient

@SpringBootApplication

  public class ConfigClientApplication {

  

    public static void main(String[] args) {

        SpringApplication.run(ConfigClientApplication.class, args);

    }

  

}

 

 

效果:

访问接口  读取到远程git仓库 的配置信息

动态刷新数据

在SpringCloud中有手动刷新配置文件和实时刷新配置文件两种方式。

手动方式采用actuator端点刷新数据  需要人工调用接口读取最新配置文件

实时刷新采用SpringCloud Bus消息总线

 

 

actuator端点刷新数据

 

Maven依赖信息

<!-- actuator监控中心 -->

                       <dependency>

                                   <groupId>org.springframework.boot</groupId>

                                   <artifactId>spring-boot-starter-actuator</artifactId>

                       </dependency>

 

Bootstrap.xml新增

开启监控断点

 

 

#开启所有端点

  management.endpoints.web.exposure.include= "*"

 

生效前提

在需要刷新的Bean上添加@RefreshScope注解。当配置更改时,标有@RefreshScopeBean将得到特殊处理来生效配置

 


  @Component

@RefreshScope

  public class Index {

   @Value("${env:1}")

   private String env;

  

   public String getEnv() {

      return env;

   }

  

   public void setEnv(String env) {

      this.env = env;

   }

}

 Controller



@RestController
public class newController {

    @Autowired
    Index index;
    @RequestMapping("/getenv")
    private String getenv() {
        return index.getEnv();
    }

}

手动刷新接口

Post请求手动刷新

 

http://127.0.0.1:9004/actuator/refresh

调用上面接口   启动刷新器 cofnig server读取最新配置

 

猜你喜欢

转载自blog.csdn.net/wota5037/article/details/114383940