API 网关
API 网关选型
1
|
xml version = "1.0" encoding = "UTF-8" ?>< project xmlns = "http://maven.apache.org/POM/4.0.0" xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation = "http://maven.apache.org/POM/4.0.0 [url]http://maven.apache.org/xsd/maven-4.0.0.xsd[/url]" > < modelVersion >4.0.0 modelVersion > < parent > < groupId >org.springframework.boot groupId > < artifactId >spring-boot-starter-parent artifactId > < version >2.1.3.RELEASE version > < relativePath />
parent > < groupId >com.example groupId > < artifactId >spring-cloud-gateway-example artifactId > < version >0.0.1-SNAPSHOT version > < name >spring-cloud-gateway-example name > < description >Demo project for Spring Boot description > < properties > < java.version >1.8 java.version > < spring-cloud.version >Greenwich.RELEASE spring-cloud.version > properties > < dependencies > < dependency > < groupId >org.springframework.cloud groupId > < artifactId >spring-cloud-starter-gateway artifactId > dependency > < dependency > < groupId >org.springframework.boot groupId > < artifactId >spring-boot-starter-test artifactId > < scope >test scope > dependency > dependencies > < dependencyManagement > < dependencies > < dependency > < groupId >org.springframework.cloud groupId > < artifactId >spring-cloud-dependencies artifactId > < version >${spring-cloud.version} version > < type >pom type > < scope >import scope > dependency > dependencies > dependencyManagement > < build > < plugins > < plugin > < groupId >org.springframework.boot groupId > < artifactId >spring-boot-maven-plugin artifactId > plugin > plugins > build > < repositories > < repository > < id >spring-milestones id > < name >Spring Milestones name > < url >[url]https://repo.spring.io/milestone url >[/url] repository > repositories > project >[url=]
|
1
|
package com.example.springcloudgatewayexample; import org.springframework.cloud.gateway.route.RouteLocator; import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configurationpublic class GatewayRoutes { @Bean public RouteLocator routeLocator(RouteLocatorBuilder builder) { return builder.routes() .route(r -> r.path( "/java/**" ) .filters( f -> f.stripPrefix( 1 ) ) .uri( "http://localhost:8090/helloWorld" ) ) .build(); }}
|
1
|
spring: cloud: gateway: routes: - predicates: - Path=/java/** filters: - StripPrefix= 1 uri: "http://localhost:8090/helloWorld"
|
1
|
. ____ _ __ _ _/\\ / ___ '_ __ _ _(_)_ __ __ _ \ \ \ \( ( )\___ | ' _ | '_| | ' _ \/ _` | \ \ \ \\\/ ___)| |_)| | | | | || (_| | ) ) ) )' |____| .__|_| |_|_| |_\__, | / / / /=========|_|==============|___/= /_/_/_/ :: Spring Boot :: (v2.1.3.RELEASE)2019-02-21 09:29:07.450 INFO 11704 --- [ main] c.e.demo.Spring5WebfluxApplication : Starting Spring5WebfluxApplication on DESKTOP-405G2C8 with PID 11704 (E:\workspaceForCloud\spring5-webflux\target\classes started by dell in E:\workspaceForCloud\spring5-webflux)2019-02-21 09:29:07.455 INFO 11704 --- [ main] c.e.demo.Spring5WebfluxApplication : No active profile set , falling back to default profiles: default2019-02-21 09:29:09.409 INFO 11704 --- [ main] o.s.b.web.embedded.netty.NettyWebServer : Netty started on port(s): 80902019-02-21 09:29:09.413 INFO 11704 --- [ main] c.e.demo.Spring5WebfluxApplication : Started Spring5WebfluxApplication in 2.304 seconds (JVM running for 7.311)
|
1
|
2019-02-21 10:34:33.435 INFO 8580 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration' of type [org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration$$EnhancerBySpringCGLIB$$1e059320] is not eligible for getting processed by all BeanPostProcessors ( for example: not eligible for auto-proxying) . ____ _ __ _ _ /\\ / ___ '_ __ _ _(_)_ __ __ _ \ \ \ \( ( )\___ | ' _ | '_| | ' _ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.1.3.RELEASE)2019-02-21 10:34:33.767 INFO 8580 --- [ main] e.s.SpringCloudGatewayExampleApplication : No active profile set, falling back to default profiles: default2019-02-21 10:34:34.219 INFO 8580 --- [ main] o.s.cloud.context.scope.GenericScope : BeanFactory id=d98183ec-3e46-38ba-ba4c-e976a1017dce2019-02-21 10:34:34.243 INFO 8580 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean ' org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration' of type [org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration$$EnhancerBySpringCGLIB$$1e059320] is not eligible for getting processed by all BeanPostProcessors ( for example: not eligible for auto-proxying)2019-02-21 10:34:44.367 INFO 8580 --- [ main] o.s.c.g.r.RouteDefinitionRouteLocator : Loaded RoutePredicateFactory [After]2019-02-21 10:34:44.367 INFO 8580 --- [ main] o.s.c.g.r.RouteDefinitionRouteLocator : Loaded RoutePredicateFactory [Before]2019-02-21 10:34:44.367 INFO 8580 --- [ main] o.s.c.g.r.RouteDefinitionRouteLocator : Loaded RoutePredicateFactory [Between]2019-02-21 10:34:44.367 INFO 8580 --- [ main] o.s.c.g.r.RouteDefinitionRouteLocator : Loaded RoutePredicateFactory [Cookie]2019-02-21 10:34:44.367 INFO 8580 --- [ main] o.s.c.g.r.RouteDefinitionRouteLocator : Loaded RoutePredicateFactory [Header]2019-02-21 10:34:44.368 INFO 8580 --- [ main] o.s.c.g.r.RouteDefinitionRouteLocator : Loaded RoutePredicateFactory [Host]2019-02-21 10:34:44.368 INFO 8580 --- [ main] o.s.c.g.r.RouteDefinitionRouteLocator : Loaded RoutePredicateFactory [Method]2019-02-21 10:34:44.368 INFO 8580 --- [ main] o.s.c.g.r.RouteDefinitionRouteLocator : Loaded RoutePredicateFactory [Path]2019-02-21 10:34:44.368 INFO 8580 --- [ main] o.s.c.g.r.RouteDefinitionRouteLocator : Loaded RoutePredicateFactory [Query]2019-02-21 10:34:44.368 INFO 8580 --- [ main] o.s.c.g.r.RouteDefinitionRouteLocator : Loaded RoutePredicateFactory [ReadBodyPredicateFactory]2019-02-21 10:34:44.368 INFO 8580 --- [ main] o.s.c.g.r.RouteDefinitionRouteLocator : Loaded RoutePredicateFactory [RemoteAddr]2019-02-21 10:34:44.368 INFO 8580 --- [ main] o.s.c.g.r.RouteDefinitionRouteLocator : Loaded RoutePredicateFactory [Weight]2019-02-21 10:34:44.368 INFO 8580 --- [ main] o.s.c.g.r.RouteDefinitionRouteLocator : Loaded RoutePredicateFactory [CloudFoundryRouteService]2019-02-21 10:34:44.920 INFO 8580 --- [ main] o.s.b.web.embedded.netty.NettyWebServer : Netty started on port(s): 80802019-02-21 10:34:44.923 INFO 8580 --- [ main] e.s.SpringCloudGatewayExampleApplication : Started SpringCloudGatewayExampleApplication in 12.329 seconds (JVM running for 13.126)
|
1
|
private Route(String id, URI uri, int order, AsyncPredicate this .id = id; this .uri = uri; this .order = order; this .predicate = predicate; this .gatewayFilters = gatewayFilters; }
|
1
|
/** * A predicate that checks if the path of the request matches the given pattern * @param patterns the pattern to check the path against. * The pattern is a {@link org.springframework.util.PathMatcher} pattern * @return a {@link BooleanSpec} to be used to add logical operators */ public BooleanSpec path(String... patterns) { return asyncPredicate(getBean(PathRoutePredicateFactory. class ) .applyAsync(c -> c.setPatterns(Arrays.asList(patterns)))); }
|
1
|
@Override public Predicate final ArrayList new ArrayList<>(); synchronized ( this .pathPatternParser) { pathPatternParser.setMatchOptionalTrailingSeparator( config.isMatchOptionalTrailingSeparator()); config.getPatterns().forEach(pattern -> { PathPattern pathPattern = this .pathPatternParser.parse(pattern); pathPatterns.add(pathPattern); }); } return exchange -> { PathContainer path = parsePath(exchange.getRequest().getURI().getPath()); Optional if (optionalPathPattern.isPresent()) { PathPattern pathPattern = optionalPathPattern.get(); traceMatch( "Pattern" , pathPattern.getPatternString(), path, true ); PathMatchInfo pathMatchInfo = pathPattern.matchAndExtract(path); putUriTemplateVariables(exchange, pathMatchInfo.getUriVariables()); return true ; } else { traceMatch( "Pattern" , config.getPatterns(), path, false ); return false ; } }; }
|