二、SpringCloud 之 Eureka 框架入門

Eureka 提供了基于 REST 的服務(wù),在集群中主要用于服務(wù)管理。

  • Eureka 服務(wù)器端對(duì)于注冊(cè)到服務(wù)端的服務(wù)組件,不提供后臺(tái)儲(chǔ)存,通過心跳保存最新狀態(tài),整個(gè)過程在內(nèi)存中執(zhí)行,并保存在內(nèi)存中的注冊(cè)中心。Eureka 客戶端(服務(wù)調(diào)用者和服務(wù)提供者) 也提供相同的 內(nèi)存注冊(cè)中心機(jī)制,這種機(jī)制提升了 Eureka 組件的性能,每次服務(wù)請(qǐng)求不必經(jīng)過服務(wù)端注冊(cè)中心進(jìn)行訪問。

  • Eureka 服務(wù)提供者

向服務(wù)器注冊(cè)服務(wù);發(fā)送心跳給服務(wù)器;向服務(wù)器獲取注冊(cè)列表;當(dāng)注冊(cè)到服務(wù)器,它會(huì)把自己相關(guān)的信息發(fā)送給服務(wù)端,諸如主機(jī)、端口、健康檢查連接等。

  • Eureka 服務(wù)調(diào)用者

服務(wù)器發(fā)布服務(wù),服務(wù)調(diào)用者對(duì)其進(jìn)行服務(wù)查找和調(diào)用。

  • 構(gòu)建 Eureka 應(yīng)用

1. 構(gòu)建服務(wù)器

1.1 配置 POM

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

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <spring-cloud.version>Edgware.SR1</spring-cloud.version>
    </properties>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </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>

1.2 Eureka 服務(wù)器啟動(dòng)類編寫

@SpringBootApplication
@EnableEurekaServer
public class DemoEkServerApplication {

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

1.3 properties 文件 服務(wù)器端口配置,默認(rèn)為 8080

server.port=8761
eureka.client.registerWithEureka=false
eureka.client.fetchRegistry=false

1.4 啟動(dòng) 通過 URL :http://127.0.0.1:8761/

圖片.png

2. 構(gòu)建服務(wù)提供者

2.1 配置 POM 文件

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

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <spring-cloud.version>Edgware.SR1</spring-cloud.version>
    </properties>

    <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.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </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>

2.2 properties 配置文件

spring.application.name=demo-ek-server-provider
eureka.instance.hostname=localhost
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/

2.3 配置 服務(wù)提供者啟動(dòng)類

@SpringBootApplication
@EnableEurekaClient
public class DemoEkServerProviderApplication {

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

2.4 編寫一個(gè) REST 服務(wù)

@RestController
public class ApiDemoController {

    @RequestMapping(value = "/person/{pid}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public Person findPerson(@PathVariable("pid") Long id) {

        Person person = new Person();
        person.setId(id);
        person.setLogname("張劍");
        person.setPassword("6000");

        return person;
    }

}

2.5 測(cè)試,訪問 eureka 服務(wù)地址:

圖片.png

服務(wù)提供者成功注冊(cè)到了 eureka 服務(wù)器中

3.構(gòu)建服務(wù)調(diào)用者

3.1 配置 POM 文件

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

        <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>

3.2 properties 配置文件

server.port=8888
spring.application.name=demo-ek-server-invoker
eureka.instance.hostname=localhost
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/

3.3 配置 服務(wù)調(diào)用者啟動(dòng)類

@SpringBootApplication
@EnableDiscoveryClient
public class DemoEkServerInvokerApplication {

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

3.4 配置 服務(wù)調(diào)用者對(duì)外暴露接口

@Configuration
@RestController
public class InvokerController {

    @Bean
    @LoadBalanced
    public RestTemplate getRestTemplate() {

        return new RestTemplate();
    }

    @RequestMapping(value = "/pubServer", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public String pubServer() {

        RestTemplate resTemple = getRestTemplate();
        String json = resTemple.getForObject("http://demo-ek-server-provider/person/1", String.class);
        return json;
    }

}

3.5測(cè)試

圖片.png

4. Eureka 集群搭建

由于之前工作忙加上身體不舒服,沒有進(jìn)行更新,現(xiàn)在SpringCloud 版本最新已經(jīng)是2.x 版本了,搭建Eureka 服務(wù)發(fā)現(xiàn)服務(wù)器采用新版本來(lái)搭建集群

4.1 項(xiàng)目整體結(jié)構(gòu)(平行)

image.png

4.2 服務(wù)端程序 配置文件修改為

server:
  port: 9999
spring:
  profiles: slave1
  application:
    name: cluster
eureka:
  instance:
    hostname: slave1
  client:
    registerWithEureka: false
    fetchRegistry: false
    service-url:
      defaultZone: http://slave2:9999/eureka/
---
      
server:
  port: 9998
spring:
  profiles: slave2
  application:
    name: cluster
eureka:
  instance:
    hostname: slave2
  client:
    registerWithEureka: false
    fetchRegistry: false
    service-url:
      defaultZone: http://slave1:9998/eureka/

注意:通過profiles 方式 在本地一臺(tái)主機(jī)上模擬兩臺(tái)主機(jī),通過修改 hosts 來(lái)實(shí)現(xiàn),hostname(slave1/slave2) 都綁定在 127.0.0.1 ip 上。這樣 兩個(gè)服務(wù)端就可以相互注冊(cè)

image.png
image.png

4.3 服務(wù)提供者模擬兩臺(tái) 配置文件修改為

spring:
  application:
    name: sc1
eureka:
  instance:
    hostname: localhost
  client:
    service-url:
      defaultZone: http://slave1:9999/eureka/,http://slave2:9998/eureka/

注意: 模擬兩臺(tái) 服務(wù)提供者采用 端口號(hào)來(lái)區(qū)別,在啟動(dòng)服務(wù)提供者定義 server.port 端口號(hào)


image.png

4.4 服務(wù)調(diào)用者 配置文件改造

spring:
  application:
    name: sc2
eureka:
  instance:
    hostname: localhost
  client:
    service-url:
      defaultZone: http://slave1:9999/eureka/,http://slave2:9998/eureka/
server:
  port: 7999

注意 服務(wù)調(diào)用者使用了 負(fù)載均衡 調(diào)用集群中的 服務(wù)提供者提供的服務(wù)。

image.png

4.5 使用 Postman 測(cè)試 服務(wù)調(diào)用者提供的接口

image.png
image.png

通過上面測(cè)試來(lái)看,調(diào)用接口相同,但是提供的服務(wù)的程序不同。

  • Erueka 學(xué)習(xí)案例源代碼下載 github 地址:

https://github.com/cqzhangjian/eureka.git

5. 服務(wù)實(shí)例健康自檢

Eureka 的客戶端會(huì)默認(rèn)每隔大約 30 秒會(huì)發(fā)送一次心跳給服務(wù)器端。這樣來(lái)告訴我們的服務(wù)器端,自己還活著。但是在某些情況下客戶端是活著的,但是卻因?yàn)槟承┰虿荒芴峁┓?wù),這種情況,客戶端也能告訴服務(wù)器端當(dāng)前的狀態(tài),就可以使用 Eureka 的健康自檢功能

開發(fā)步驟:

  • 在服務(wù)提供客戶端添加依賴:
    <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
  • 實(shí)現(xiàn)健康指示器接口:HealthIndicator 接口
  • 實(shí)現(xiàn)健康檢查處理器:HealthCheckHandler 接口
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容