SpringCloud(3)---Eureka服務註冊與發現

Eureka服務註冊與發現

 GitHub地址https://github.com/yudiandemingzi/spring-cloud-studygit

1、Eureka概述

 一、Eureka特色

  (1) Eureka是一個基於REST的服務,用於定位服務,以實現雲端中間層服務發現和故障轉移。github

  (2) Eureka 主管服務註冊與發現,在微服務中,之後了這二者,只須要使用服務的標識符(==就是那個在每一個服務的yml文件中取得服務名稱==),spring

          就能夠訪問到服務,不須要修改服務調用的配置文件。app

  (3) Eureka遵循AP原則(高可用,分區容錯性),由於使用了自我保護機制因此保證了高可用。ide

二、Eureka兩大組件

    兩大組件Eureka Server(提供註冊服務) Eureka Client(JAVA客戶端,負責發送心跳)微服務

   系統中的其餘微服務使用Eureka客戶端鏈接到Eureka服務端維持心跳鏈接(即註冊)。SpringCloud的其餘模塊能夠經過Eureka Server 來發現系統中的微服務並加以調用fetch

三、Eureka三大角色

            Eureka Server:提供服務註冊和發現spa

        Service Provider:服務提供方,將自身服務註冊到Eureka,從而使服務消費方可以找到code

     Service Consumer:服務消費方,從Eureka獲取註冊服務列表,從而可以消費服務。server

 

 

2、Eureka Server服務註冊中心

    一、pom.xml

<!--註冊服務中心的jar要多個-server-->
<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka-server</artifactId>
  </dependency>

    二、application.yml

server:
  port: 7001 eureka:
  instance:
    hostname: localhost
  client:
  #聲明本身是個服務端
    registerWithEureka: false    #false表示不向註冊中心註冊本身
    fetchRegistry: false         #false表示本身就是註冊中心,職責是維護實例,不參加檢索
    serviceUrl:                  #設置eureka server的交互地址,即對外暴露的地址
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

    三、啓動類

//注意:要在類前加@EnableEurekaServer標註
@SpringBootApplication
@EnableEurekaServer public class Eureka7001_APP {
    public static void main(String[] args) {
        SpringApplication.run(Eureka7001_APP.class,args);
    }
}

運行結果:輸入:http://localhost:7001/

 

3、Service Provider服務提供方

假設這個商品微服務。

   一、pom.xml

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

   二、application.yml

server:
  port: 8001
#指定註冊中心地址
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:7001/eureka/
#服務的名稱
spring:
  application:
    name: product-service

     三、啓動類

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

    四、啓動後查看服務註冊中心

 發如今服務註冊中心已經註冊了一個服務

    五、換端口號再啓動一個

   六、在看服務中心

 

這就是搭建了商品微服務集羣。

 

4、Service Consumer服務消費方

      其實服務方和消費在配置時候沒有任何區別,它們都屬於Eureka Client組件。只是涉及服務間的調用,因此就把被調方稱爲提供方,調用方稱爲消費方。就比如訂單微服務,

訂單服務確定須要去調商品微服務,因此這個訂單微服務對於商品來說能夠理解服務提供方。一個微服務便可以是服務方也同時是提供方。

      一、pom.xml

    <!--這個對於每一個不是註冊中心的微服務都要添加-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

     二、application.yml

server:
  port: 9001

#指定註冊中心地址
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:7001/eureka/

#服務的名稱
spring:
  application:
    name: order-service

    三、啓動類

@SpringBootApplication
public class OrderApplication {

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

四、查看註冊中心

發現訂單微服務也成功註冊到註冊中心

 

至於訂單微服務如何調商品微服務呢,下一遍博客在寫咯。

 

     我只是偶爾安靜下來,對過去的種種思忖一番。那些曾經的舊時光裏即使有過天真愚鈍,也不值得譴責。畢竟,日後的日子,還很長。不斷鼓勵本身,

 天一亮,又是嶄新的起點,又是未知的征程(上校5)

相關文章
相關標籤/搜索