1. 程式人生 > >springCloud(3):微服務的註冊與發現(Eureka)

springCloud(3):微服務的註冊與發現(Eureka)

springcloud 微服務的註冊與發現 eureka

一、簡介

服務消費者需要一個強大的服務發現機制,服務消費者使用這種機制獲取服務提供者的網絡信息。即使服務提供者的信息發生變化,服務消費者也無須修改配置。


服務提供者、服務消費者、服務發現組件三者之間的關系大致如下:

1.各個微服務在啟動時,將自己的網絡地址等信息註冊到服務發現組件中,服務發現組件會存儲這些信息。

2.服務消費者可以從服務發現組件查詢服務提供者的網絡地址,並使用該地址調用服務提供者的接口。

3.各個微服務與服務發現組件使用一定機制(如:心跳)通信,服務發現組件如長時間無法與某微服務實例通信,就會註銷該實例。

4.微服務網絡地址發生變更時,會重新註冊到服務發現組件。

1.1、服務發現組件

服務發現組件應具備以下功能:服務註冊表、服務註冊與服務發現、服務檢查

1.1.1、服務註冊表

是服務發現組件的核心,用來記錄各個微服務的信息,如服務名稱、IP、端口等。服務註冊表提供查詢API(用於查詢可用的微服務實例)和管理API(用戶服務的註冊與註銷)

1.1.2、服務註冊與服務發現

服務註冊是指微服務在啟動時,將自己的信息註冊到服務發現組件上的過程。服務發現是指查詢可用微服務列表及其網絡地址的機制。

1.1.3、服務檢查

服務發現組件使用一定機制定時檢測已註冊的服務,如發現某實例長時間無法訪問,就會從服務註冊表中移除實例


Spring Cloud提供了多種服務發現組件的支持,如:Eureka、Zookeeper等。

二、Eureka

Eureka是Netflix開源的服務發現組件,本身是基於REST的服務。它包含Server和Client兩部分。Spring Cloud將其集成在子項目Spring Cloud Netflix中,從而實現微服務的註冊與發現。

2.1、編寫Eureka Server

2.1.1 新建springboot工程

新建一個springboot(1.5.4.RELEASE)工程,再添加如下依賴:

<dependencyManagement>
    <dependencies>
        <dependency>
	    <groupId>org.springframework.cloud</groupId>
	    <artifactId>spring-cloud-dependencies</artifactId>
	    <version>Dalston.SR1</version>
	    <type>pom</type>
	    <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>
<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-config</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-eureka</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-eureka-server</artifactId>
    </dependency>
</dependencies>

2.1.2、編寫啟動類

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {

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

2.1.3、配置文件application.properties

server.port=9090
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
eureka.client.service-url.defaultZone=http://localhost:9090/eureka/,http://localhost:9090/eureka1/

說明:

eureka.client.register-with-eureka:表示是否將自己註冊到Eureka Server,默認為true.(由於此工程本身就是Eureka Server,所有這裏寫false)

eureka.client.fetch-registry:表示是否從Eureka Server獲取信息,默認為true(由於此工程現在是單節點的,所有這裏寫false)

eureka.client.service-url.defaultZone:設置與Eureka Server交互的地址,查詢服務和註冊服務都需要依賴這個地址,多個用逗號分隔


2.1.4、測試

訪問:http://localhost:9090/

技術分享

2.2、將微服務註冊到Eureka Server上

2.2.1 新建springboot工程

新建一個springboot(1.5.4.RELEASE)工程,再添加如下依賴:

<dependencyManagement>
    <dependencies>
        <dependency>
	    <groupId>org.springframework.cloud</groupId>
	    <artifactId>spring-cloud-dependencies</artifactId>
	    <version>Dalston.SR1</version>
	    <type>pom</type>
	    <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>
<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-config</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-eureka</artifactId>
    </dependency>
</dependencies>

2.2.2、編寫啟動類

package com.example.demo;

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

@EnableEurekaClient
@SpringBootApplication
public class EurekaClientApplication {

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

2.2.3、配置文件application.properties

server.port=9091
spring.application.name=demo1
eureka.client.service-url.defaultZone=http://localhost:9090/eureka/
eureka.instance.prefer-ip-address=true

2.2.4、測試

訪問:http://localhost:9090/

技術分享


如果在Eureka Server的首頁看到以下這段提示,則說明Eureka已經進入了保護模式:

EMERGENCY! EUREKA MAY BE INCORRECTLY CLAIMING INSTANCES ARE UP WHEN THEY‘RE NOT. RENEWALS ARE LESSER THAN THRESHOLD AND HENCE THE INSTANCES ARE NOT BEING EXPIRED JUST TO BE SAFE.

解決:顯示指定ip

server.port=9091
spring.application.name=demo1
eureka.client.service-url.defaultZone=http://localhost:9090/eureka/
eureka.instance.prefer-ip-address=true
#顯示指定IP
eureka.instance.instance-id: ${spring.cloud.client.ipAddress}:${server.port}

技術分享


本文出自 “我愛大金子” 博客,請務必保留此出處http://1754966750.blog.51cto.com/7455444/1941405

springCloud(3):微服務的註冊與發現(Eureka)