2、Spring Cloud之註冊中心 Eureka

前言

算是正式開始學習 spring cloud 的項目知識了,大概的知道Springcloud 是由衆多的微服務組成的,因此咱們如今一個一個的來學習吧。java

註冊中心,在微服務中算是核心了。全部的服務都會註冊到註冊中心,請求服務的時候,並不會直接去請求服務地址,而是先經過註冊中心再轉到目的地址。雖然Eureka 已經中止維護了,可是咱們暫時使用起來仍是沒有問題的。linux

Eureka 主要有服務註冊中心、服務提供者和服務消費。不少時候服務消費者也是服務提供者。因此就 Eureka 而言,分爲 Eureka 服務端和Eureka 客戶端,服務端就是註冊中心,客戶端就是服務提供者和消費者。git

單機模式

好了,咱們動手搭建一個Eureka 的服務端吧先,服務端有單機模式和集羣模式,咱們先來單機模式。程序員

更具上篇文章講的,咱們使用maven 模塊化開發,咱們建立一個父級maven項目,pom.xml 文件內容以下:github

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <groupId>cn.quellanan</groupId>
    <artifactId>SpringCloud</artifactId>
    <version>1.0.0</version>
    <packaging>pom</packaging>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Hoxton.SR1</spring-cloud.version>
    </properties>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</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>
    
    <modules>
        <module>eureka-server-8000</module>
        <module>eureka-server-8001</module>
        <module>eureka-server-8002</module>
        <module>zlflovemm</module>
    </modules>

</project>

能夠看到文件中指定了spring boot 和Spring cloud 等基礎依賴的版本,這樣保證各個模塊版本的一致性。web

子模塊

接下來咱們建立一個eureka-server-8000 的子模塊。spring

pom.xml

pom.xml的內容以下:apache

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>cn.quellanan</groupId>
        <artifactId>SpringCloud</artifactId>
        <version>1.0.0</version>
    </parent>
    <groupId>com.quellanan.springcloud</groupId>
    <artifactId>eureka-server-8000</artifactId>
    <version>1.0.0</version>
    <name>eureka-server-8000</name>
    <description>eureka project for Spring Boot</description>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
    </dependencies>

</project>

能夠看到繼承了父級pom,額外的增長了 spring-cloud-starter-netflix-eureka-server 的依賴。segmentfault

@EnableEurekaServer

在啓動類中增長@EnableEurekaServer 註解,表示啓用Eureka 服務端。服務器

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

application.properties

配置文件中增長以下配置

spring.application.name=spring-cloud-eureka-server-8000
server.port=8000

#表示是否將本身註冊到Eureka Server,默認爲true。
eureka.client.register-with-eureka=true

# 表示是否從Eureka Server獲取註冊信息,默認爲true。
eureka.client.fetch-registry=true

#設置與Eureka Server交互的地址,查詢服務和註冊服務都須要依賴這個地址。多個地址可以使用 , 分隔。
eureka.client.serviceUrl.defaultZone=http://localhost:${server.port}/eureka/

如今咱們就能夠啓動項目看看
在這裏插入圖片描述
能夠看到咱們將自身註冊到了服務中。

Eureka 客戶端

前面說了,服務提供者和服務消費者都是客戶端,其實就是咱們具體的某一業務的項目。因此咱們再建立一個子模塊。我這裏分開吧,咱們分別建立服務提供者和服務消費者。

服務提供者

咱們建立一個eureka-client-provider的子模塊,pom 文件中引入spring-cloud-starter-netflix-eureka-client。

<parent>
        <groupId>cn.quellanan</groupId>
        <artifactId>SpringCloud</artifactId>
        <version>1.0.0</version>
    </parent>
    groupId>com.quellanan.springcloud</groupId>
    <artifactId>eureka-client-provider</artifactId>
    <version>1.0.0</version>
    <name>eureka-client-provider</name>
    <description>eureka-client-provider 服務提供者</description>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
    </dependencies>

啓動類中加入@EnableEurekaClient註解或者@EnableDiscoveryClient註解均可以。

@SpringBootApplication
@EnableDiscoveryClient
public class EurekaClientProviderApplication {

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

}

application.properties 中增長以下配置

server.port=9000
#服務名,在註冊時所用
spring.application.name=eureka-client-provider
eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/

這裏指定的eureka的服務中心的地址爲8000。如上配置就能夠將服務註冊到註冊中心啦。
咱們在寫一個測試接口。
建立一個IndexController 類,內容以下:

@RestController
public class HelloController {
    @RequestMapping("/hello")
    public String hello(){
        return "hello world ";
    }
}

服務消費者

咱們同樣的建立一個 eureka-client-consumer的模塊。pom文件以下:

<parent>
        <groupId>cn.quellanan</groupId>
        <artifactId>SpringCloud</artifactId>
        <version>1.0.0</version>
    </parent>

    <groupId>com.quellanan.springcloud</groupId>
    <artifactId>eureka-client-consumer</artifactId>
    <version>1.0.0</version>
    <name>eureka-client-consumer</name>
    <description>eureka-client-consumer 服務消費者</description>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
    </dependencies>

相對於服務提供者,咱們增長了Feign 依賴,主要用來發現服務和實現客戶端負載均衡,咱們這裏用來發現服務就能夠了。

在啓動類中@EnableDiscoveryClient 用來發現服務,並注入RestTemplate 的實例bean 用來對服務提供的接口進行調用。@LoadBalanced 是開啓客戶端負載均衡的,最開始我沒有加這個註解,可是發現不加的話,服務消費者就不能經過服務名來獲取可用的服務提供者的實例。因此這裏你們能夠試驗一下。

@EnableDiscoveryClient
@SpringBootApplication
public class EurekaClientConsumerApplication {
    @Bean
    @LoadBalanced
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }
    public static void main(String[] args) {
        SpringApplication.run(EurekaClientConsumerApplication.class, args);
    }
}

咱們接下寫一個接口,調用服務消費者,咱們建立一個IndexController,內容以下:

@RestController
public class IndexController {
    private static final String applicationName = "eureka-client-provider";
    @Autowired
    private RestTemplate restTemplate;
    @RequestMapping("/index")
    public String getHello(){
        String url = "http://"+ applicationName +"/hello";
        return  restTemplate.getForObject(url,String.class);
    }
}

這裏咱們能夠看到applicationName 就是服務提供者的服務名。實際中,一種類型的服務可能有好幾臺服務器,可能物理地址和ip不同,可是保證他們的服務名同樣就能夠了,這樣服務消費者就能夠經過服務名獲取可用的列表,再經過複雜均衡策略選擇其中一個實例訪問。

最後咱們在application中加上以下配置:

server.port=9001
#服務名,在註冊時所用
spring.application.name=eureka-client-consumer
eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/

測試

如今咱們啓動註冊中心和客戶端這兩個項目來看下。啓動後,咱們輸入

http://localhost:8000

在這裏插入圖片描述
能夠發現咱們的客戶端已經註冊到註冊中心啦。服務提供者和服務消費者都已經註冊到了註冊中心啦。咱們再來調接口試試。咱們輸入以下:

http://localhost:9001/index

在這裏插入圖片描述
能夠看到其實獲取了9000服務提供者的接口。這樣就實現了服務的註冊和發現啦,並實現遠程調用。

集羣模式(高可用)

上面咱們咱們搭建的註冊中心只是單機模式,只有一個Eureka 服務端,單實際應用中註冊中心實際上是尤其重要的,因此就須要搭建集羣環境,其實Eureka 對集羣模式是自然的支持的,咱們搭建也很是簡單。
爲何這麼說呢,咱們前面能夠看到只要配置了eureka.client.serviceUrl.defaultZone 就就會被對應的註冊中線檢測到,因此咱們代碼徹底同樣,只須要將eureka.client.serviceUrl.defaultZone相互指引就能夠了,就就能夠簡單的搭建一個高可用的環境。
下面咱們來搭建一個,由於咱們就一臺服務器,因此就用不一樣的端口,其實代碼徹底同樣的,只是配置文件中配置不同,咱們分別把三個分配置文件貼出來。
8000端口的

spring.application.name=spring-cloud-eureka-server-8000
server.port=8000

#表示是否將本身註冊到Eureka Server,默認爲true。
eureka.client.register-with-eureka=true

# 表示是否從Eureka Server獲取註冊信息,默認爲true。
eureka.client.fetch-registry=true

#設置與Eureka Server交互的地址,查詢服務和註冊服務都須要依賴這個地址。多個地址可以使用 , 分隔。
eureka.client.serviceUrl.defaultZone=http://localhost:8001/eureka/,http://localhost:8002/eureka/

8001端口:

spring.application.name=spring-cloud-eureka-server-8001
server.port=8001
#表示是否將本身註冊到Eureka Server,默認爲true。
eureka.client.register-with-eureka=true
# 表示是否從Eureka Server獲取註冊信息,默認爲true。
eureka.client.fetch-registry=true
#設置與Eureka Server交互的地址,查詢服務和註冊服務都須要依賴這個地址。多個地址可以使用 , 分隔。
eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/,http://localhost:8002/eureka/

8002 端口

spring.application.name=spring-cloud-eureka-server-8002
server.port=8002
#表示是否將本身註冊到Eureka Server,默認爲true。
eureka.client.register-with-eureka=true
# 表示是否從Eureka Server獲取註冊信息,默認爲true。
eureka.client.fetch-registry=true
#設置與Eureka Server交互的地址,查詢服務和註冊服務都須要依賴這個地址。多個地址可以使用 , 分隔。
eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/,http://localhost:8001/eureka/

咱們如今分別啓動這個這三個配置文件,大家能夠用profile 來指向,我這爲了分明,直接建立了三個模塊。啓動後,咱們分別訪問

http://localhost:8000/
http://localhost:8001/
http://localhost:8002/

在這裏插入圖片描述
這裏能夠看到其實已經相互監控了。咱們瞭解一下這兩個配置參數。

#定義服務續約任務的調用時間間隔,默認30s
eureka.instance.lease-renewal-interval-in-seconds=30
#定義服務失效的時間默認90s
eureka.instance.lease-expiration-duration-in-seconds=90

咱們如今再將咱們的服務提供者和服務消費者註冊進來,可是這裏,須要修改的地方也是eureka.client.serviceUrl.defaultZone。將服務註冊到集羣中。

eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/,http://localhost:8001/eureka/,http://localhost:8002/eureka/

而後啓動項目可,能夠看到註冊到了註冊中心,而且能夠調用服務提供者提供的接口。
在這裏插入圖片描述

總結

最後畫了一張圖來講明整個註冊中心的架構圖。
在這裏插入圖片描述

能夠看到註冊服務端能夠是一個集羣。相互註冊監控。服務消費者和服務提供者都是服務客戶端,都會將服務註冊到服務中心,同時這些服務也均可以使是集羣或者分佈式的。服務提供者會從服務端獲取服務提供者可用的服務實例列表,經過負載均衡策略選擇其中某一實例進行調用。這個算是Eureka 的總結吧哈哈

番外

好啦,老是是寫完了,這篇文章真是是卡了我好幾天,有的地方寫的不是很好,歡迎你們指點。
代碼上傳到github:
https://github.com/QuellanAn/...

後續加油♡

歡迎你們關注我的公衆號 "程序員愛酸奶"

分享各類學習資料,包含java,linux,大數據等。資料包含視頻文檔以及源碼,同時分享本人及投遞的優質技術博文。

若是你們喜歡記得關注和分享喲❤

file

相關文章
相關標籤/搜索