zoukankan      html  css  js  c++  java
  • 微服务深入浅出(8)-- 配置中心Spring Cloud Config

    Config Server从本地读取配置文件

    将所有的配置文件统一写带Config Server过程的目录下,Config Server暴露Http API接口,Config Client调用Config Server的Http API来读取配置文件

    1、引入依赖

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-config-server</artifactId>
    </dependency>

    2、启动类添加@EnableConfigServer注解

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

    3、工程配置文件配置本地读取

    spring:
      cloud:
        config:
          server:
            native:
              search-locations: classpath:/shared
      profiles:
        active: native
      application:
        name: config-server
    server:
      port: 9001

    4、创建共享配置文件config-client-dev.yml

    server:
      port: 9002
    foo: foo version 1

    ==========================

    1、引入依赖

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-config-server</artifactId>
    </dependency>

    2、bootstrap.yml作为程序的配置文件(bootstrap相对于application具有优先的执行顺序)

    spring:
      application:
        name: config-client
      cloud:
        config:
          uri: http://localhost:9001
          fail-fast: true
      profiles:
        active: dev

    这样的客户端程序已经在9002端口启动,也可以像本地文件一样读取远程的配置文件:

     @Value("${foo}")
        String foo;
    
        @GetMapping("/hi")
        public String hi() {
            return foo;
        }

    Config Server从远程Git读取配置文件

    spring:
      cloud:
        config:
          server:
            git:
              uri: https://github.com/forezp/springcloudConfig
              search-paths: respo
              username: aaa
              password: aaa
          label: master
      application:
        name: config-server

    高可用Config Server集群

    1、配置Eureka Server

    <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
            </dependency>
    server:
      port: 9003
    eureka:
      client:
        register-with-eureka: false
        fetch-registry: false
        service-url:
          defaultZone: http://localhost:${server.port}/eureka
    @EnableEurekaServer
    @SpringBootApplication
    public class DemoApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(DemoApplication.class, args);
        }
    }

    2、改造config-server

    spring:
      cloud:
        config:
          server:
            native:
              search-locations: classpath:/shared
      profiles:
        active: native
      application:
        name: config-server
    server:
      port: 9001
    eureka:
      client:
        service-url:
          defaultZone: http://localhost:9003/eureka
    @EnableConfigServer
    @SpringBootApplication
    @EnableEurekaClient
    public class DemoApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(DemoApplication.class, args);
        }
    }

    3、改造config-client

    spring:
      application:
        name: config-client
      cloud:
        config:
          fail-fast: true
          discovery:
            enabled: true
            service-id: config-server
      profiles:
        active: dev
    server:
      port: 9002
    eureka:
      client:
        service-url:
          defaultZone: http://localhost:9003/eureka
    @SpringBootApplication
    @RestController
    @EnableEurekaClient
    public class DemoApplication {
    
        @Value("${foo}")
        String foo;
    
        @GetMapping("/hi")
        public String hi() {
            return foo;
        }
    
        public static void main(String[] args) {
            SpringApplication.run(DemoApplication.class, args);
        }
    }

    Spring Cloud Bus刷新配置

     只需要改造config-client部分,消息驱动

    1、引入依赖

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

    2、配置文件添加mq连接信息

    spring:
      application:
        name: config-client
      cloud:
        config:
          fail-fast: true
          discovery:
            enabled: true
            service-id: config-server
      profiles:
        active: dev
      rabbitmq:
        host:
        port:
        username:
        password:
    management:
      security:
        enabled: false

    3、在需要更新的配置类添加@RefreshScope,只有添加了该注解,才会服务不重启的情况下更新配置

    4、访问一个应用,http://localhost:9003/bus/refresh即可更新所有集成了config-client的应用的配置信息

  • 相关阅读:
    关于面向对象
    javaScrip字符串(String)相关
    http协议了解
    javascript-回归原生基础
    Java每日一面(Part1:计算机网络)[19/11/25]
    Java每日一面(Part1:计算机网络)[19/11/13]
    Java每日一面(Part1:计算机网络)[19/11/02]
    Java每日一面(Part1:计算机网络)[19/10/21]
    tomcat8 到idea控制台和servlet乱码问题
    Java每日一面(Part1:计算机网络)[19/10/14]
  • 原文地址:https://www.cnblogs.com/ijavanese/p/9201027.html
Copyright © 2011-2022 走看看