1.说明

本文详细介绍Spring Cloud创建Gateway模块的方法,
基于已经创建好的Spring Cloud父工程,
请参考SpringCloud创建项目父工程,
和已经创建好的Eureka子工程,
请参考SpringCloud创建Eureka模块,
创建Gateway模块这个子工程,
作为Spring Cloud的网关路由。

2.创建gateway模块

这一步创建一个Maven Module,
作为Spring Cloud的父工程下的一个子工程:
在父工程spring-cloud-demo上右键 -> New -> Other... -> Maven -> Maven Project

勾选Create a simple project(skip archetype selection),
输入Module Name:gateway,
查看Parent Project: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: gatewaycloud:gateway:routes:#路由的ID,没有固定规则但要求唯一,建议配合服务名- id: payment_routh #匹配后提供服务的路由地址uri: http://news.baidu.com   predicates:#断言,路径相匹配的进行路由- Path= /guonei/**   eureka:instance:#eureka客户端的实例名字(主机名)hostname: gateway-serviceclient:service-url:#表示向注册中心注册自己register-with-eureka: true#表示需要去注册中心检索服务fetch-registry: true#与eureka server交互的地址,包括查询服务和注册服务defaultZone: http://localhost:7001/eureka

主要是用于连接Eureka服务中心,
以及配置的Gateway的路由配置。

5.新增主启动类

在src/main/java目录下新增主启动类,
Package:com.yuwen.spring.gateway
Name:GatewayApplication
然后修改GatewayApplication.java如下,
注意一定要有EnableEurekaClient注解,
表示这是一个Eureka的客户端:

package com.yuwen.spring.gateway;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;@SpringBootApplication
@EnableEurekaClient
public class GatewayApplication {public static void main(String[] args) {SpringApplication.run(GatewayApplication.class, args);}
}

6.启动gateway

右键主启动类GatewayApplication.java,
Run As ... -> Java Application
主要提前启动Eureka Server服务,
成功启动日志如下,
可以看到对外提供的服务端口是6001:

.   ____          _            __ _ _/\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \\\/  ___)| |_)| | | | | || (_| |  ) ) ) )'  |____| .__|_| |_|_| |_\__, | / / / /=========|_|==============|___/=/_/_/_/:: Spring Boot ::        (v2.3.1.RELEASE)2020-07-10 12:24:23.363  INFO 18968 --- [           main] c.y.spring.gateway.GatewayApplication    : No active profile set, falling back to default profiles: default
2020-07-10 12:24:23.780  INFO 18968 --- [           main] o.s.cloud.context.scope.GenericScope     : BeanFactory id=db5a2a8d-f793-34a6-b94a-45993e42245a
2020-07-10 12:24:23.833  INFO 18968 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.cloud.client.loadbalancer.reactive.LoadBalancerBeanPostProcessorAutoConfiguration' of type [org.springframework.cloud.client.loadbalancer.reactive.LoadBalancerBeanPostProcessorAutoConfiguration] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2020-07-10 12:24:23.834  INFO 18968 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.cloud.client.loadbalancer.reactive.LoadBalancerBeanPostProcessorAutoConfiguration$ReactiveLoadBalancerConfig' of type [org.springframework.cloud.client.loadbalancer.reactive.LoadBalancerBeanPostProcessorAutoConfiguration$ReactiveLoadBalancerConfig] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2020-07-10 12:24:23.835  INFO 18968 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'deferringLoadBalancerExchangeFilterFunction' of type [org.springframework.cloud.client.loadbalancer.reactive.DeferringLoadBalancerExchangeFilterFunction] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2020-07-10 12:24:23.905  WARN 18968 --- [           main] c.n.c.sources.URLConfigurationSource     : No URLs will be polled as dynamic configuration sources.
2020-07-10 12:24:23.905  INFO 18968 --- [           main] c.n.c.sources.URLConfigurationSource     : To enable URLs as dynamic configuration sources, define System property archaius.configurationSource.additionalUrls or make config.properties available on classpath.
2020-07-10 12:24:23.908  WARN 18968 --- [           main] c.n.c.sources.URLConfigurationSource     : No URLs will be polled as dynamic configuration sources.
2020-07-10 12:24:23.908  INFO 18968 --- [           main] c.n.c.sources.URLConfigurationSource     : To enable URLs as dynamic configuration sources, define System property archaius.configurationSource.additionalUrls or make config.properties available on classpath.
2020-07-10 12:24:26.134  INFO 18968 --- [           main] o.s.c.g.r.RouteDefinitionRouteLocator    : Loaded RoutePredicateFactory [After]
2020-07-10 12:24:26.134  INFO 18968 --- [           main] o.s.c.g.r.RouteDefinitionRouteLocator    : Loaded RoutePredicateFactory [Before]
2020-07-10 12:24:26.134  INFO 18968 --- [           main] o.s.c.g.r.RouteDefinitionRouteLocator    : Loaded RoutePredicateFactory [Between]
2020-07-10 12:24:26.134  INFO 18968 --- [           main] o.s.c.g.r.RouteDefinitionRouteLocator    : Loaded RoutePredicateFactory [Cookie]
2020-07-10 12:24:26.134  INFO 18968 --- [           main] o.s.c.g.r.RouteDefinitionRouteLocator    : Loaded RoutePredicateFactory [Header]
2020-07-10 12:24:26.134  INFO 18968 --- [           main] o.s.c.g.r.RouteDefinitionRouteLocator    : Loaded RoutePredicateFactory [Host]
2020-07-10 12:24:26.134  INFO 18968 --- [           main] o.s.c.g.r.RouteDefinitionRouteLocator    : Loaded RoutePredicateFactory [Method]
2020-07-10 12:24:26.135  INFO 18968 --- [           main] o.s.c.g.r.RouteDefinitionRouteLocator    : Loaded RoutePredicateFactory [Path]
2020-07-10 12:24:26.135  INFO 18968 --- [           main] o.s.c.g.r.RouteDefinitionRouteLocator    : Loaded RoutePredicateFactory [Query]
2020-07-10 12:24:26.135  INFO 18968 --- [           main] o.s.c.g.r.RouteDefinitionRouteLocator    : Loaded RoutePredicateFactory [ReadBodyPredicateFactory]
2020-07-10 12:24:26.135  INFO 18968 --- [           main] o.s.c.g.r.RouteDefinitionRouteLocator    : Loaded RoutePredicateFactory [RemoteAddr]
2020-07-10 12:24:26.135  INFO 18968 --- [           main] o.s.c.g.r.RouteDefinitionRouteLocator    : Loaded RoutePredicateFactory [Weight]
2020-07-10 12:24:26.135  INFO 18968 --- [           main] o.s.c.g.r.RouteDefinitionRouteLocator    : Loaded RoutePredicateFactory [CloudFoundryRouteService]
2020-07-10 12:24:27.507  WARN 18968 --- [           main] ockingLoadBalancerClientRibbonWarnLogger : You already have RibbonLoadBalancerClient on your classpath. It will be used by default. As Spring Cloud Ribbon is in maintenance mode. We recommend switching to BlockingLoadBalancerClient instead. In order to use it, set the value of `spring.cloud.loadbalancer.ribbon.enabled` to `false` or remove spring-cloud-starter-netflix-ribbon from your project.
2020-07-10 12:24:27.513  WARN 18968 --- [           main] eactorLoadBalancerClientRibbonWarnLogger : You have RibbonLoadBalancerClient on your classpath. LoadBalancerExchangeFilterFunction that uses it under the hood will be used by default. Spring Cloud Ribbon is now in maintenance mode, so we suggest switching to ReactorLoadBalancerExchangeFilterFunction instead. In order to use it, set the value of `spring.cloud.loadbalancer.ribbon.enabled` to `false` or remove spring-cloud-starter-netflix-ribbon from your project.
2020-07-10 12:24:27.564  INFO 18968 --- [           main] o.s.c.n.eureka.InstanceInfoFactory       : Setting initial instance status as: STARTING
2020-07-10 12:24:27.657  INFO 18968 --- [           main] com.netflix.discovery.DiscoveryClient    : Initializing Eureka in region us-east-1
2020-07-10 12:24:28.214  INFO 18968 --- [           main] c.n.d.provider.DiscoveryJerseyProvider   : Using JSON encoding codec LegacyJacksonJson
2020-07-10 12:24:28.214  INFO 18968 --- [           main] c.n.d.provider.DiscoveryJerseyProvider   : Using JSON decoding codec LegacyJacksonJson
2020-07-10 12:24:28.286  INFO 18968 --- [           main] c.n.d.provider.DiscoveryJerseyProvider   : Using XML encoding codec XStreamXml
2020-07-10 12:24:28.286  INFO 18968 --- [           main] c.n.d.provider.DiscoveryJerseyProvider   : Using XML decoding codec XStreamXml
2020-07-10 12:24:28.392  INFO 18968 --- [           main] c.n.d.s.r.aws.ConfigClusterResolver      : Resolving eureka endpoints via configuration
2020-07-10 12:24:28.815  INFO 18968 --- [           main] com.netflix.discovery.DiscoveryClient    : Disable delta property : false
2020-07-10 12:24:28.815  INFO 18968 --- [           main] com.netflix.discovery.DiscoveryClient    : Single vip registry refresh property : null
2020-07-10 12:24:28.815  INFO 18968 --- [           main] com.netflix.discovery.DiscoveryClient    : Force full registry fetch : false
2020-07-10 12:24:28.815  INFO 18968 --- [           main] com.netflix.discovery.DiscoveryClient    : Application is null : false
2020-07-10 12:24:28.815  INFO 18968 --- [           main] com.netflix.discovery.DiscoveryClient    : Registered Applications size is zero : true
2020-07-10 12:24:28.815  INFO 18968 --- [           main] com.netflix.discovery.DiscoveryClient    : Application version is -1: true
2020-07-10 12:24:28.815  INFO 18968 --- [           main] com.netflix.discovery.DiscoveryClient    : Getting all instance registry info from the eureka server
2020-07-10 12:24:28.943  INFO 18968 --- [           main] com.netflix.discovery.DiscoveryClient    : The response status is 200
2020-07-10 12:24:28.944  INFO 18968 --- [           main] com.netflix.discovery.DiscoveryClient    : Starting heartbeat executor: renew interval is: 30
2020-07-10 12:24:28.945  INFO 18968 --- [           main] c.n.discovery.InstanceInfoReplicator     : InstanceInfoReplicator onDemand update allowed rate per min is 4
2020-07-10 12:24:28.948  INFO 18968 --- [           main] com.netflix.discovery.DiscoveryClient    : Discovery Client initialized at timestamp 1594355068947 with initial instances count: 0
2020-07-10 12:24:28.948  INFO 18968 --- [           main] o.s.c.n.e.s.EurekaServiceRegistry        : Registering application GATEWAY with eureka with status UP
2020-07-10 12:24:28.949  INFO 18968 --- [           main] com.netflix.discovery.DiscoveryClient    : Saw local status change event StatusChangeEvent [timestamp=1594355068949, current=UP, previous=STARTING]
2020-07-10 12:24:28.950  INFO 18968 --- [nfoReplicator-0] com.netflix.discovery.DiscoveryClient    : DiscoveryClient_GATEWAY/yuwen-asiainfo:gateway:6001: registering service...
2020-07-10 12:24:29.004  INFO 18968 --- [nfoReplicator-0] com.netflix.discovery.DiscoveryClient    : DiscoveryClient_GATEWAY/yuwen-asiainfo:gateway:6001 - registration status: 204
2020-07-10 12:24:29.505  INFO 18968 --- [           main] o.s.b.web.embedded.netty.NettyWebServer  : Netty started on port(s): 6001
2020-07-10 12:24:29.506  INFO 18968 --- [           main] .s.c.n.e.s.EurekaAutoServiceRegistration : Updating port to 6001
2020-07-10 12:24:30.752  INFO 18968 --- [           main] c.y.spring.gateway.GatewayApplication    : Started GatewayApplication in 9.197 seconds (JVM running for 10.148)
2020-07-10 12:24:58.947  INFO 18968 --- [freshExecutor-0] com.netflix.discovery.DiscoveryClient    : Disable delta property : false
2020-07-10 12:24:58.947  INFO 18968 --- [freshExecutor-0] com.netflix.discovery.DiscoveryClient    : Single vip registry refresh property : null
2020-07-10 12:24:58.947  INFO 18968 --- [freshExecutor-0] com.netflix.discovery.DiscoveryClient    : Force full registry fetch : false
2020-07-10 12:24:58.947  INFO 18968 --- [freshExecutor-0] com.netflix.discovery.DiscoveryClient    : Application is null : false
2020-07-10 12:24:58.947  INFO 18968 --- [freshExecutor-0] com.netflix.discovery.DiscoveryClient    : Registered Applications size is zero : true
2020-07-10 12:24:58.947  INFO 18968 --- [freshExecutor-0] com.netflix.discovery.DiscoveryClient    : Application version is -1: false
2020-07-10 12:24:58.947  INFO 18968 --- [freshExecutor-0] com.netflix.discovery.DiscoveryClient    : Getting all instance registry info from the eureka server
2020-07-10 12:24:58.972  INFO 18968 --- [freshExecutor-0] com.netflix.discovery.DiscoveryClient    : The response status is 200

7.测试效果

查看上面gateway的路由配置,
会把http://localhost:6001/guonei
的访问自动转到http://news.baidu.com/guonei,
即通过gateway请求到百度新闻国内页面。
浏览器访问效果如下:

如果访问http://localhost:6001/guoji,
由于没有配置相应的路由规则,
则会出现下面的错误页面:

http://www.taodudu.cc/news/show-1251001.html

相关文章:

  • Eclipse控制台Console使用说明
  • POI导入导出Excel(HSSF格式,User Model方式)
  • 正则表达式常用汇总
  • 接口文档编写技巧
  • MySQL客户端mysql常用命令
  • HAproxy开启日志记录
  • SpringCloud创建Config模块
  • SpringCloud创建Eureka Client服务注册
  • SpringCloud创建Config Client配置读取
  • SpringCloud创建Config Client通过Eureka访问Config
  • SpringCloud集成Security安全(Config配置中心)
  • SpringCloud集成Security安全(Eureka注册中心)
  • SpringCloud创建Config多客户端公共配置
  • SpringCloud创建Config读取本地配置
  • SpringCloud使用汇总Config
  • SpringCloud创建Eureka模块集群
  • Eclipse启动SpringCloud微服务集群的方法
  • SpringCloud发现服务代码(EurekaClient,DiscoveryClient)
  • SpringBoot集成Actuator监控管理
  • SpringBoot集成Actuator端点配置
  • SpringBoot集成Actuator健康指示器health
  • gRPC创建Java RPC服务
  • ProtoBuf3语法指南(Protocol Buffers)_上
  • ProtoBuf3语法指南(Protocol Buffers)_下
  • gPRC基本介绍
  • Log4j2日志框架集成Slf4j日志门面
  • XML解析的四种方式
  • XML解析和创建的JAXB方式
  • 【转载】JSON介绍
  • Elasticsearch单机安装Version7.10.1

SpringCloud创建Gateway模块相关推荐

  1. SpringCloud创建Eureka模块

    1.说明 本文详细介绍Spring Cloud创建Eureka模块的方法, 基于已经创建好的Spring Cloud父工程, 请参考SpringCloud创建项目父工程, 在里面创建Eureka模块, ...

  2. SpringCloud创建Eureka模块集群

    1.说明 本文详细介绍Spring Cloud创建Eureka模块集群的方法, 基于已经创建好的Spring Cloud Eureka Server模块, 请参考SpringCloud创建Eureka ...

  3. SpringCloud创建Config模块

    1.说明 本文详细介绍Spring Cloud创建Config模块的方法, 基于已经创建好的Spring Cloud父工程, 请参考SpringCloud创建项目父工程, 创建Config模块这个子工 ...

  4. SpringCloud创建项目父工程

    1.说明 本文详解介绍Spring Cloud项目的父工程创建, 由于Spring Cloud项目下有很多模块组件, 需要先创建一个大的父工程项目, 然后在下面创建各个子工程模块. 2.创建父工程 这 ...

  5. SpringCloud之Gateway

    1.Gateway是什么? 1.1 为微服务提供简单有效的路由管理方式 1.2 词汇 (1)Route(路由) :构建网关的基础模块,由ID.目标URL.断言和过滤器等组成 id:路由唯一标识,区别于 ...

  6. SpringCloud创建Config读取本地配置

    1.说明 Config Server获取配置支持的方式很多, 包括Git仓库(github/gitee等),任何与JDBC兼容的数据库, Subversion,Hashicorp Vault,Cred ...

  7. SpringCloud创建Config多客户端公共配置

    1.说明 基于已经创建好的Spring Cloud配置中心, 在配置中心仅保存一套配置文件, 多个客户端可以通过配置中心读取到相同的配置, 而不需要在每个客户端重复配置一遍, 下面以一个Config ...

  8. SpringCloud创建Config Client通过Eureka访问Config

    1.说明 本文详细介绍配置中心客户端使用方法, 即Config Client到Config Server读取配置. 读取配置的方式有两种, 第一种是直接配置Configer Server的URL, 第 ...

  9. SpringCloud创建Config Client配置读取

    1.说明 本文详细介绍配置中心客户端使用方法, 即Config Client到Config Server读取配置, 这里以创建Config Client服务为例, 基于已经创建好的Config Ser ...

最新文章

  1. GAN与力学系统的海森伯图像
  2. 鼠标取点——ginput()和getpts()详解
  3. 八月22日,django知识点总结:
  4. osi模型:七层模型介绍_联网| OSI模型能力问题和解答 套装1
  5. ORA-09925: Unable to create audit trail file 在DBCA时
  6. hystrix源码小贴士之中断
  7. java http get和post请求
  8. 螺旋矩阵的上下左右四指针解法
  9. 基于asyncio编写一个telegram爬虫机器人
  10. Python 基于modbus tcp 协议 实现与plc通信
  11. 【Git学习】解决GitLab内存消耗大的问题
  12. Linux清理文件内容的四种方式
  13. 在Silverlight 2 beta1中使用IronPython等动态语言
  14. 如何从零开始设计一款小程序原型?
  15. 啥?Grafana 还能为日志添加告警?
  16. 用python控制大华摄像头简单转动
  17. html播放h265,Web 播放 H.265视频
  18. urllib3.exceptions.MaxRetryError问题的解决
  19. [公式推导]用最简洁的方法证明多元正态分布的条件分布
  20. 北京公共自行车租赁方法_百度知道

热门文章

  1. [HDU4635] Strongly connected
  2. android 豆瓣客户端 视频
  3. 加两句代码让你的VC界面透明起来
  4. 前端工程师需要懂的前端面试题(c s s方面)总结(二)
  5. 前端基础牢记的一些操作-Github仓库管理
  6. 再见2019,拥抱2020
  7. 如何将CSS3 transforms应用于背景图像
  8. 有关cookie实现统计pv,uv的一些用法
  9. css3 中background的新增加的属性的用法(一)
  10. 计算机网络「二」—— 物理层(多图详解)