溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Spring Cloud如何構(gòu)建Eureka應(yīng)用

發(fā)布時(shí)間:2021-08-05 15:28:48 來(lái)源:億速云 閱讀:142 作者:小新 欄目:編程語(yǔ)言

小編給大家分享一下Spring Cloud如何構(gòu)建Eureka應(yīng)用,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

Eureka 介紹

Eureka提供基于REST的服務(wù),在集群中主要用于服務(wù)管理。Eureka提供了基于Java語(yǔ)言的客戶端組件,客戶端組件實(shí)現(xiàn)了負(fù)載均衡的功能,為業(yè)務(wù)組件的集群部署創(chuàng)造了條件。使用該框架,可以將業(yè)務(wù)組件注冊(cè)到Eureka容器中,這些業(yè)務(wù)組件可進(jìn)行集群部署,Eureka主要維護(hù)這些服務(wù)的列表并自動(dòng)檢查它們的狀態(tài)。

程序結(jié)構(gòu)

Spring Cloud如何構(gòu)建Eureka應(yīng)用

創(chuàng)建Eureka Server

maven依賴

  <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 啟動(dòng)端口 在application.yml

server:
 port: 8761

開(kāi)啟Eureka服務(wù)注解 @EnableEurekaServer

@EnableEurekaServer
@SpringBootApplication
public class EKServerApplication {

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

啟動(dòng)springboot

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

啟動(dòng)期間會(huì)出現(xiàn)一個(gè)無(wú)法連接到服務(wù)器的異常 這個(gè)是由于Eureka在啟動(dòng)的時(shí)候會(huì)把自己當(dāng)作一個(gè)客戶端去服務(wù)器抓取注冊(cè)信息

復(fù)制代碼 代碼如下:


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

增加如下配置啟動(dòng)時(shí)便不會(huì)再出現(xiàn)該異常

eureka:
 client:
  registerWithEureka: false
  fetchRegistry: false

registerWithEureka 聲明是否將自己的信息注冊(cè)到Eureka服務(wù)器,默認(rèn)值為true。

fetchRegistry 聲明是否到Eureka服務(wù)器中抓取注冊(cè)信息,默認(rèn)值為true。

在瀏覽器中訪問(wèn) http://localhost:8761 查看Eureka控制臺(tái) 輸入圖片說(shuō)明

Spring Cloud如何構(gòu)建Eureka應(yīng)用

創(chuàng)建服務(wù)提供者

依賴

<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實(shí)例名稱和Eureka服務(wù)地址

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

創(chuàng)建一個(gè) REST 服務(wù)

@RestController
public class HelloController {

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

開(kāi)啟Eureka客戶端注解 @EnableEurekaServer

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

  }
}

啟動(dòng)之后在 Eureka 控制臺(tái)可以看到服務(wù)提供者已經(jīng)在 Eureka 中注冊(cè)

Spring Cloud如何構(gòu)建Eureka應(yīng)用

創(chuàng)建服務(wù)調(diào)用者

依賴

<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實(shí)例名稱和Eureka服務(wù)地址

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

編寫一個(gè) REST 服務(wù) 調(diào)用服務(wù)提供者的 “/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);
  }
}

在傳統(tǒng)模式中,我們通常會(huì)用Apache中的Httpclient來(lái)調(diào)用 REST 服務(wù),在這里我們使用 Spring 提供調(diào)用 REST 服務(wù)的組件 RestTemplate。 RestTemplate 本身并不具備調(diào)用分布式服務(wù)的能力,但是RestTemplate的bean被@LoadBalanced注解修飾后,這個(gè)RestTemplate實(shí)例就具有訪問(wèn)分布式服務(wù)的能力,這得益于 Spring 為其提供的各種攔截器

開(kāi)啟Eureka客戶端注解 @EnableEurekaServer

@EnableEurekaClient
@SpringBootApplication
public class EkInvokeApplication {

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

啟動(dòng)之后在 Eureka 控制臺(tái)可以看到服務(wù)調(diào)用者已經(jīng)在 Eureka 中注冊(cè)

之后在瀏覽器訪問(wèn)服務(wù)調(diào)用者的 “invoke” 接口 返回如下

Spring Cloud如何構(gòu)建Eureka應(yīng)用

以上是“Spring Cloud如何構(gòu)建Eureka應(yīng)用”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI