Eurekaの導入と活用方法

基本概念

EurekaはSpring Cloudが提供するサービス登録と発見機能です。各サービスは登録センターに登録され、他のサービスがその存在を知ることができます。

Eurekaには以下の3つの役割があります:

  • 登録センター:サービスの状態を管理する中心サーバ
  • サービス提供者:Eurekaに登録されるマイクロサービス
  • サービス消費者:Eurekaを利用してサービスを検索するマイクロサービス

登録センターはサーバーとして独立して起動します。

サービス登録と発見の流れ

マイクロサービスアーキテクチャでは、単一アプリケーションを複数の独立したサービスに分割します。各サービスは異なるサーバーまたはポートで実行されます。しかし、ビジネス要求を処理するには複数のマイクロサービスを連携させる必要があります。

以下のような課題を解決します:

  • サービスがどのサーバー、どのポートで稼働しているかを自動的に検知
  • サーバー障害時の自動的なサービス移行
  • 大規模トラフィック時の自動的なサービススケールアウト

Eurekaは負荷分散機能も備えており、サービス名を指定するだけで適切なサービスインスタンスを選択します。

導入手順

1. 登録センターの作成

新しいSpring Bootプロジェクトを作成し、以下の依存関係を追加します。


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

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-parent</groupId>
            <version>2021.0.3</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

2. サーバー起動クラスの作成


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

3. 設定ファイルの設定


server:
  port: 8761 # 登録センターの起動ポート
eureka:
  client:
    registerWithEureka: false # 自分自身を登録しない
    fetchRegistry: false

プロジェクトを起動後、ブラウザで`http://localhost:8761`にアクセスするとEureka管理画面が表示されます。

4. サービスの登録

マイクロサービスを登録するには以下の依存関係を追加します。


<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</groupId>
    <version>3.3.1</version>
</dependency>

起動クラスに以下の注解を追加します。


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

設定ファイルに以下の内容を追加します。


spring:
  application:
    name: my-eureka-service
server:
  port: 0 # 随意ポートで起動
eureka:
  client:
    serviceUrl:
      defaultZone: ${EUREKA_URI:http://localhost:8761/eureka} # 登録先を指定
  instance:
    preferIpAddress: true
    appname: my-test-service

プロジェクトを起動すると"My Test Service"がEurekaに登録されます。

5. サービスの検索

サービスを検索するプロジェクトを作成します。

POMファイルに以下の依存関係を追加します。


<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</groupId>
        <version>3.3.1</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-openfeign</groupId>
    </dependency>
</dependencies>

設定ファイルに以下の内容を追加します。


spring:
  application:
    name: service-consumer
server:
  port: 0
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka
    register-with-eureka: false

起動クラスに以下の注解を追加します。


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

以下のようなFeignクライアントを作成します。


@FeignClient(name = "my-eureka-service")
public interface MyServiceClient {
    @GetMapping("/test/hello")
    String getHello();
}

以下のようなコントローラを作成します。


@RestController
@RequestMapping("/test")
public class TestController {
    @Autowired
    private MyServiceClient myServiceClient;

    @GetMapping("/hello")
    public String test() {
        return myServiceClient.getHello();
    }
}

ブラウザで`http://localhost:port/test/hello`にアクセスすると、サービス検索成功のメッセージが表示されます。

タグ: Spring Cloud Eureka マイクロサービス サービス登録 Feign

6月25日 22:26 投稿