springCloud學習-高可用的分佈式配置中心(Spring Cloud Config)

一、簡介git

  高可用的分佈式配置中心,即將配置中心作成一個微服務,將其集羣化,從而達到高可用。config-server和config-client向eureka-server註冊,且將config-server多實例集羣化部署github

二、改造config-server
web

   一、咱們使用以前建立的eureka-server工程做爲註冊中心,端口8761spring

   二、咱們將config-server工程,看成eureka-client,須要在pom.xml中引入spring-cloud-starter-netflix-eureka-client依賴,代碼以下apache

 

<?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">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.lishun</groupId>
    <artifactId>config-server</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>config-server</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>com.lishun</groupId>
        <artifactId>cloud</artifactId>
        <version>1.0-SNAPSHOT</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>


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


</project>

 

  三、在工程的啓動類ConfigServerApplication上加上註解@EnableEurekaClient,開啓eurekaclient的功能,代碼以下bootstrap

@EnableEurekaClient
@EnableConfigServer
@SpringBootApplication
public class ConfigServerApplication {

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

  四、在application.properties配置文件中,指定服務註冊地址,代碼以下瀏覽器

spring.application.name=config-server
server.port=8889

#spring.cloud.config.server.native.search-locations= classpath:/shared
#spring.profiles.active=native

spring.cloud.config.server.git.uri=https://github.com/lis-ylfy/config-test/
spring.cloud.config.server.git.searchPaths=lis
spring.cloud.config.label=master
spring.cloud.config.server.git.username=
spring.cloud.config.server.git.password=
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/

三、改造config-clientapp

  一、將config-client也做爲eureka-client,在pom.xml文件中加入spring-cloud-starter-netflix-eureka-client依賴,在工程的啓動類上加上註解@EnableEurekaClient,開啓eurekaclient的功能負載均衡

    二、在工程的配置文件bootstrap.properties中指定服務的註冊地址爲http://localhost:8761/eureka,指定向serviceId爲config-server的配置服務讀取配置文件,代碼以下maven

spring.application.name=config-client
spring.cloud.config.uri= http://localhost:8888/
spring.cloud.config.fail-fast=true
spring.profiles.active=dev
server.port=8881


eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/
spring.cloud.config.discovery.enabled=true
spring.cloud.config.discovery.serviceId=config-server

  三、依次啓動 eureka-server、 config-server 和 config-client 工程,注意這裏須要 config-server 啓動成功井且向 eureka-server 註冊完成後,才能啓動 config-client,不然 config-client 找不到config-server 。

  四、訪問http://localhost:8881/hi,瀏覽器顯示:

config-test

  五、實現config-server的高可用

  用idea開啓多個config-server實例,端口分別爲8888和8889,而後再開啓多個config-client實例,從控制能夠看到它輪流從8888和8889兩個config-server中讀取了配置文件,並實現了負載均衡。

相關文章
相關標籤/搜索