SpringCloud微服務(wù)(01):Eureka組件,管理服務(wù)注冊與發(fā)現(xiàn)

本文源碼:GitHub·點這里 || GitEE·點這里

創(chuàng)新互聯(lián)服務(wù)項目包括錫林郭勒盟網(wǎng)站建設(shè)、錫林郭勒盟網(wǎng)站制作、錫林郭勒盟網(wǎng)頁制作以及錫林郭勒盟網(wǎng)絡(luò)營銷策劃等。多年來,我們專注于互聯(lián)網(wǎng)行業(yè),利用自身積累的技術(shù)優(yōu)勢、行業(yè)經(jīng)驗、深度合作伙伴關(guān)系等,向廣大中小型企業(yè)、政府機構(gòu)等提供互聯(lián)網(wǎng)行業(yè)的解決方案,錫林郭勒盟網(wǎng)站推廣取得了明顯的社會效益與經(jīng)濟效益。目前,我們服務(wù)的客戶以成都為中心已經(jīng)輻射到錫林郭勒盟省份的部分城市,未來相信會繼續(xù)擴大服務(wù)區(qū)域并繼續(xù)獲得客戶的支持與信任!

一、Eureka基本架構(gòu)

1、Eureka簡介

Eureka是Netflix開發(fā)的服務(wù)發(fā)現(xiàn)框架,本身是一個基于REST的服務(wù),SpringCloud將它集成在其子項目spring-cloud-netflix中,以實現(xiàn)SpringCloud的服務(wù)發(fā)現(xiàn)功能。

2、Eureka角色結(jié)構(gòu)圖

SpringCloud微服務(wù)(01):Eureka組件,管理服務(wù)注冊與發(fā)現(xiàn)
角色職責如下:
1)、Register:服務(wù)注冊中心,它是一個Eureka Server ,提供服務(wù)注冊和發(fā)現(xiàn)功能。
2)、Provider:服務(wù)提供者,它是一個Eureka Client ,提供服務(wù)。
3)、Consumer:服務(wù)消費者,它是一個Eureka Cient ,消費服務(wù)。

3、Eureka中幾個核心概念

1)、Registe服務(wù)注冊
當Client向Server 注冊時,Client 提供自身的元數(shù)據(jù),比如IP 地址、端口、運行狀況指標的Uri 、主頁地址等信息。
2)、Renew服務(wù)續(xù)約
Client 在默認的情況下會每隔30 秒發(fā)送一次心跳來進行服務(wù)續(xù)約。通過服務(wù)續(xù)約來告知Server該Client仍然可用。正常情況下,如果Server在90 秒內(nèi)沒有收到Client 的心跳,Server會將Client 實例從注冊列表中刪除。官網(wǎng)建議不要更改服務(wù)續(xù)約的間隔時間。
3)、Fetch Registries獲取服務(wù)注冊列表信息
Client 從Server 獲取服務(wù)注冊表信息,井將其緩存在本地。Client 會使用服務(wù)注冊列表信息查找其他服務(wù)的信息,從而進行遠程調(diào)用。該注冊列表信息定時(每30 秒) 更新一次。
4)Cancel服務(wù)下線
Client 在程序關(guān)閉時可以向Eureka Server 發(fā)送下線請求。發(fā)送請求后,該客戶端的實例信息將從Server 的服務(wù)注冊列表中刪除。該下線請求不會自動完成,需要在程序關(guān)閉時調(diào)用以下代碼:

DiscoveryManager.getinstance().shutdownComponent();

5) Eviction服務(wù)下線
在默認情況下,當Client 連續(xù)90 秒沒有向Server 發(fā)送服務(wù)續(xù)約(即心跳〉時,Server 會將該服務(wù)實例從服務(wù)注冊列表刪除,即服務(wù)下線。

二、Eureka案例代碼

1、項目基本結(jié)構(gòu)圖

主要包括兩個注冊中心(集群)
node01-eureka-7001
node01-eureka-7002
一個服務(wù)提供方
node01-provider-8001
一個服務(wù)消費方
node01-consume-8002

2、配置本機的Host文件

# cloud host
127.0.0.1 registry01.com
127.0.0.1 registry02.com
127.0.0.1 provider-8001.com

3、注冊中心代碼

1)Eureka注冊中心依賴

<dependencies>
    <!--eureka-server服務(wù)端 -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-eureka-server</artifactId>
    </dependency>
</dependencies>

2)核心配置文件

server:
  port: 7001
spring:
  application:
    name: node01-eureka-7001
eureka:
  instance:
    hostname: registry01.com
    prefer-ip-address: true
  client:
    # false表示不向注冊中心注冊自己
    register-with-eureka: false
    # false表示該端就是注冊中心,維護服務(wù)實例,不去檢索服務(wù)
    fetch-registry: false
    service-url:
      # 單點注冊中心
      # defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
      # 集群注冊中心
      defaultZone: http://registry02.com:7002/eureka/

這里采用集群的配置,如果有多個集群,逗號分隔,如下寫法就好:

defaultZone: 
http://registry02.com:7002/eureka/,
http://registry02.com:7002/eureka/

3)啟動類注解

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer // 注冊中心注解
public class Application_7001 {
    public static void main(String[] args) {
        SpringApplication.run(Application_7001.class,args) ;
    }
}

這樣注冊中心代碼完成。
4)啟動項目,如圖
SpringCloud微服務(wù)(01):Eureka組件,管理服務(wù)注冊與發(fā)現(xiàn)
暫時沒有服務(wù)注冊進來。

4、服務(wù)提供方代碼

1)核心依賴

<dependencies>
    <!-- 將微服務(wù)provider側(cè)注冊進eureka -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-eureka</artifactId>
    </dependency>
</dependencies>

2)配置文件如下

server:
  port: 8003
spring:
  application:
    name: node01-provider-8001
eureka:
  instance:
    hostname: provider-8001
    prefer-ip-address: true
  client:
    service-url:
      # 集群注冊中心
      defaultZone: http://registry01.com:7001/eureka/,http://registry02.com:7002/eureka/

3)提供一個接口服務(wù)

@RestController
public class ProviderController {
    @RequestMapping("/getInfo")
    public Map<String,String> getInfo (){
        Map<String,String> infoMap = new HashMap<>() ;
        infoMap.put("作者:","知了一笑") ;
        infoMap.put("時間:","2019-05-18") ;
        infoMap.put("主題:","SpringCloud微服務(wù)框架") ;
        return infoMap ;
    }
}

4)啟動類上需要注解

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@SpringBootApplication
@EnableEurekaClient    // 本服務(wù)啟動后會自動注冊進eureka服務(wù)中
@EnableDiscoveryClient
public class Application_8001 {
    public static void main(String[] args) {
        SpringApplication.run(Application_8001.class,args) ;
    }
}

5、服務(wù)消費方代碼

服務(wù)消費方和提供方的代碼邏輯基本一致。
1)配置文件
這里不需要注冊自己,只是單純從注冊中心獲取服務(wù)提供方的消息。

server:
  port: 8002
spring:
  application:
    name: node01-consume-8002
eureka:
  client:
    register-with-eureka: false
    service-url:
      # 集群注冊中心
      defaultZone: http://registry01.com:7001/eureka/,http://registry02.com:7002/eureka/

2)消費服務(wù)代碼
注意這里的【server_name】就服務(wù)提供方的

spring:
  application:
    name: node01-provider-8001

使用RestTemplate調(diào)用服務(wù)。

@RestController
public class ConsumeController {
    @Autowired
    private RestTemplate restTemplate ;
    String server_name = "node01-provider-8001" ;
    @RequestMapping("/showInfo")
    public Map<String,String> showInfo (){
        return restTemplate.getForObject("http://"+server_name+":8001/getInfo",Map.class) ;
    }
}

到這里,一個基于Eureka的服務(wù)注冊與發(fā)現(xiàn)就完成了。
3)四個服務(wù)全部啟動
SpringCloud微服務(wù)(01):Eureka組件,管理服務(wù)注冊與發(fā)現(xiàn)
4)訪問如下地址

http://localhost:8002/showInfo

獲取服務(wù)接口結(jié)果

{
    "主題:": "SpringCloud微服務(wù)框架",
    "作者:": "知了一笑",
    "時間:": "2019-05-18"
}

三、源代碼案例

GitHub·地址
https://github.com/cicadasmile/spring-cloud-base
GitEE·地址
https://gitee.com/cicadasmile/spring-cloud-base

當前題目:SpringCloud微服務(wù)(01):Eureka組件,管理服務(wù)注冊與發(fā)現(xiàn)
標題網(wǎng)址:http://www.muchs.cn/article42/iidihc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供云服務(wù)器、微信小程序App開發(fā)、網(wǎng)站設(shè)計網(wǎng)站改版、域名注冊

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)站建設(shè)公司