Spring Cloud Netflix Eureka によるサービスディスカバリの実装

親プロジェクトの設定

まず、Mavenマルチモジュールプロジェクトの親POMを作成します。pom.xmlは以下のように構成します:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
         http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <packaging>pom</packaging>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.12.RELEASE</version>
        <relativePath/>
    </parent>

    <groupId>jp.techdev.microservices</groupId>
    <artifactId>eureka-parent-project</artifactId>
    <version>1.0.0</version>
    <name>eureka-parent</name>
    <description>Spring Cloud Netflix Eureka親プロジェクト</description>

    <properties>
        <java.version>11</java.version>
        <spring-cloud.version>Hoxton.SR12</spring-cloud.version>
    </properties>

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

依存関係の解決後、親プロジェクトのsrcディレクトリは削除可能です。

単一Eurekaサーバーの構築

新規Mavenモジュールeureka-registry-primaryを作成し、以下の依存関係を追加します:

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

application.ymlにレジストリ設定を記述します:

server:
  port: 8761
spring:
  application:
    name: eureka-registry
eureka:
  instance:
    hostname: primary-registry
  client:
    register-with-eureka: false
    fetch-registry: false
    service-url:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
  server:
    enable-self-preservation: false

起動クラスを作成します:

package jp.techdev.eureka.server;

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

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

アプリケーション起動後、http://primary-registry:8761にアクセスするとEurekaダッシュボードが表示されます。

Eurekaクライアントの実装

モジュールeureka-service-sampleを作成し、以下の依存関係を追加します:

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

クライアント設定ファイル:

server:
  port: 8081
spring:
  application:
    name: sample-service
eureka:
  client:
    register-with-eureka: true
    fetch-registry: true
    service-url:
      defaultZone: http://primary-registry:8761/eureka/

起動クラス:

package jp.techdev.eureka.client;

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

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

Eurekaクラスターの構築

可用性向上のため、レジストリを複数台構成します。eureka-registry-primaryの設定を変更します:

server:
  port: 8761
spring:
  application:
    name: eureka-registry
eureka:
  instance:
    hostname: registry-primary
  client:
    service-url:
      defaultZone: http://registry-secondary:8762/eureka/
    fetch-registry: true
    register-with-eureka: true

2台目のモジュールeureka-registry-secondaryを作成します:

server:
  port: 8762
eureka:
  instance:
    hostname: registry-secondary
  client:
    register-with-eureka: true
    fetch-registry: true
    service-url:
      defaultZone: http://registry-primary:8761/eureka/
spring:
  application:
    name: eureka-registry

起動クラス:

package jp.techdev.eureka.server;

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

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

hostsファイルに以下を追加します:

127.0.0.1 registry-primary
127.0.0.1 registry-secondary

クライアントは両方のレジストリに登録するように設定します:

eureka:
  client:
    service-url:
      defaultZone: http://registry-primary:8761/eureka/,http://registry-secondary:8762/eureka/

認証付きEurekaサーバー

モジュールeureka-secure-registryを作成し、Spring Security依存関係を追加します:

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

認証設定を含む設定ファイル:

server:
  port: 8763
spring:
  application:
    name: eureka-secure-registry
  security:
    user:
      name: admin
      password: securepass
eureka:
  instance:
    hostname: secure-registry
  client:
    fetch-registry: false
    register-with-eureka: false

CSRF保護を無効化するセキュリティ設定:

package jp.techdev.eureka.security.config;

import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

@EnableWebSecurity
public class EurekaSecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().ignoringAntMatchers("/eureka/**");
        super.configure(http);
    }
}

起動クラス:

package jp.techdev.eureka.security;

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

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

認証付きレジストリへのクライアント登録

クライアントの設定ファイルに認証情報を含めます:

eureka:
  client:
    service-url:
      defaultZone: http://admin:securepass@secure-registry:8763/eureka/

主要な設定パラメータ

eureka:
  client:
    register-with-eureka: true
    fetch-registry: true
    service-url:
      defaultZone: http://localhost:8761/eureka/
    enabled: true
    registry-fetch-interval-seconds: 30
  instance:
    lease-renewal-interval-in-seconds: 30
    lease-expiration-duration-in-seconds: 90
    metadata-map:
      zone: tokyo
    hostname: localhost
    prefer-ip-address: false
  server:
    enable-self-preservation: false

タグ: Spring-Cloud netflix-eureka service-discovery Microservices spring-security

7月7日 00:24 投稿