美文网首页
GateWay网关入门案例

GateWay网关入门案例

作者: CodeYang | 来源:发表于2021-09-02 12:06 被阅读0次

一、新建模块 cloud-gateway-gateway-9527

  1. 引用相关依赖,gateway , Eureka
 <!-- 引入网关依赖 gateway -->
 <dependency>
     <groupId>org.springframework.cloud</groupId>
     <artifactId>spring-cloud-starter-gateway</artifactId>
 </dependency>
 <!-- 引入注册中心 Eureka -->
 <dependency>
     <groupId>org.springframework.cloud</groupId>
     <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  1. 新建 application.yml 文件

配置基本信息、注册中心
配置路由 ; 这里配置的是cloud-provider-8001 里面的服务

server:
  port: 9527
spring:
  application:
    name: cloud-gateway
  cloud:
    gateway:
      routes:
        - id: provider_routh               #路由ID,没有固定规则但是要求唯一,建议配合服务名
          uri: http://localhost:8001       #匹配后提供服务的路由地址
          predicates:
            - Path=/provider/hystrix/**    #断言:路径相匹配进行路由

        - id: provider_routh2              #路由ID,没有固定规则但是要求唯一,建议配合服务名
          uri: http://localhost:8001       #匹配后提供服务的路由地址
          predicates:
            - Path=/provider/hello/**      #断言:路径相匹配进行路由

eureka:
  instance:
    hostname: cloud-gateway-service
  client:
    register-with-eureka: true
    fetch-registry: true
    service-url:
      defaultZone: http://localhost:7001/eureka

大致配置示意.png
  1. 新建启动类 GateWayApp9527
@SpringBootApplication
@EnableEurekaClient
public class GateWayApp9527 {
    public static void main(String[] args) {
        SpringApplication.run(GateWayApp9527.class);
    }
}
  1. 启动测试:分别启动,Eureka, cloud-provider-8001 ,cloud-gateway-gateway-9527
    访问 cloud-provider-8001 的:
    http://localhost:8001/provider/hello/AAZ
    http://localhost:8001/provider/hystrix/ok/123
    访问 cloud-gateway-gateway-9527-provider-8001 的:
    http://localhost:9527/provider/hello/AAZ
    http://localhost:9527/provider/hystrix/ok/123

发现两者结果一样,说明路由成功。

8001访问结果.png 9527访问结果.png

相关文章

  • GateWay配置网关的两种方式

    一、在yml文件中配置 见GateWay网关入门案例 二、代码中注入 RouteLocator 的Bean 在启动...

  • GateWay网关入门案例

    一、新建模块 cloud-gateway-gateway-9527 引用相关依赖,gateway , Eureka...

  • 第五章 Gateway--服务网关

    接上一篇文章开始网关之旅,首先告诉大家网关是什么,Gateway简介,怎么配置,怎么入门,执行流程等等相关介绍。 ...

  • Api网关(Spring cloud Gateway)

    1. Spring cloud Gateway网关 什么是网关? 网关就是网络请求的统一入口。gateway是sp...

  • Gateway网关

    Gateway网关: 在介绍之前,先要明白Gateway的作用,为什么用Gateway。 从图中能够看到,网关在微...

  • 高级框架第十二天Gateway:

    Gateway:网关 主要内容 1.API网关 2.Spring Cloud Gateway介绍 3.Gatewa...

  • 30.Gateway从nacos中获取服务信息

    Gateway快速入门 通过浏览器访问api网关,然后通过网关请求转发到商品微服务 基础版 第1步:创建一个api...

  • Gateway

    Gateway | 简介 Spring Cloud Gateway 是 Spring 官方提供的 API 网关; ...

  • 2020-02-14 网关

    网关的定义 网关(Gateway)又称网间连接器、协议转换器。顾名思义,网关(Gateway)就是一个网络连接到另...

  • Gateway网关入门学习

    概念简介 Cloud全家桶中有个很重要的组件就是网关,在1.x版本中都是采用Zuul网关;但是在2.x版本中,zu...

网友评论

      本文标题:GateWay网关入门案例

      本文链接:https://www.haomeiwen.com/subject/rlgdiltx.html