- 对外提供服务接口
- 对内根据逻辑调用内部多个接口,进行信息聚合返回给调用者
- 异步调用无需等待反馈的服务
- 商品详情: 须要调用商品基础信息、推荐信息、评价、排名接口
- 登陆+积分:调用登陆、积分规则链等接口
- 鉴权
- … …
建立APIGateway module,引入spring-cloud-starter-zuuljavascript
build.gradlejava
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 启用zuulgit
- /servers/** 为经过apigateway访问时此服务对外提供的path
- FEIGNSERVER 调用的服务在Eureka上注册的ServiceId,此为Feign server时的写的module
- 也能够采用Url的方式而不使用serviceid
zuul:
routes:
servers:
path: /servers/**
serviceId: FEIGNSERVER
设置zuul connect-timeoutgithub
zuul:
max:
host:
connections: 200
host:
socket-timeout-millis: 60000
connect-timeout-millis: 60000
hystrix:
command:
default:
execution:
isolation:
thread:
timeoutInMilliseconds: 10000
Applicationweb
@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%20Worldspring
测试结果: Hello World for Springbootapache
- 与SpringMvc中的过滤器功能同样
- 例如检查请求来源
- 自定义Filter须要extend ZuulFilter
- 设置 filterType, 重写run内的业务逻辑
建立 AccessSignFilter api
public class AccessSignFilter extends ZuulFilter {}
filterType类型springboot
- pre:能够在请求被路由以前调用
- routing:在路由请求时候被调用
- post:在routing和error过滤器以后被调用
- error:处理请求时发生错误时被调用
在Application中启用ruby
@SpringCloudApplication
@EnableZuulProxy
@EnableSidecar
public class ApiGatewayBootcwenaoApplication {
public static void main(String[] args) {
SpringApplication.run(ApiGatewayBootcwenaoApplication.class, args);
}
@Bean
public AccessSignFilter accessSignFilter(){
return new AccessSignFilter();
}
}
若有疑问请加公众号(K171),若是以为对您有帮助请 github start