spring cloud config與eureka配合使用

前面兩篇介紹了Spring Cloud Config服務端客戶端的簡單配置,本篇介紹Spring Cloud Config與Eureka配合使用html

 

前言git

默認狀況下,配置客戶端啓動時,都是經過配置屬性 spring.cloud.config.uri 綁定到配置服務器,並使用遠程屬性初始化 Spring Environment。這樣作的最終結果是全部想要使用Config Server的客戶端必須在bootstrap.yml中配置 spring.cloud.config.uri (默認是"http://localhost:8888")。github

若是您正在使用DiscoveryClient實現,可將ConfigServer與Eureka等註冊中心聯合使用(目前Spring Cloud只支持與Eureka及Consul聯合使用,不支持與Zookeeper聯合使用)。可是若是配置了 spring.cloud.config.uri ,客戶端將沒法利用註冊web

使用服務發現的壞處是啓動時額外的網絡往返,以定位服務註冊。好處是配置服務器能夠更改其座標,只要發現服務是一個固定點(如項目名稱不變)。spring

 

準備工做bootstrap

一、啓動Eureka服務器(很簡單,這裏就不演示了)。啓動成功後訪問http://localhost:8761,以下圖所示:服務器

 

配置服務器代碼示例網絡

在pom文件中增長依賴:app

        <dependency>
            <!-- 配置中心服務端 -->
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
        <dependency>
            <!-- eureka客戶端 -->
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>

啓動類開啓服務發現:spring-boot

@SpringBootApplication
@EnableConfigServer // 經過@EnableConfigServer註解激活配置服務
@EnableDiscoveryClient // 開啓服務發現
public class ConfigServerApplication {

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

}

配置文件,application.yml

server:
  port: 18083
  
spring:
  application:
    name: config-server #應用程序名稱
  cloud:
    config:
      server:
        git:
          uri: https://github.com/xuwenjin/config-repo-xwj #git上配置中心地址
          
eureka:
  client:
    serviceUrl:
      defaultZone: http://admin:admin@localhost:8761/eureka
  instance:
    prefer-ip-address: true #當猜想主機名時,服務器的IP地址應該在操做系統報告的主機名中使用

 

配置客戶端代碼示例

在pom中增長依賴:

       <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <!-- 配置中心客戶端 -->
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
        <dependency>
            <!-- 監控管理(如:狀態頁和健康指標) -->
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <!-- eureka客戶端 -->
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>

啓動類增長服務發現:

@SpringBootApplication
@EnableDiscoveryClient // 開啓服務發現
public class ConfigClientApplication {

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

}

增長一個Controller,用於測試請求:

@RestController
@RefreshScope
public class IndexController {

    @Value("${profile}")
    private String profile;

    @RequestMapping("/profile")
    public String getProfile() {
        return profile;
    }

}

配置文件,bootstrap.yml

spring:
  application:
    name: config-client
  cloud:
    config:
      profile: test   #對應spring.profiles.active
      label: master   #分支名。當使用配置服務器是git時,默認是master
      username: user  #配置服務器的用戶名密碼,此配置會覆蓋uri中的配置
      password: password123
      discovery: enabled: true  #默認false,設爲true表示使用註冊中心中的configserver配置,而不是本身配置configserver的uri
        service-id: CONFIG-SERVER #指定config server在服務發現中的serviceId,默認爲:configserver
     
eureka:
  client:
    serviceUrl:
      defaultZone: http://admin:admin@localhost:8761/eureka
  instance:
    prefer-ip-address: true #當猜想主機名時,服務器的IP地址應該在操做系統報告的主機名中使用

配置文件,application.yml

server:
  port: 18084

從示例代碼能夠看到,想要將Config Server與註冊中心聯合使用,只須要在客戶端配置 spring.cloud.config.discovery.enabled:truespring.cloud.config.discovery.serviceId 兩個配置項便可(serviceId是註冊到Eureka中的Application)。

 

測試工做

一、啓動配置服務器,會發現Eureka中增長了該示例

二、啓動配置客戶端,在日誌中能夠看到客戶端發現了服務器的地址:

三、訪問http://localhost:18084/profile,返回配置信息。至此配置完成~

相關文章
相關標籤/搜索