SpringBoot -- 服務網關APIGateway

APIGateway

  • 對外提供服務接口
  • 對內根據邏輯調用內部多個接口,進行信息聚合返回給調用者
  • 異步調用無需等待反饋的服務

使用場景

  • 商品詳情: 需要調用商品基礎信息、推薦信息、評價、排名接口
  • 登錄+積分:調用登錄、積分規則鏈等接口
  • 鑑權
  • … …

Zuul

創建APIGateway module,引入spring-cloud-starter-zuul

build.gradle

apply plugin: 'org.springframework.boot'

dependencyManagement {
    imports {
        mavenBom "org.springframework.cloud:spring-cloud-dependencies:"+ springCloudVersion
        mavenBom "org.springframework.boot:spring-boot-dependencies:"+ springBootVersion
        mavenBom "org.springframework.boot:spring-boot-starter:"+ springBootVersion
    }
}

dependencies {
    compile('org.springframework.cloud:spring-cloud-starter-config')
    compile('org.springframework.cloud:spring-cloud-starter-eureka')
    compile('org.springframework.cloud:spring-cloud-starter-zuul')
    compile('org.springframework.cloud:spring-cloud-netflix-sidecar')
    compile('org.springframework.cloud:spring-cloud-starter-bus-kafka')
    compile('org.springframework.cloud:spring-cloud-stream')

    compile('org.xerial.snappy:snappy-java:' + snappyVersion)
    compile ('org.springframework.boot:spring-boot-starter-web')
    compile('org.springframework.boot:spring-boot-starter-log4j2')
    compile('org.apache.logging.log4j:log4j-1.2-api:'+ log4jAPIVersion)
    testCompile('org.springframework.boot:spring-boot-starter-test')
    testCompile group: 'junit', name: 'junit', version: '4.11'

}
configurations {
    all*.exclude module: 'spring-boot-starter-logging'
    all*.exclude module: 'logback-classic'
    all*.exclude module: 'log4j-over-slf4j'
    all*.exclude module: 'snappy-java'
}
sourceSets {
    main {
        resources.srcDirs = ['src/main/resources', 'src/main/java']
        resources.includes = ['**/*.xml', '**/*.yml']
    }
}
jar {
    baseName = 'apigateway-bootcwenao'
}

配置application.yml 啓用zuul

  • /servers/** 爲通過apigateway訪問時此服務對外提供的path
  • FEIGNSERVER 調用的服務在Eureka上註冊的ServiceId,此爲Feign server時的寫的module
  • 也可以採用Url的方式而不使用serviceid
zuul:
  routes:
    servers:
      path: /servers/**
      serviceId: FEIGNSERVER

設置zuul connect-timeout

zuul:
  max:
    host:
      connections: 200
  host:
    socket-timeout-millis: 60000
    connect-timeout-millis: 60000
hystrix:
  command:
    default:
      execution:
        isolation:
          thread:
            timeoutInMilliseconds: 10000

Application

@SpringCloudApplication
@EnableSidecar
public class ApiGatewayBootcwenaoApplication {

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

測試

  • 依次啓動 discovery、configserver、feignserver、ribbonserver
  • 訪問apigateway,加上需要訪問的server path

http://localhost:10002/servers/testFeign?content=Hello%20World

測試結果: Hello World for Springboot

Filter

  • 與SpringMvc中的過濾器功能一樣
  • 例如檢查請求來源
  • 自定義Filter需要extend ZuulFilter
  • 設置 filterType, 重寫run內的業務邏輯

創建 AccessSignFilter

public class AccessSignFilter extends ZuulFilter {}

filterType類型

  • pre:可以在請求被路由之前調用
  • routing:在路由請求時候被調用
  • post:在routing和error過濾器之後被調用
  • error:處理請求時發生錯誤時被調用

Application中啓用

@SpringCloudApplication
@EnableZuulProxy
@EnableSidecar
public class ApiGatewayBootcwenaoApplication {

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

    @Bean
    public AccessSignFilter accessSignFilter(){
        return new AccessSignFilter();
    }

}

代碼

代碼請移步 Github參考地址

如有疑問請加公衆號(K171),如果覺得對您有幫助請 github start
公衆號_k171

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章