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

SpringBoot -- 服务网关APIGateway_第1张图片
公众号_k171

你可能感兴趣的:(SpringBoot -- 服务网关APIGateway)