LoginSignup
9
19

More than 3 years have passed since last update.

Spring Cloudを使ってみる。

Last updated at Posted at 2018-02-02

そもそもSpring Cloudは、「クラウドネイティブなアプリをつくるための便利なツール群」
じゃあ「クラウドネイティブ」って何?と考えると、クラウド上での利用を前提として設計されたシステムやサービスのこととのこと。
結局、いろんな小さなサービスを組み合わせて大きなシステムを構築するために必要な便利なツール群と理解しました。

Springが軽量コンテナといわれていた時代が懐かしい
ということでやってみたいと思います。

全体構成

SpringCloudの機能の一部ですが、ServiceDeliveryを実現する部分をやってみたいと思います。ソースはこちら

構成図は、あとで書く。

discovery-serviceの実装

@EnableEurekaServerアノテーションを付けたSpring-bootアプリケーションを作成するだけで、サービスレジストリとしてEurekaサーバをつくることが出来ます。
起動すると、
http://localhost:8761/
でこんな画面にアクセスできます。

image

準備

http://start.spring.io/
でひな形のアプリケーションを作ります。
image
Dependenciesに「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;

@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {

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

ひな形で作成されたクラスに@EnableEurekaServerをつけるだけ

もう一つ。設定情報を記載するapplication.ymlを作成
ポートと、自分自身をレジストリに登録しないための設定を記述しています。

server:
  port: 8761
eureka:
  client:
    registerWithEureka: false
    fetchRegistry: false

WebServiceを作る

今度はWebServiceレイヤを作ります。
ここの役割は、クライアントとの会話、BackendServiceに対するリバースプロキシです。

準備

先ほどと同様に、
http://start.spring.io/
からひな形を作ります。今度は、Dependenciesに「Eureka Discovery」を選択してください。

また、後続で、リバースプロキシを使っていて、「Zuul」もあわせて選択してください。

実装

ここでは、二つのアノテーションを使います。

@EnableEurekaClient をつけることで、EurekaServerにActivateされます。
@EnableZuulProxy をつけることで、Zuulを使用してリバースプロキシとして動作させることができます

package com.example.webservice;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;

@EnableEurekaClient
@EnableZuulProxy
@SpringBootApplication
public class WebServiceApplication {

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

設定情報はapplication.ymlに記述

spring:
  application:
    name: web-service
server:
  port: 8001
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka
  instance:
    preferIpAddress: true
zuul:
  ignored-services: '*'
  routes:
    backend-service: 'api/**'

spring.application.name 設定した名前でEurekaServerにサービスを登録
eureka.client.service.url.defaultZone 登録左記のEurekaServerを指定
zuulのapi/ へのリクエストをBackendServiceに転送するようにしています

BackendServiceを作る

準備

これまで同様、start.spring.ioからひな形を作ります。
WebService同様、「Eureka Discovery」を選択しておいてください。

実装

ソース2本に、設定ファイル1本作ります。

もはや、お作法。

@EnableEurekaClient
@SpringBootApplication
public class WebServiceApplication {

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

}

RestAPIを実装

package com.example.backendservice.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class BackendController {

    @RequestMapping("/hello")
    public String hello() {
        return "Hello from EurekaClient!";
    }
}

これまで説明したこと以外含まれないので割愛

spring:
  application:
    name: backend-service
server:
  port: 8002
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka
  instance:
    preferIpAddress: true

起動

さて、ここまで実装が終われば、あとは起動して接続確認

image

おー、無事、接続されました。

まとめ

Graldeの設定なども、start.spring.ioからプロジェクトを作成することができて、ほとんど自動でできてしまいました。
また、Spring Cloudで出来ることの一部しかまだやれていないので、
どんどんいろんなことをやってみたいと思います。

9
19
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
19