springcloud配置中心的设置

什么是springcloud的配置中心
spring cloud config 为微服务架构中的微服务提供集中化的外部配置支持,配置服务器为各个不同的微服务应用的所有环境提供了一个中心化的外部配置
在这里插入图片描述
spring cloud config能干什么

  1. 集中化的管理配置文件
  2. 不同的环境不同配置,动态化的配置更新,
  3. 运行期间动态调整配置,不再需要在每个服务部署的机器上编写配置文件,服务会向配置中心统一拉取配置自己的信息
  4. 当配置发生变化时,服务不需要再重启即可感知到配置的比那花并应用最新的配置
  5. 将配置信息以rest的接口风格暴露

通过github配置配置中心

  1. 在github上新建仓库spring-cloud-config的新repository
  2. 本地clone后进行修改,添加不同环境的配置文件
  3. 新建module模块进行设置配置中心

application.yml

server:
  port: 3344
spring:
  application:
    name: cloud-config-center  # 注册进eureka服务器的微服务名字
  cloud:
    config:
      server:
        git:
          uri: https://github.com/sofencyXiao/spring-cloud-config.git
          search-paths:
            - spring-cloud-config  #搜索目录
      label: master  #读取分支


eureka:
  client:
    service-url:
      defaultZone: http://localhost:7001/eureka

启动类

@SpringBootApplication
@EnableDiscoveryClient
@EnableConfigServer  // 启动配置服务器
public class ConfigCenterApplication {
    
    
    public static void main(String[] args) {
    
    
        SpringApplication.run(ConfigCenterApplication.class,args);
    }
}

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>Spring Cloud学习</artifactId>
        <groupId>org.example</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>ConfigCenter</artifactId>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</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-actuator</artifactId>
        </dependency>
        <!--eureka client-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
    </dependencies>
</project>

配置客户端可以访问到服务端的配置信息
新建模块测试配置中心客户端
pom.xml的设置

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>Spring Cloud学习</artifactId>
        <groupId>org.example</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>ConfigClient</artifactId>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</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-actuator</artifactId>
        </dependency>
        <!--eureka client-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
    </dependencies>

</project>

bootsrtap.yml 是系统级的配置文件 加载顺序优先于application.yml
配置如下

server:
  port: 3355

spring:
  application:
    name: config-client
  cloud:
    #客户端配置
    config:
      label: master  #分支名称  github配置中心的分支
      name: config  #配置文件的名字  对应github上面的文件就是{name}-{profile}.yml
      profile: dev  #读取的后缀名称
      uri: http://localhost:3344  #配置中心的地址
#服务注册到eureka
eureka:
  client:
    service-url:
      defaultZone: http://localhost:7001/eureka

客户端的启动类

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

测试controller是否可以访问到github上的配置文件

@RestController
public class ConfigClientController {
    
    


    @Value("${config.info}")  //github配置中心上面config-dev.yml下的内容config.info的信息
    private String configInfo;

    @GetMapping("/configInfo")
    public String getConfigInfo(){
    
    
        return configInfo;//
    }
}

上述配置出现的问题就是 我们在github上修改了配置后 在3344的应用下访问http://localhost:3344/master/config-dev.yml可以访问到github上的最新信息,但是在3355上通过接口访问配置文件却显示的是原来的信息,没有及时的进行更新操作.
除非重启.

但是随着服务的越来越多,如果每次修改都要重启业务类模块,非常耗时,因此需要引入一个注解,并且加上actuor的监控

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

@RefreshScope,并且再需要使用post请求 curl -X POST "http://localhost:3355/actuator/refresh"

@RestController
@RefreshScope
public class ConfigClientController {
    
    
    @Value("${config.info}")  //github配置中心上面config-dev.yml下的内容config.info的信息
    private String configInfo;

    @GetMapping("/configInfo")
    public String getConfigInfo(){
    
    
        return configInfo;//
    }
}

但是微服务多的化 还要每次都执行执行post请求激活客户端吗?因此引入bus总线的概念

猜你喜欢

转载自blog.csdn.net/qq_43079376/article/details/108314485