溫馨提示×

溫馨提示×

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

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

SpringCloud?Eureka服務注冊中心應用入門實例分析

發布時間:2022-07-12 14:27:15 來源:億速云 閱讀:167 作者:iii 欄目:開發技術

SpringCloud Eureka服務注冊中心應用入門實例分析

引言

在微服務架構中,服務注冊與發現是一個至關重要的組件。Spring Cloud Eureka 是 Netflix 開源的一個服務注冊與發現組件,它能夠幫助我們在分布式系統中實現服務的自動注冊與發現。本文將詳細介紹如何使用 Spring Cloud Eureka 構建一個簡單的服務注冊中心,并通過實例分析其應用。

1. Spring Cloud Eureka 簡介

1.1 什么是 Eureka?

Eureka 是 Netflix 開源的一個基于 REST 的服務注冊與發現組件,主要用于 AWS 云中的服務定位。Spring Cloud 將其集成到 Spring 生態系統中,使得在 Spring Boot 應用中能夠輕松使用 Eureka 來實現服務注冊與發現。

1.2 Eureka 的核心概念

  • Eureka Server:服務注冊中心,負責管理所有注冊的服務實例。
  • Eureka Client:服務提供者和消費者,負責向 Eureka Server 注冊自己,并從 Eureka Server 獲取其他服務的實例信息。

2. 環境準備

在開始之前,確保你已經安裝了以下工具:

  • JDK 1.8 或更高版本
  • Maven 3.x 或更高版本
  • IntelliJ IDEA 或 Eclipse IDE

3. 創建 Eureka Server

3.1 創建 Spring Boot 項目

首先,我們需要創建一個 Spring Boot 項目作為 Eureka Server??梢允褂?Spring Initializr 快速生成項目。

  1. 打開 Spring Initializr。
  2. 選擇 Maven 項目,語言選擇 Java,Spring Boot 版本選擇 2.5.6(或更高版本)。
  3. 在依賴項中添加 Eureka Server。
  4. 點擊生成并下載項目。

3.2 配置 Eureka Server

下載并解壓項目后,使用 IDE 打開項目。在 src/main/resources/application.yml 文件中添加以下配置:

server:
  port: 8761

eureka:
  instance:
    hostname: localhost
  client:
    register-with-eureka: false
    fetch-registry: false
    service-url:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
  • server.port:指定 Eureka Server 的端口號,默認為 8761。
  • eureka.client.register-with-eureka:設置為 false,表示 Eureka Server 不需要向自己注冊。
  • eureka.client.fetch-registry:設置為 false,表示 Eureka Server 不需要從自己獲取注冊表。
  • eureka.client.service-url.defaultZone:指定 Eureka Server 的地址。

3.3 啟動 Eureka Server

src/main/java/com/example/eurekaserver/EurekaServerApplication.java 文件中,添加 @EnableEurekaServer 注解以啟用 Eureka Server:

package com.example.eurekaserver;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {

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

}

運行 EurekaServerApplication,Eureka Server 將會啟動,并監聽 8761 端口。打開瀏覽器,訪問 http://localhost:8761,你將看到 Eureka Server 的管理界面。

4. 創建 Eureka Client

4.1 創建 Spring Boot 項目

接下來,我們需要創建一個 Spring Boot 項目作為 Eureka Client。同樣使用 Spring Initializr 生成項目。

  1. 打開 Spring Initializr。
  2. 選擇 Maven 項目,語言選擇 Java,Spring Boot 版本選擇 2.5.6(或更高版本)。
  3. 在依賴項中添加 Eureka Discovery Client。
  4. 點擊生成并下載項目。

4.2 配置 Eureka Client

下載并解壓項目后,使用 IDE 打開項目。在 src/main/resources/application.yml 文件中添加以下配置:

server:
  port: 8081

spring:
  application:
    name: eureka-client

eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
  • server.port:指定 Eureka Client 的端口號,這里設置為 8081。
  • spring.application.name:指定服務的名稱,這里設置為 eureka-client。
  • eureka.client.service-url.defaultZone:指定 Eureka Server 的地址。

4.3 啟動 Eureka Client

src/main/java/com/example/eurekaclient/EurekaClientApplication.java 文件中,添加 @EnableDiscoveryClient 注解以啟用 Eureka Client:

package com.example.eurekaclient;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@SpringBootApplication
@EnableDiscoveryClient
public class EurekaClientApplication {

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

}

運行 EurekaClientApplication,Eureka Client 將會啟動,并向 Eureka Server 注冊自己。

4.4 驗證服務注冊

打開瀏覽器,訪問 http://localhost:8761,你將看到 eureka-client 服務已經成功注冊到 Eureka Server。

5. 創建服務消費者

5.1 創建 Spring Boot 項目

接下來,我們需要創建一個 Spring Boot 項目作為服務消費者。同樣使用 Spring Initializr 生成項目。

  1. 打開 Spring Initializr。
  2. 選擇 Maven 項目,語言選擇 Java,Spring Boot 版本選擇 2.5.6(或更高版本)。
  3. 在依賴項中添加 Eureka Discovery ClientSpring Web。
  4. 點擊生成并下載項目。

5.2 配置服務消費者

下載并解壓項目后,使用 IDE 打開項目。在 src/main/resources/application.yml 文件中添加以下配置:

server:
  port: 8082

spring:
  application:
    name: service-consumer

eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
  • server.port:指定服務消費者的端口號,這里設置為 8082。
  • spring.application.name:指定服務的名稱,這里設置為 service-consumer。
  • eureka.client.service-url.defaultZone:指定 Eureka Server 的地址。

5.3 創建 REST 控制器

src/main/java/com/example/serviceconsumer/ServiceConsumerApplication.java 文件中,添加 @EnableDiscoveryClient 注解以啟用 Eureka Client:

package com.example.serviceconsumer;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@SpringBootApplication
@EnableDiscoveryClient
public class ServiceConsumerApplication {

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

}

接下來,創建一個 REST 控制器來調用 eureka-client 服務。在 src/main/java/com/example/serviceconsumer/controller/ConsumerController.java 文件中添加以下代碼:

package com.example.serviceconsumer.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;

@RestController
public class ConsumerController {

    @Autowired
    private DiscoveryClient discoveryClient;

    @GetMapping("/call-client")
    public String callClient() {
        List<ServiceInstance> instances = discoveryClient.getInstances("eureka-client");
        if (instances.isEmpty()) {
            return "No instances available";
        }
        String uri = instances.get(0).getUri().toString();
        RestTemplate restTemplate = new RestTemplate();
        return restTemplate.getForObject(uri + "/hello", String.class);
    }
}

5.4 啟動服務消費者

運行 ServiceConsumerApplication,服務消費者將會啟動,并向 Eureka Server 注冊自己。

5.5 驗證服務調用

打開瀏覽器,訪問 http://localhost:8082/call-client,你將看到服務消費者成功調用了 eureka-client 服務并返回了結果。

6. 總結

通過本文的介紹,我們學習了如何使用 Spring Cloud Eureka 構建一個簡單的服務注冊中心,并通過實例分析了其應用。Eureka 作為服務注冊與發現的核心組件,在微服務架構中扮演著重要的角色。希望本文能夠幫助你快速入門 Spring Cloud Eureka,并在實際項目中應用它。

7. 參考資料


以上是關于 Spring Cloud Eureka 服務注冊中心應用入門的實例分析。通過本文的學習,你應該能夠掌握如何使用 Eureka 構建服務注冊中心,并在微服務架構中實現服務的自動注冊與發現。希望本文對你有所幫助!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女