SpringCloudで遊ぶ 3日目

SpringBootAdminを入れる

SpringBootAdminを入れてみる。Eureka経由でサービスを見つけてくれるようになる。

project("admin") {
    dependencies {
        implementation 'de.codecentric:spring-boot-admin-starter-server'
        implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client'
    }
}
@Configuration
@EnableAutoConfiguration
@EnableAdminServer
public class SpringBootAdminApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpringBootAdminApplication.class, args);
    }
}
eureka:
  instance:
    leaseRenewalIntervalInSeconds: 10
    health-check-url-path: /actuator/health
    metadata-map:
      startup: ${random.int}    #needed to trigger info and endpoint update after restart
    prefer-ip-address: true
  client:
    registryFetchIntervalSeconds: 5
    serviceUrl:
      defaultZone: ${EUREKA_URI:http://localhost:8761/eureka}

management:
  endpoints:
    web:
      exposure:
        include: "*"
  endpoint:
    health:
      show-details: ALWAYS
spring:
  application:
    name: admin
version: '3.7'

services:
  hello-app:
    image: net.orekyuu/hello:0.0.5
    ports:
      - 8080:8080
    deploy:
      replicas: 2
    environment:
      EUREKA_URI: http://eureka:8761/eureka
  eureka:
    image: net.orekyuu/eureka:0.0.5
    ports:
      - 8761:8761
    deploy:
      replicas: 1
  admin:
    image: net.orekyuu/admin:0.0.5
    ports:
      - 18080:8080
    environment:
      EUREKA_URI: http://eureka:8761/eureka

デプロイ…! f:id:orekyuu:20190912203857p:plain よし!動いてる!

API Gatewayを作ってみる

API Gatewayでhello-appを叩けるようにしてみる

project(':api-gateway') {
    dependencies {
        implementation 'org.springframework.cloud:spring-cloud-starter-gateway'
        implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client'
       // actuatorはsubprojectsのdependenciesに移動した。どうせ全部のプロジェクトで必要だし
    }
}
@SpringBootApplication
public class GatewayApplication {
    public static void main(String[] args) {
        SpringApplication.run(GatewayApplication.class, args);
    }

    @Bean
    public RouteLocator routeLocator(RouteLocatorBuilder builder) {
        return builder.routes()
                .route("hello_app_route", r -> r
                        .path("/hello")
                        .uri("http://hello-app:8080/hello"))
                .build();
    }
}

ガッっとアプリケーションコードを書く。
RouteLocatorBuilderでメソッドチェーンしながら組み立てていくっぽい。/helloにアクセスが来たらkubernetes内のhello-appiの/helloにプロキシするようにした。
hello-appの名前解決はeurekaでやってくれる。便利~!

spring:
  application:
    name: api-gateway
eureka:
  instance:
    leaseRenewalIntervalInSeconds: 10
    health-check-url-path: /actuator/health
    metadata-map:
      startup: ${random.int}    #needed to trigger info and endpoint update after restart
    prefer-ip-address: true
  client:
    registryFetchIntervalSeconds: 5
    serviceUrl:
      defaultZone: ${EUREKA_URI:http://localhost:8761/eureka}
management:
  endpoints:
    web:
      exposure:
        include: "*"
  endpoint:
    health:
      show-details: ALWAYS

application.ymlはコピペ。spring.application.nameだけ変えてる。似たような設定が散らばるのやだなー。
一応ルーティングの設定はapplication.ymlでも書けるらしいけど、個人的にはJavaConfigのほうがコンパイルできるし補完も効くのですき。

version: '3.7'

services:
  hello-app:
    image: net.orekyuu/hello:0.0.5
    deploy:
      replicas: 2
    environment:
      EUREKA_URI: http://eureka:8761/eureka
  eureka:
    image: net.orekyuu/eureka:0.0.5
    ports:
      - 8761:8761
    deploy:
      replicas: 1
  admin:
    image: net.orekyuu/admin:0.0.5
    ports:
      - 18080:8080
    environment:
      EUREKA_URI: http://eureka:8761/eureka
  api-gateway:
    image: net.orekyuu/api-gateway:0.0.5
    environment:
      EUREKA_URI: http://eureka:8761/eureka
    ports:
      - 8080:8080

最後にapi-gatewayの設定を追加して8080で受けるようにする。もともとhello-appが使っていたけど、直接叩くことはなくなったのでportsは削除。
全部api-gatewayを通してリクエス

デプロイして8080を確認するとhello-appのときと変わらない画面が見える。
結局API Gatewayが使われているかわからん!
Adminを見てみる。

f:id:orekyuu:20190912210949p:plain ちゃんとリクエストきてる!やったー!