关于java:分布式配置管理-Spring-Cloud-Config

34次阅读

共计 2546 个字符,预计需要花费 7 分钟才能阅读完成。

在微服务开发过程中,为了保障咱们服务的高可用,不可避免的须要配置多台服务器组成集群,而集群中每一台主机的配置文件都是雷同的,对配置文件的更新保护就成为了一个辣手的问题,针对于该问题,spring cloud 官网给出了一个解决方案,应用 Spring Cloud Config 配置核心来治理所有的配置文件

原理
Spring Cloud Config 就是对微服务的配置文件进行对立治理的。其工作原理是,咱们首先须要将各个微服务公共的配置信息推送到 GitHub 近程版本库。而后咱们再定义一个 Spring Cloud Config Server,其会连贯上这个 GitHub 近程库。这样咱们就能够定义 Config 版的 Eureka
Server、提供者与消费者了,它们都将作为 Spring Cloud Config Client 呈现,它们都会通过连贯 Spring Cloud Config Server 连贯上 GitHub 上的近程库,以读取到指定配置文件中的内容。

疾速体验

2.1 Eureka 服务端

<!-- eureka 服务端依赖 -->
<dependency>
 <groupId>org.springframework.cloud</groupId>
 <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>

application.yml

server:
  port: 10080
spring:
  application:
    name: eureka-server
eureka:
  client:
    service-url:
      defaultZone: http://127.0.0.1:10000/eureka
    register-with-eureka: false
 fetch-registry: false

启动类

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

2.2 spring cloud config

<!-- eureka client -->
<dependency>
 <groupId>org.springframework.cloud</groupId>
 <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!-- spring cloud config -->
<dependency>
 <groupId>org.springframework.cloud</groupId>
 <artifactId>spring-cloud-config-server</artifactId>
</dependency>

application.yml

server:
  port: 10010
spring:
  application:
    name: config-server
  cloud:
    config:
      server:
        git:
          uri: ${git 仓库地址}
          default-label: master
          timeout: 5
          #若网络须要代理配置,则加上 proxy 配置
          #proxy:
          #  https: 
          #    host: 
          #    port: 
          #    username: 
          #    password:
          username: ${git 用户名}
          password: ${git 明码}
  
eureka:
  client:
    service-url:
      defaultZone: http://127.0.0.1:10080/eureka
  instance:
    prefer-ip-address: true
 ip-address: 127.0.0.1

启动类

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

spring cloud config 客户端

<dependency>
 <groupId>org.springframework.cloud</groupId>
 <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

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

application.yml

spring:
  cloud:
    config:
      name: bill-server #与 git 中的配置文件 application 名统一
      profile: dev
      label: master
      discovery:
        enabled: true
        service-id: config-server #服务名

eureka:
  client:
    service-url:
      defaultZone: http://127.0.0.1:10080/eureka
    register-with-eureka: true
 fetch-registry: true
 instance:
    ip-address: 127.0.0.1
    prefer-ip-address: true
management:
  endpoints:
    web:
      exposure:
        include: bus-refresh

启动类

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

正文完
 0