Spring Cloud實戰(二)-Spring Cloud Eureka

概要

  • 什麼是Spring Cloud Eureka?html

  • 使用Eureka獲取服務調用git

  • Eureka整合Spring Config Servergithub

  • 構建Eureka Server集羣web

什麼是Spring Cloud Eureka?

Spring Cloud Eureka 模塊提供的功能是被動式的服務發現.
什麼是服務發現?
服務發現就像聊天室一個,每一個用戶來的時候去服務器上註冊,這樣他的好友們就能看到你,你同時也將獲取好友的上線列表.
在微服務中,服務就至關於聊天室的用戶,而服務註冊中心就像聊天室服務器同樣,目前服務發現的解決方案有Eureka,Consul,Etcd,Zookeeper,SmartStack,等等.
2555949490-5714a6ed90a1e_articlex
本文就來說講Eureka,如圖所示,Eureka Client經過HTTP(或者TCP,UDP)去Eureka Server註冊和獲取服務列表,爲了高可用通常會有多個Eureka Server組成集羣.Eureka會移除那些心跳檢查未到達的服務.spring

使用Eureka獲取服務調用

這節咱們將構建一個Eureka Server,5個Eureka Client(分別提供主語,動詞,量詞,形容詞,名詞服務),再構建一個Sentence Eureka Client 來用前面五個服務造句.bootstrap

1.建立mmb-eureka-server服務器

  • 添加依賴-spring-cloud-starter-parent,spring-cloud-starter-eureka-server(pom.xml)app

<parent>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-parent</artifactId>
        <version>Brixton.SR4</version>
        <relativePath/>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka-server</artifactId>
        </dependency>
    </dependencies>
  • 配置應用信息-端口和應用名稱 application.ymldom

server:
  port: 8010

spring:
  application:
    name: mmb-eureka-server
  • 啓動服務spring-boot

@SpringBootApplication
@EnableEurekaServer
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}
  • 打開管理頁面,檢查是否成功

圖片描述
2.建立mmb-eureka-client

  • 添加依賴-spring-cloud-starter-parent,spring-cloud-starter-eureka (pom.xml)

<parent>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-parent</artifactId>
        <version>Brixton.SR4</version>
        <relativePath/>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>
    </dependencies>
  • 配置應用信息-eureka server信息,實際使用的words信息,端口號 (application.yml)

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

words: 你,我,他

server:
  port: ${PORT:${SERVER_PORT:0}}
#  這個的意思是隨機指定個沒使用的端口
  • 配置啓動信息-應用名稱 (bootstrap.xml)

spring:
  application:
    name: mmb-eureka-client-subject
  • 添加Controller-隨機獲取words中的一條

@RestController
public class Controller {

   @Value("${words}") String words;

    @RequestMapping("/")
    public  String getWord() {
        String[] wordArray = words.split(",");
        int i = (int)Math.round(Math.random() * (wordArray.length - 1));
        return wordArray[i];
    }
}
  • 啓動服務

@SpringBootApplication
@EnableEurekaClient
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}
  • 訪問127.0.0.1/port(看日誌能夠獲得各個應用的port) 看到words裏的詞就啓動成功了,

其它的verb,acticle,adjective,noun工程相似,就把words,和spring.application.name改爲對應的工程名字就行了

3.建立sentence工程

  • 添加依賴-spring-cloud-starter-parent,spring-cloud-starter-eureka,spring-boot-starter-web,spring-boot-starter-actuator (pom.xml)

<parent>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-parent</artifactId>
        <version>Brixton.SR4</version>
        <relativePath/>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</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>
    </dependencies>
  • 配置應用信息-eureka server和端口號 (application.yml)

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8010/eureka/

server:
  port: 8020
  • 配置啓動信息-應用名稱 (bootstrap.yml)

spring:
  application:
    name: mmb-eureka-sentence
  • 添加Controller-用其餘eureka-clients(subject,verb,acticle,adjective,noun)的各個服務造句

@RestController
public class Controller {

    @Autowired
    DiscoveryClient client;

    @RequestMapping("/sentence")
    public  String getSentence() {
        return
                getWord("mmb-eureka-client-subject") + " "
                        + getWord("MMB-EUREKA-CLIENT-VERB") + " "
                        + getWord("mmb-eureka-client-article") + " "
                        + getWord("mmb-eureka-client-adjective") + " "
                        + getWord("mmb-eureka-client-noun") + "."
                ;//大小寫不區分
    }

    public String getWord(String service) {
        List<ServiceInstance> list = client.getInstances(service);
        if (list != null && list.size() > 0 ) {
            URI uri = list.get(0).getUri();
            if (uri !=null ) {
                return (new RestTemplate()).getForObject(uri,String.class);
            }
        }
        return null;
    }
}
  • 啓動服務

@SpringBootApplication
@EnableEurekaServer
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}
  • 先啓動Eureka Server,再啓動Eureka Client,在管理頁面上看到服務都起成功時,訪問127.0.0.1/8020/sentence 能夠獲得一個隨機組成的句子

Eureka整合Spring Config Server

  1. 在git的repository裏添加application.yml

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8010/eureka/
  1. 啓動實戰(一)中的Spring Cloud Config Server

  2. 修改各個client的配置

    • application.yml移除屬性eureka.client.serviceUrl.defaultZone

    • bootstrap.yml添加屬性 spring.cloud.config.uri: http://localhost:8001

    • pom.xml添加依賴spring-cloud-config-client

  3. 依次啓動Config Server,Eureka Server,Eureka Client,在管理頁面上看到服務都起成功時,訪問127.0.0.1/8020/sentence 能夠獲得一個隨機組成的句子
    -

  4. 若是你想把words信息也放入repository呢?在application.yml中添加,以下信息,各個client啓動的時候加上-Dspring.profiles.active對應到相應的啓動參數就好了.

---
  spring:
    profiles: subject
  words: I,You,He,She,It
  
  ---
  spring:
    profiles: verb
  words: ran,knew,had,saw,bought

  ---
  spring:
    profiles: article
  words: a,the

  ---
  spring:
    profiles: adjective
  words: reasonable,leaky,suspicious,ordinary,unlikely

  ---
  spring:
    profiles: noun
  words: boat,book,vote,seat,backpack,partition,groundhog

構建Eureka Server集羣

  • host文件中添加 (c:WINDOWSsystem32driversetchosts).

127.0.0.1       eureka-primary
  127.0.0.1       eureka-secondary
  127.0.0.1       eureka-tertiary
  • Eureka Server的application.yml添加多個profiles,和instanceId

---
spring:
  application:
    name: eureka-server-clustered   
  profiles: primary
server:
  port: 8011  
eureka:
  instance:
    hostname: eureka-primary       
  ---
spring:
  application:
    name: eureka-server-clustered      
  profiles: secondary
server:
  port: 8012
eureka:
  instance:
    hostname: eureka-secondary       
 ---
spring:
  application:
    name: eureka-server-clustered      
  profiles: tertiary
server:
  port: 8013
eureka:
  instance:
    hostname: eureka-tertiary
  • 此時Eureka Server 同時也是個Eureka Client,須要設置eureka.client.serviceUrl.defaultZone,值是另外兩個,最終會是下面這樣

---
spring:
  application:
    name: eureka-server-clustered   
  profiles: primary
server:
  port: 8011  
eureka:
  instance:
    hostname: eureka-primary       
  client:
    registerWithEureka: true
    fetchRegistry: true        
    serviceUrl:
      defaultZone: http://eureka-secondary:8012/eureka/,http://eureka-tertiary:8013/eureka/

---
spring:
  application:
    name: eureka-server-clustered      
  profiles: secondary
server:
  port: 8012
eureka:
  instance:
    hostname: eureka-secondary       
  client:
    registerWithEureka: true
    fetchRegistry: true        
    serviceUrl:
      defaultZone: http://eureka-tertiary:8013/eureka/,http://eureka-primary:8011/eureka/

---
spring:
  application:
    name: eureka-server-clustered      
  profiles: tertiary
server:
  port: 8013
eureka:
  instance:
    hostname: eureka-tertiary       
  client:
    registerWithEureka: true
    fetchRegistry: true    
    serviceUrl:
      defaultZone: http://eureka-primary:8011/eureka/,http://eureka-secondary:8012/eureka/
  • 以-Dspring.profiles.active=primary (and secondary, and tertiary)爲啓動參數分別啓動Eureka Server

  • 修改全部Eureka Client的eureka.client.serviceUrl.defaultZone值爲http://eureka-primary:8011/eu...逗號分隔,無空白),集羣啓動成功登陸管理頁面查看,以下圖所示即成功
    圖片描述

  • 再啓動全部的Eureka Clients,查看http://localhost:8020/sentence 是否成功

  • 爲了測試容錯性,關掉兩個Eureka Client,重啓若干個Eureka Client,觀察啓動是否報錯,再去查看查看http://localhost:8020/sentence 是否成功

特別感謝 kennyk65
Spring Cloud 中文用戶組 31777218
Spring-Cloud-Config 官方文檔-中文譯本 (本人有參與,哈哈)
Spring Cloud Netflix 官網文檔-中文譯本
本文實例github地址 mmb-eureka

相關文章
相關標籤/搜索