Spring Cloud 學習筆記-構建 Eureka 應用

Eureka 介紹

Eureka提供基於REST的服務,在集羣中主要用於服務管理。Eureka提供了基於Java語言的客戶端組件,客戶端組件實現了負載均衡的功能,爲業務組件的集羣部署創造了條件。使用該框架,能夠將業務組件註冊到Eureka容器中,這些業務組件可進行集羣部署,Eureka主要維護這些服務的列表並自動檢查它們的狀態。git

程序結構

輸入圖片說明

建立Eureka Server

maven依賴github

<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-eureka-server</artifactId>
        </dependency>
    </dependencies>

更改spring boot 啓動端口 在application.ymlspring

server:
  port: 8761

開啓Eureka服務註解 @EnableEurekaServer瀏覽器

@EnableEurekaServer
@SpringBootApplication
public class EKServerApplication {

    public static void main(String[] args) {
        new SpringApplicationBuilder(EKServerApplication.class).run(args);
    }
}

啓動springbootspringboot

[Thread-11] o.s.c.n.e.server.EurekaServerBootstrap: Initialized server context
[main] s.b.c.e.t.TomcatEmbeddedServletContainer: Tomcat started on port(s): 8761 (http)
[main] .s.c.n.e.s.EurekaAutoServiceRegistration: Updating port to 8761
[main] c.b.firstEkServer.EKServerApplication: Started EKServerApplication in 8.594 seconds (JVM running for 9.523)

啓動期間會出現一個沒法鏈接到服務器的異常 這個是因爲Eureka在啓動的時候會把本身看成一個客戶端去服務器抓取註冊信息服務器

com.netflix.discovery.shared.transport.TransportException: Cannot execute request on any known server

增長以下配置啓動時便不會再出現該異常app

eureka:
  client:
    registerWithEureka: false
    fetchRegistry: false
  • registerWithEureka 聲明是否將本身的信息註冊到Eureka服務器,默認值爲true。
  • fetchRegistry 聲明是否到Eureka服務器中抓取註冊信息,默認值爲true。

在瀏覽器中訪問 http://localhost:8761 查看Eureka控制檯 輸入圖片說明負載均衡

建立服務提供者

依賴框架

<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-ribbon</artifactId>
        </dependency>

在 application.yml 中配置端口、Eureka實例名稱和Eureka服務地址maven

server:
  port: 8080
spring:
  application:
    name: ek-provider
eureka:
  instance:
    hostname: localhost
  client:
      serviceUrl:
        defaultZone: http://localhost:8761/eureka/

建立一個 REST 服務

@RestController
public class HelloController {

    @RequestMapping("/hello")
    public String hello(HttpServletRequest request) {
        return "hello:" + request.getRequestURL();
    }
}

開啓Eureka客戶端註解 @EnableEurekaServer

@EnableEurekaClient
@SpringBootApplication
public class EkProviderApplication {

    public static void main(String[] args) {
        new SpringApplicationBuilder(EkProviderApplication.class).run(args);

    }
}

啓動以後在 Eureka 控制檯能夠看到服務提供者已經在 Eureka 中註冊

輸入圖片說明

建立服務調用者

依賴

<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-ribbon</artifactId>
        </dependency>

在 application.yml 中配置端口、Eureka實例名稱和Eureka服務地址

server:
  port: 9000
spring:
  application:
    name: ek-invoke
eureka:
  instance:
    hostname: localhost
  client:
      serviceUrl:
        defaultZone: http://localhost:8761/eureka/

編寫一個 REST 服務 調用服務提供者的 「/hello」

@RestController
@Configuration
public class InvokeController {

    @Bean
    @LoadBalanced
    public RestTemplate getRestTemplate() {
        return new RestTemplate();
    }

    @RequestMapping("/invoke")
    public String invoke() {
        RestTemplate restTemplate = getRestTemplate();
        return restTemplate.getForObject("http://ek-provider/hello", String.class);
    }
}

在傳統模式中,咱們一般會用Apache中的Httpclient來調用 REST 服務,在這裏咱們使用 Spring 提供調用 REST 服務的組件 RestTemplate。 RestTemplate 自己並不具有調用分佈式服務的能力,可是RestTemplate的bean被@LoadBalanced註解修飾後,這個RestTemplate實例就具備訪問分佈式服務的能力,這得益於 Spring 爲其提供的各類攔截器 詳情

開啓Eureka客戶端註解 @EnableEurekaServer

@EnableEurekaClient
@SpringBootApplication
public class EkInvokeApplication {

    public static void main(String[] args) {
        new SpringApplicationBuilder(EkInvokeApplication.class).run(args);
    }
}

啓動以後在 Eureka 控制檯能夠看到服務調用者已經在 Eureka 中註冊

以後在瀏覽器訪問服務調用者的 「invoke」 接口 返回以下

輸入圖片說明

總結

Eureka 服務器 經過心跳連接來維護最新的註冊信息,這些註冊信息都保存在內存中。

Eureka 服務提供者 主要進行:

  1. 向 Eureka 服務器註冊服務
  2. 發送心跳到 Eureka服務器,使 Eureka 服務器註冊信息保持最新
  3. 向服務器獲取最新的註冊列表,一般 Eureka 客戶端既是服務提供者也是服務調用者,但其主要職責爲服務提供。

Eureka 服務調用者 主要進行:

  1. 向 Eureka 服務器註冊服務
  2. 發送心跳到 Eureka服務器,使 Eureka 服務器註冊信息保持最新
  3. 向服務器獲取最新的註冊列表,一般 Eureka 客戶端既是服務提供者也是服務調用者,但其主要職責爲發現與調用。

源碼

相關文章
相關標籤/搜索