Spring Cloud Zookeeperの紹介

Spring Cloud Zookeeperの紹介

1. 前書き

この記事では、Zookeeperと、クラウド内のサービスに関する一元化された知識として使用されるService DiscoveryでのZookeeperの使用方法について説明します。

Spring Cloud Zookeeperは、自動構成とSpring環境へのバインドを通じて、Spring BootアプリにApache Zookeeper統合を提供します。

2. サービスディスカバリのセットアップ

2つのアプリを作成します。

  • サービスを提供するアプリ(この記事ではService Provider)と呼びます

  • このサービスを利用するアプリ(Service Consumerと呼ばれます)

Apache Zookeeperは、サービス検出セットアップのコーディネーターとして機能します。 Apache Zookeeperのインストール手順は、次のlinkで入手できます。

3. サービスプロバイダー登録

spring-cloud-starter-zookeeper-discovery依存関係を追加し、メインアプリケーションでアノテーション@EnableDiscoveryClientを使用することにより、サービス登録を有効にします。

以下に、GET要求への応答で「Hello World!」を返すサービスのこのプロセスをステップごとに示します。

3.1. Mavenの依存関係

まず、必要なspring-cloud-starter-zookeeper-discoveryspring-webspring-cloud-dependencies、およびspring-boot-starterの依存関係をpom.xmlファイルに追加しましょう。


    
        org.springframework.boot
    spring-boot-starter
    1.5.2.RELEASE
    
    
        org.springframework
    spring-web
        4.3.7.RELEASE
    
    
        org.springframework.cloud
        spring-cloud-starter-zookeeper-discovery
        1.0.3.RELEASE
     


    
        
            org.springframework.cloud
            spring-cloud-dependencies
            Brixton.SR7
            pom
            import
        
    

3.2. サービスプロバイダーの注釈

次に、メインクラスに@EnableDiscoveryClientの注釈を付けます。 これにより、HelloWorldアプリケーションが検出可能になります。

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

そしてシンプルなコントローラー:

@GetMapping("/helloworld")
public String helloWorld() {
    return "Hello World!";
}

3.3. YAML構成

次に、アプリケーションのログレベルを構成し、アプリケーションが検出可能であることをZookeeperに通知するために使用されるYAMLApplication.ymlファイルを作成しましょう。

Zookeeperに登録されるアプリケーションの名前が最も重要です。 後のサービスコンシューマーで、feignクライアントはサービス検出中にこの名前を使用します。

spring:
  application:
    name: HelloWorld
  cloud:
    zookeeper:
      discovery:
        enabled: true
logging:
  level:
    org.apache.zookeeper.ClientCnxn: WARN

スプリングブートアプリケーションは、デフォルトポート2181でzookeeperを探します。 zookeeperが別の場所にある場合、構成を追加する必要があります。

spring:
  cloud:
    zookeeper:
      connect-string: localhost:2181

4. サービス消費者

次に、RESTサービスコンシューマーを作成し、spring Netflix Feign Clientを使用して登録します。

4.1. メーベン依存

まず、必要なspring-cloud-starter-zookeeper-discoveryspring-webspring-cloud-dependenciesspring-boot-starter-actuator、およびspring-cloud-starter-feignの依存関係をpom.xmlファイルに追加しましょう。


    
        org.springframework.cloud
        spring-cloud-starter-zookeeper-discovery
        1.0.3.RELEASE
    
    
        org.springframework.boot
        spring-boot-starter-actuator
        1.5.2.RELEASE
    
    
        org.springframework.cloud
        spring-cloud-starter-feign
        1.2.5.RELEASE
    


    
        
            org.springframework.cloud
            spring-cloud-dependencies
            Brixton.SR7
            pom
            import
        
    

4.2. サービス消費者アノテーション

サービスプロバイダーと同様に、メインクラスに@EnableDiscoveryClientアノテーションを付けて、検出に対応できるようにします。

@SpringBootApplication
@EnableDiscoveryClient
public class GreetingApplication {

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

4.3. Feignクライアントでサービスを発見

宣言型RESTクライアントを定義できるNetflixのプロジェクトSpring Cloud Feign Integration,を使用します。 URLがどのように見えるかを宣言し、偽装がRESTサービスへの接続を処理します。

Feign Clientは、spring-cloud-starter-feignパッケージを介してインポートされます。 @Configuration@EnableFeignClientsの注釈を付けて、アプリケーション内で使用できるようにします。

最後に、インターフェイスに@FeignClient(“service-name”)の注釈を付け、プログラムでこのサービスにアクセスできるように、アプリケーションに自動配線します。

ここで、注釈@FeignClient(name = “HelloWorld”)では、以前に作成したサービスプロデューサーのservice-nameを参照しています。

@Configuration
@EnableFeignClients
@EnableDiscoveryClient
public class HelloWorldClient {

    @Autowired
    private TheClient theClient;

    @FeignClient(name = "HelloWorld")
    interface TheClient {

        @RequestMapping(path = "/helloworld", method = RequestMethod.GET)
        @ResponseBody
    String helloWorld();
    }
    public String HelloWorld() {
        return theClient.HelloWorld();
    }
}

4.4. コントローラークラス

以下は、偽のクライアントクラスのサービスプロバイダー関数を呼び出して、挿入されたインターフェイスhelloWorldClientオブジェクトを介してサービス(詳細はサービス検出によって抽象化されます)を消費し、応答として表示する単純なサービスコントローラークラスです。

@RestController
public class GreetingController {

    @Autowired
    private HelloWorldClient helloWorldClient;

    @GetMapping("/get-greeting")
    public String greeting() {
        return helloWorldClient.helloWorld();
    }
}

4.5. YAML構成

次に、前に使用したものと非常によく似たYAMLファイルApplication.ymlを作成します。 これにより、アプリケーションのログレベルが構成されます。

logging:
  level:
    org.apache.zookeeper.ClientCnxn: WARN

アプリケーションは、デフォルトポート2181でZookeeperを探します。 Zookeeperが別の場所にある場合、構成を追加する必要があります。

spring:
  cloud:
    zookeeper:
      connect-string: localhost:2181

5. セットアップのテスト

HelloWorld RESTサービスは、デプロイ時にZookeeperに登録されます。 次に、サービスコンシューマとして機能するGreetingサービスが、Feignクライアントを使用してHelloWorldサービスを呼び出します。

これで、これら2つのサービスを構築して実行できます。

最後に、ブラウザでhttp://localhost:8083/get-greetingを指定すると、次のように表示されます。

Hello World!

6. 結論

この記事では、Spring Cloud Zookeeperを使用してサービス検出を実装する方法を確認し、Feign Clientを使用してGreetingサービスによって検出および消費されるように、Zookeeperサーバー内にHelloWorldというサービスを登録しました。 )その場所の詳細を知らなくても。

いつものように、この記事のコードはGitHubで入手できます。