LoginSignup
0
0

More than 3 years have passed since last update.

Spring Cloud Gatewayを使ってルーティングの設定をしてみた

Last updated at Posted at 2019-12-21

概要

Spring Cloud Gateway を使った設定を行ったときのメモ

環境

  • SpringBoot:2.1.9.RELEASE

詳細

  • Spring Initializr から SpringBootのプロジェクトを作成
    • Dependencies に Gateway を設定してプロジェクトを作成する
  • 作成したプロジェクトの build.gradle は以下のような内容で生成されているはず
build.gradle
plugins {
    id 'org.springframework.boot' version '2.1.9.RELEASE'
    id 'io.spring.dependency-management' version '1.0.8.RELEASE'
    id 'java'
}

group = 'com.ap.itopics'
version = '0.0.1'
sourceCompatibility = '1.8'

repositories {
    mavenCentral()
}

ext {
    set('springCloudVersion', "Greenwich.SR3")
}

dependencies {
    implementation 'org.springframework.cloud:spring-cloud-starter-gateway'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

dependencyManagement {
    imports {
        mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
    }
}
  • 作成したプロジェクトの application.yml に以下のような設定を追加していく
    • spring.cloud.gateway.routes 配下にルーティングする先のサーバー情報を記述していく
    • http://localhost:9001/frontend/** にアクセスすると、http://localhost:9002 のサーバーにリクエストが送られるようになる
application.yml
server:
  port: 9001

spring:
  cloud:
    gateway:
      routes:
        - id: frontend
          uri: http://localhost:9002
          predicates:
            - Path=/frontend/**
          filters:
            - RewritePath=/view/(?<segment>.*), /$\{segment}
        - id: backend
          uri: http://localhost:9003
          predicates:
            - Path=/backend/**
          filters:
            - RewritePath=/webapi/(?<segment>.*), /$\{segment}

まとめ

まだまだ他にも細かい設定ができそうなので、調べたり実装する機会があればその内容もまとめていこうかと思う。

0
0
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
0
0