天天看點

Spring Cloud學習(2)-----Eureka引言一.環境搭建二.功能體系

思維導圖:

Spring Cloud學習(2)-----Eureka引言一.環境搭建二.功能體系

引言

    Spring Cloud Eureka是Spring Cloud Netflix微服務套件中的一部分,它基于Netflix Eureka做了二次封裝,主要負責完成微服務架構中的服務治理功能.

    服務治理是微服務架構中最為核心和基礎的子產品,他主要實作微服務執行個體的注冊與發現.為什麼我們需要服務治理來管理微服務執行個體呢?當微服務執行個體較少時,我們可以通過某些靜态配置來進行服務的聲明和調用,随着業務的發展,微服務執行個體個數增加時,再使用靜态申明就會導緻極大的維護成本.

    本文會通過 使用部分:環境搭建 ,原理部分:功能體系 來介紹Eureka的簡單使用.

一.環境搭建

    Eureka的環境主要分為服務端和用戶端.服務端也稱為注冊中心.每個微服務都需要向注冊中心注冊自己能提供的服務.用戶端則會将自己的服務向注冊中心注冊,并向注冊中心擷取可以調用的服務.

1.1 搭建注冊中心

    我們将以單節點和雙節點的方式來搭建注冊中心.

1.1.1 單節點注冊中心

    首先使用Spring Boot搭建一個基礎環境,以下是Maven引用:

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.3.7.RELEASE</version>
        <relativePath/>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka-server</artifactId>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Brixton.SR5</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
           

随後,在Application.class處使用@EnableEurekaServer啟用EurekaServer 

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

最後,配置Eureka相關參數

## 服務名稱
spring.application.name=eureka-server
##服務端口号
server.port=1111

## 是否向注冊中心注冊自己.單節點的注冊中心不需要此功能
eureka.client.register-with-eureka=false
## 是否需要檢索可用微服務.單節點的注冊中心不需要此功能
eureka.client.fetch-registry=false
## Eureka服務URL
eureka.client.service-url.defaultZone=http://localhost:1111/eureka/
           

啟動服務,進入http://localhost:1111/ 可以看到注冊中心的相關資訊

Spring Cloud學習(2)-----Eureka引言一.環境搭建二.功能體系

1.1.2 雙節點注冊中心

    對于分布式架構來說,單節點注冊中心如果出現錯誤将是緻命性的.是以,注冊中心至少 得有兩個.使用多節點的注冊中心式,每個注冊中心會向其他注冊中心注冊,是以兩台注冊中心的配置如下:

peer1:

## 服務名稱
spring.application.name=eureka-server
##服務端口号
server.port=1111

## 執行個體主機位址
eureka.instance.hostname=peer1
## 服務URL
eureka.client.service-url.defaultZone=http://peer2:1112/eureka/
           

peer2:

spring.application.name=eureka-server
server.port=1112
eureka.instance.hostname=peer2
eureka.client.service-url.defaultZone=http://peer1:1111/eureka/
           

可以看到,兩台服務注冊中心會互相注冊,是以需要将一下參數設定為true

  • eureka.client.register-with-eureka
  • eureka.client.fetch-registry

然後,在配置中,我們看到微服務執行個體位址為peer1和peer2,即eureka.instance.hostname.這是因為我在本地測試,是以還需要再host檔案中做 相關映射.Window hosts檔案位址:C:\Windows\System32\drivers\etc\hosts,在檔案中添加兩行:

127.0.0.1 peer1
127.0.0.1 peer2
           

我們分别啟動配置不同的兩個注冊中心服務.進入http://localhost:1111/ 或者http://localhost:1112/可以看到如下界面:

Spring Cloud學習(2)-----Eureka引言一.環境搭建二.功能體系

 至此,雙節點注冊中心搭建完畢.

1.2 服務提供者

接下來,我們嘗試向注冊中心注冊服務.當然,Eureka用戶端的Maven引用稍稍不同于Eureka服務端:

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.3.7.RELEASE</version>
        <relativePath/>
    </parent>
    
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

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

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Brixton.SR5</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
           

需要使用@EnableDiscoveryClient開啟Eureka用戶端服務

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

最後,相關配置為:

spring.application.name=hello-client-service

eureka.client.service-url.defaultZone=http://localhost:1111/eureka/,http://localhost:1112/eureka/
           

至現在位置,我們這個服務提供者還沒有一個可供調用的服務.是以添加一個簡單的Controller,用于列印目前注冊中心資訊的接口.

@RestController
public class HelloController {
    @Autowired
    private DiscoveryClient discoveryClient;

    @RequestMapping(value = "/hello",method = RequestMethod.GET)
    public String index(){
        ServiceInstance serviceInstance = discoveryClient.getLocalServiceInstance();
        System.out.println("host:" + serviceInstance.getHost() + ",serviceId:" + serviceInstance.getServiceId());
        return "hello world";
    }
}
           

啟動服務,可以看到Eureka的管理頁面中有這一服務的資訊了.

Spring Cloud學習(2)-----Eureka引言一.環境搭建二.功能體系

 手動調用此接口http://localhost:8080/hello,可以看到hello world.

最後,将此producer服務提供者打包後,我們使用指令用不同的端口後再次啟動此服務.

java -jar eureka-client-1.0-SNAPSHOT.jar --server.port=8081
java -jar eureka-client-1.0-SNAPSHOT.jar --server.port=8082
           

可以看到,頁面上已有三個同名的服務了,他們的端口号分别是8080,8081,8082

Spring Cloud學習(2)-----Eureka引言一.環境搭建二.功能體系

 1.3 服務消費者

    現在,我們有了雙節點的服務注冊中心,和三個一樣的服務提供者.接下來,我們建構具有簡單的負載均衡的服務消費者來擷取并調用服務.

消費者配置:

spring.application.name=consumer-service
server.port=9000
eureka.client.service-url.defaultZone=http://localhost:1111/eureka/,http://localhost:1112/eureka/
           

    服務消費者和服務提供者的配置完全一樣.隻是因為我們希望在這個消費者進行負載均衡,是以需要引入Ribbon.

<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-ribbon</artifactId>
        </dependency>
           

接下來,我們需要調用服務了.,這個HELLO-CLIENT-SERVICE是我服務提供者的spring.application.name=hello-client-service

@RestController
public class ConsumerController {
    @Autowired
    private RestTemplate restTemplate;

    @RequestMapping(value = "/consume",method = RequestMethod.GET)
    public String consume(){
        return restTemplate.getForEntity("http://HELLO-CLIENT-SERVICE/hello",String.class).getBody();
    }
}
           

接下來,多次調用http://localhost:9000/consume,可以看到,多台服務提供者都會列印以下資訊.

Spring Cloud學習(2)-----Eureka引言一.環境搭建二.功能體系

至此,一個簡單的微服務治理體系搭建完成.

二.功能體系

    第一小節完成的一個簡單的微服務治理體系.接下來,我們看看以上三者的功能和作用分别是什麼.

    在實際使用中一個微服務執行個體一般即是消費者也是提供者.

2.1 服務注冊中心

  • 服務維護:儲存服務提供者注冊的服務中繼資料
  • 失效剔除:當服務提供者失效時,剔除出可用服務清單
  • 服務同步:接收到一個服務提供者注冊的服務時,會将此資訊同步給其他注冊中心
  • 自我保護:服務提供者在服務注冊中心注冊成功後,會維護一個心跳連接配接,告訴注冊中心自己還活着.如果統計的心跳連接配接失敗的比例在15分鐘内地域85%,注冊中心将會把目前執行個體的資訊保護起來,讓其不會過期.我們将三個服務提供者關掉一個,等一會就會出現如下提示,就表示已有服務進入自我保護機制了.
Spring Cloud學習(2)-----Eureka引言一.環境搭建二.功能體系

2.2 服務提供者

  • 服務注冊:服務在啟動時向注冊中心注冊服務
  • 服務續約:注冊成功後維護一個心跳連接配接告訴注冊中心服務正常,防止被剔除.

2.3 服務消費者

  • 服務擷取:服務啟動時,向注冊中心擷取服務清單,此清單沒30秒更新一次.
  • 服務調用:服務消費者可以根據服務資訊自己選擇合适的服務調用方式.注冊中心有Region和Zone的概念.一個Region可能具有多個Zone,一個Zone下可能具有多個服務執行個體.進行服務調用時,會優先通路同一個Zone下的服務執行個體
  • 服務下線:當服務面臨重新開機或關閉的情況下,告訴注冊中心我要下下線了.

繼續閱讀