SpringCloud创建Gateway模块
本文内容纲要:
-1.说明
-2.创建gateway模块
-3.添加依赖
-4.新增配置文件
-5.新增主启动类
-6.启动gateway
-7.测试效果
1.说明
本文详细介绍SpringCloud创建Gateway模块的方法,
基于已经创建好的SpringCloud父工程,
请参考SpringCloud创建项目父工程,
和已经创建好的Eureka子工程,
请参考SpringCloud创建Eureka模块,
创建Gateway模块这个子工程,
作为SpringCloud的网关路由。
2.创建gateway模块
这一步创建一个MavenModule,
作为SpringCloud的父工程下的一个子工程:
在父工程spring-cloud-demo上右键->New->Other...->Maven->MavenProject
勾选Createasimpleproject(skiparchetypeselection),
输入ModuleName:gateway,
查看ParentProject:spring-cloud-demo,
如果不是自己选择的父工程,请重新选择。
点击Finish完成工程创建。
3.添加依赖
在pom.xml中增加eureka-client的依赖,
以及spring-cloud-starter-gateway的依赖:
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
4.新增配置文件
在src/main/resource目录下新增application.yml文件,
并且增加如下配置:
server:
port:6001
spring:
application:
name:gateway
cloud:
gateway:
routes:
#路由的ID,没有固定规则但要求唯一,建议配合服务名
-id:payment_routh
#匹配后提供服务的路由地址
uri:http://news.baidu.com
predicates:
#断言,路径相匹配的进行路由
-Path=/guonei/**
eureka:
instance:
#eureka客户端的实例名字(主机名)
hostname:gateway-service
client:
service-url:
#表示向注册中心注册自己
register-with-eureka:true
#表示需要去注册中心检索服务
fetch-registry:true
#与eurekaserver交互的地址,包括查询服务和注册服务
defaultZone:http://localhost:7001/eureka
主要是用于连接Eureka服务中心,
以及配置的Gateway的路由配置。
5.新增主启动类
在src/main/java目录下新增主启动类,
Package:com.yuwen.spring.gateway
Name:GatewayApplication
然后修改GatewayApplication.java如下,
注意一定要有EnableEurekaClient注解,
表示这是一个Eureka的客户端:
packagecom.yuwen.spring.gateway;
importorg.springframework.boot.SpringApplication;
importorg.springframework.boot.autoconfigure.SpringBootApplication;
importorg.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableEurekaClient
publicclassGatewayApplication{
publicstaticvoidmain(String[]args){
SpringApplication.run(GatewayApplication.class,args);
}
}
6.启动gateway
右键主启动类GatewayApplication.java,
RunAs...->JavaApplication
主要提前启动EurekaServer服务,
成功启动日志如下,
可以看到对外提供的服务端口是6001:
._________
/\\/___'_____(_)______\\\\
(()\___|'_|'_||'_\/_`|\\\\
\\/___)||_)|||||||(_||))))
'|____|.__|_||_|_||_\__,|////
=========|_|==============|___/=/_/_/_/
::SpringBoot::(v2.3.1.RELEASE)
2020-07-1012:24:23.363INFO18968---[main]c.y.spring.gateway.GatewayApplication:Noactiveprofileset,fallingbacktodefaultprofiles:default
2020-07-1012:24:23.780INFO18968---[main]o.s.cloud.context.scope.GenericScope:BeanFactoryid=db5a2a8d-f793-34a6-b94a-45993e42245a
2020-07-1012:24:23.833INFO18968---[main]trationDelegate$BeanPostProcessorChecker:Bean'org.springframework.cloud.client.loadbalancer.reactive.LoadBalancerBeanPostProcessorAutoConfiguration'oftype[org.springframework.cloud.client.loadbalancer.reactive.LoadBalancerBeanPostProcessorAutoConfiguration]isnoteligibleforgettingprocessedbyallBeanPostProcessors(forexample:noteligibleforauto-proxying)
2020-07-1012:24:23.834INFO18968---[main]trationDelegate$BeanPostProcessorChecker:Bean'org.springframework.cloud.client.loadbalancer.reactive.LoadBalancerBeanPostProcessorAutoConfiguration$ReactiveLoadBalancerConfig'oftype[org.springframework.cloud.client.loadbalancer.reactive.LoadBalancerBeanPostProcessorAutoConfiguration$ReactiveLoadBalancerConfig]isnoteligibleforgettingprocessedbyallBeanPostProcessors(forexample:noteligibleforauto-proxying)
2020-07-1012:24:23.835INFO18968---[main]trationDelegate$BeanPostProcessorChecker:Bean'deferringLoadBalancerExchangeFilterFunction'oftype[org.springframework.cloud.client.loadbalancer.reactive.DeferringLoadBalancerExchangeFilterFunction]isnoteligibleforgettingprocessedbyallBeanPostProcessors(forexample:noteligibleforauto-proxying)
2020-07-1012:24:23.905WARN18968---[main]c.n.c.sources.URLConfigurationSource:NoURLswillbepolledasdynamicconfigurationsources.
2020-07-1012:24:23.905INFO18968---[main]c.n.c.sources.URLConfigurationSource:ToenableURLsasdynamicconfigurationsources,defineSystempropertyarchaius.configurationSource.additionalUrlsormakeconfig.propertiesavailableonclasspath.
2020-07-1012:24:23.908WARN18968---[main]c.n.c.sources.URLConfigurationSource:NoURLswillbepolledasdynamicconfigurationsources.
2020-07-1012:24:23.908INFO18968---[main]c.n.c.sources.URLConfigurationSource:ToenableURLsasdynamicconfigurationsources,defineSystempropertyarchaius.configurationSource.additionalUrlsormakeconfig.propertiesavailableonclasspath.
2020-07-1012:24:26.134INFO18968---[main]o.s.c.g.r.RouteDefinitionRouteLocator:LoadedRoutePredicateFactory[After]
2020-07-1012:24:26.134INFO18968---[main]o.s.c.g.r.RouteDefinitionRouteLocator:LoadedRoutePredicateFactory[Before]
2020-07-1012:24:26.134INFO18968---[main]o.s.c.g.r.RouteDefinitionRouteLocator:LoadedRoutePredicateFactory[Between]
2020-07-1012:24:26.134INFO18968---[main]o.s.c.g.r.RouteDefinitionRouteLocator:LoadedRoutePredicateFactory[Cookie]
2020-07-1012:24:26.134INFO18968---[main]o.s.c.g.r.RouteDefinitionRouteLocator:LoadedRoutePredicateFactory[Header]
2020-07-1012:24:26.134INFO18968---[main]o.s.c.g.r.RouteDefinitionRouteLocator:LoadedRoutePredicateFactory[Host]
2020-07-1012:24:26.134INFO18968---[main]o.s.c.g.r.RouteDefinitionRouteLocator:LoadedRoutePredicateFactory[Method]
2020-07-1012:24:26.135INFO18968---[main]o.s.c.g.r.RouteDefinitionRouteLocator:LoadedRoutePredicateFactory[Path]
2020-07-1012:24:26.135INFO18968---[main]o.s.c.g.r.RouteDefinitionRouteLocator:LoadedRoutePredicateFactory[Query]
2020-07-1012:24:26.135INFO18968---[main]o.s.c.g.r.RouteDefinitionRouteLocator:LoadedRoutePredicateFactory[ReadBodyPredicateFactory]
2020-07-1012:24:26.135INFO18968---[main]o.s.c.g.r.RouteDefinitionRouteLocator:LoadedRoutePredicateFactory[RemoteAddr]
2020-07-1012:24:26.135INFO18968---[main]o.s.c.g.r.RouteDefinitionRouteLocator:LoadedRoutePredicateFactory[Weight]
2020-07-1012:24:26.135INFO18968---[main]o.s.c.g.r.RouteDefinitionRouteLocator:LoadedRoutePredicateFactory[CloudFoundryRouteService]
2020-07-1012:24:27.507WARN18968---[main]ockingLoadBalancerClientRibbonWarnLogger:YoualreadyhaveRibbonLoadBalancerClientonyourclasspath.Itwillbeusedbydefault.AsSpringCloudRibbonisinmaintenancemode.WerecommendswitchingtoBlockingLoadBalancerClientinstead.Inordertouseit,setthevalueof`spring.cloud.loadbalancer.ribbon.enabled`to`false`orremovespring-cloud-starter-netflix-ribbonfromyourproject.
2020-07-1012:24:27.513WARN18968---[main]eactorLoadBalancerClientRibbonWarnLogger:YouhaveRibbonLoadBalancerClientonyourclasspath.LoadBalancerExchangeFilterFunctionthatusesitunderthehoodwillbeusedbydefault.SpringCloudRibbonisnowinmaintenancemode,sowesuggestswitchingtoReactorLoadBalancerExchangeFilterFunctioninstead.Inordertouseit,setthevalueof`spring.cloud.loadbalancer.ribbon.enabled`to`false`orremovespring-cloud-starter-netflix-ribbonfromyourproject.
2020-07-1012:24:27.564INFO18968---[main]o.s.c.n.eureka.InstanceInfoFactory:Settinginitialinstancestatusas:STARTING
2020-07-1012:24:27.657INFO18968---[main]com.netflix.discovery.DiscoveryClient:InitializingEurekainregionus-east-1
2020-07-1012:24:28.214INFO18968---[main]c.n.d.provider.DiscoveryJerseyProvider:UsingJSONencodingcodecLegacyJacksonJson
2020-07-1012:24:28.214INFO18968---[main]c.n.d.provider.DiscoveryJerseyProvider:UsingJSONdecodingcodecLegacyJacksonJson
2020-07-1012:24:28.286INFO18968---[main]c.n.d.provider.DiscoveryJerseyProvider:UsingXMLencodingcodecXStreamXml
2020-07-1012:24:28.286INFO18968---[main]c.n.d.provider.DiscoveryJerseyProvider:UsingXMLdecodingcodecXStreamXml
2020-07-1012:24:28.392INFO18968---[main]c.n.d.s.r.aws.ConfigClusterResolver:Resolvingeurekaendpointsviaconfiguration
2020-07-1012:24:28.815INFO18968---[main]com.netflix.discovery.DiscoveryClient:Disabledeltaproperty:false
2020-07-1012:24:28.815INFO18968---[main]com.netflix.discovery.DiscoveryClient:Singlevipregistryrefreshproperty:null
2020-07-1012:24:28.815INFO18968---[main]com.netflix.discovery.DiscoveryClient:Forcefullregistryfetch:false
2020-07-1012:24:28.815INFO18968---[main]com.netflix.discovery.DiscoveryClient:Applicationisnull:false
2020-07-1012:24:28.815INFO18968---[main]com.netflix.discovery.DiscoveryClient:RegisteredApplicationssizeiszero:true
2020-07-1012:24:28.815INFO18968---[main]com.netflix.discovery.DiscoveryClient:Applicationversionis-1:true
2020-07-1012:24:28.815INFO18968---[main]com.netflix.discovery.DiscoveryClient:Gettingallinstanceregistryinfofromtheeurekaserver
2020-07-1012:24:28.943INFO18968---[main]com.netflix.discovery.DiscoveryClient:Theresponsestatusis200
2020-07-1012:24:28.944INFO18968---[main]com.netflix.discovery.DiscoveryClient:Startingheartbeatexecutor:renewintervalis:30
2020-07-1012:24:28.945INFO18968---[main]c.n.discovery.InstanceInfoReplicator:InstanceInfoReplicatoronDemandupdateallowedrateperminis4
2020-07-1012:24:28.948INFO18968---[main]com.netflix.discovery.DiscoveryClient:DiscoveryClientinitializedattimestamp1594355068947withinitialinstancescount:0
2020-07-1012:24:28.948INFO18968---[main]o.s.c.n.e.s.EurekaServiceRegistry:RegisteringapplicationGATEWAYwitheurekawithstatusUP
2020-07-1012:24:28.949INFO18968---[main]com.netflix.discovery.DiscoveryClient:SawlocalstatuschangeeventStatusChangeEvent[timestamp=1594355068949,current=UP,previous=STARTING]
2020-07-1012:24:28.950INFO18968---[nfoReplicator-0]com.netflix.discovery.DiscoveryClient:DiscoveryClient_GATEWAY/yuwen-asiainfo:gateway:6001:registeringservice...
2020-07-1012:24:29.004INFO18968---[nfoReplicator-0]com.netflix.discovery.DiscoveryClient:DiscoveryClient_GATEWAY/yuwen-asiainfo:gateway:6001-registrationstatus:204
2020-07-1012:24:29.505INFO18968---[main]o.s.b.web.embedded.netty.NettyWebServer:Nettystartedonport(s):6001
2020-07-1012:24:29.506INFO18968---[main].s.c.n.e.s.EurekaAutoServiceRegistration:Updatingportto6001
2020-07-1012:24:30.752INFO18968---[main]c.y.spring.gateway.GatewayApplication:StartedGatewayApplicationin9.197seconds(JVMrunningfor10.148)
2020-07-1012:24:58.947INFO18968---[freshExecutor-0]com.netflix.discovery.DiscoveryClient:Disabledeltaproperty:false
2020-07-1012:24:58.947INFO18968---[freshExecutor-0]com.netflix.discovery.DiscoveryClient:Singlevipregistryrefreshproperty:null
2020-07-1012:24:58.947INFO18968---[freshExecutor-0]com.netflix.discovery.DiscoveryClient:Forcefullregistryfetch:false
2020-07-1012:24:58.947INFO18968---[freshExecutor-0]com.netflix.discovery.DiscoveryClient:Applicationisnull:false
2020-07-1012:24:58.947INFO18968---[freshExecutor-0]com.netflix.discovery.DiscoveryClient:RegisteredApplicationssizeiszero:true
2020-07-1012:24:58.947INFO18968---[freshExecutor-0]com.netflix.discovery.DiscoveryClient:Applicationversionis-1:false
2020-07-1012:24:58.947INFO18968---[freshExecutor-0]com.netflix.discovery.DiscoveryClient:Gettingallinstanceregistryinfofromtheeurekaserver
2020-07-1012:24:58.972INFO18968---[freshExecutor-0]com.netflix.discovery.DiscoveryClient:Theresponsestatusis200
7.测试效果
查看上面gateway的路由配置,
会把http://localhost:6001/guonei
的访问自动转到http://news.baidu.com/guonei,
即通过gateway请求到百度新闻国内页面。
浏览器访问效果如下:
如果访问http://localhost:6001/guoji,
由于没有配置相应的路由规则,
则会出现下面的错误页面:
本文内容总结:1.说明,2.创建gateway模块,3.添加依赖,4.新增配置文件,5.新增主启动类,6.启动gateway,7.测试效果,
原文链接:https://www.cnblogs.com/bugzeroman/p/13737759.html