gateway就是用来替换zuul的,功能都差不多,我们看下它怎么来跟nacos一起玩。老套路,三板斧:

  1、pom:

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <artifactId>spring-cloud-alibaba-gateway</artifactId>
<groupId>com.wlf</groupId>
<version>0.0.1-SNAPSHOT</version> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.7.RELEASE</version>
</parent> <dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Greenwich.SR2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>0.9.0.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement> <dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency> <dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency> </dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> </project>

  2、application.yml(因为涉及到路由映射,routes下面会有很多配置,yml成了标配):

#端口
server:
port: 8484
#应用名
spring:
application:
name: lxytrans-gateway
#注册中心
cloud:
nacos:
discovery:
server-addr: localhost:8848
register-enabled: true
# 网关
gateway:
discovery:
locator:
lowerCaseServiceId: true
enabled: true
routes:
- id: lxytrans-consumer
uri: lb://lxytrans-consumer
predicates:
- Path=/wlf/consumer/**
filters:
- StripPrefix=2
- id: lxytrans-provider
uri: lb://lxytrans-provider
predicates:
- Path=/wlf/provider/**
filters:
- StripPrefix=2

  

  这里的配置说明下:

   spring.cloud.gateway.discovery.locator.lowerCaseServiceId:支持服务实例名小写(如果nacos上的服务名是大写的话)。

  spring.cloud.gateway.discovery.locator.enabled:支持gateway到注册中心进行服务的注册和发现,打开后可以直接通过服务实例名访问。

  spring.cloud.gateway.routes:

  • id:路由的ID,唯一。
  • uri:匹配路由的转发地址,lb是load balance缩写,后面带转发的服务实例名。
  • predicates:配置该路由的断言,有很多种断言方式,可以组合使用。最常用的Path是请求路径匹配。
  • filters:过滤规则,也是有多种。这里StripPrefix用来截去Path前面的路径,从左边开始截,配1就截去1,上面配2,那么/wlf/provider/hello最终转发过去就是/hello。

  3、启动类:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient; @EnableDiscoveryClient
@SpringBootApplication
public class TransGatewayApplication {
public static void main(String[] args) {
SpringApplication.run(TransGatewayApplication.class, args);
}
}

  打完收功。跑起来后nacos会出现gateway实例:

  我们的测试还是基于之前弄的服务提供方和消费方(最新的参见0.9.0.RELEASE版本的spring cloud alibaba sentinel+feign降级处理实例)。从上面看到服务消费方、提供方的实例都是活的,通过网关来试试调用它们:

  直接用实例名(消费方调服务方超时降级):

 

  用路由:

0.9.0.RELEASE版本的spring cloud alibaba nacos+gateway网关实例的更多相关文章

  1. 0.9.0.RELEASE版本的spring cloud alibaba sentinel+gateway网关实例

    sentinel除了让服务提供方.消费方用之外,网关也能用它来限流.我们基于上次整的网关(参见0.9.0.RELEASE版本的spring cloud alibaba nacos+gateway网关实 ...

  2. 0.9.0.RELEASE版本的spring cloud alibaba nacos+feign实例

    这里的feign依然是原来的feign,只不过将注册中心由eureka换成了nacos.服务提供方参见0.9.0.RELEASE版本的spring cloud alibaba nacos实例,消费方跟 ...

  3. 0.9.0.RELEASE版本的spring cloud alibaba nacos实例

    简而言之,nacos与eureka的不同之处有三:后台老板.部署方式.功能.nacos是阿里的,eureka是奈飞的:nacos有自己的安装包,需要独立部署,eureka仅作为一个服务组件,引入jar ...

  4. 0.9.0.RELEASE版本的spring cloud alibaba sentinel+feign降级处理实例

    既然用到了feign,那么主要是针对服务消费方的降级处理.我们基于0.9.0.RELEASE版本的spring cloud alibaba nacos+feign实例添油加醋,把sentinel功能加 ...

  5. 0.9.0.RELEASE版本的spring cloud alibaba sentinel实例

    sentinel即哨兵,相比hystrix断路器而言,它的功能更丰富.hystrix仅支持熔断,当服务消费方调用提供方发现异常后,进入熔断:sentinel不仅支持异常熔断,也支持响应超时熔断,另外还 ...

  6. 0.9.0.RELEASE版本的spring cloud alibaba sentinel限流、降级处理实例

    先看服务提供方的,我们在原来的sentinel实例(参见0.9.0.RELEASE版本的spring cloud alibaba sentinel实例)上加上限流.降级处理,三板斧只需在最后那一斧co ...

  7. Spring Cloud Alibaba | Nacos动态网关路由

    Spring Cloud Alibaba | Gateway基于Nacos动态网关路由 本篇实战所使用Spring有关版本: SpringBoot:2.1.7.RELEASE Spring Cloud ...

  8. Spring Cloud Alibaba | Nacos服务中心初探

    目录 Spring Cloud Alibaba | Nacos服务中心初探 1. 什么是Nacos? 1.1 Nacos 1.0 1.2 Nacos 2.0 2. Nacos 架构及概念 2.1 服务 ...

  9. Spring Cloud Alibaba | Nacos服务注册与发现

    目录 Spring Cloud Alibaba | Nacos服务注册与发现 1. 服务提供者 1.1 pom.xml项目依赖 1.2 配置文件application.yml 1.3 启动类Produ ...

随机推荐

  1. TODO : 一些新的学习计划

    1.读完jvm那本书 2.加深Android的开发知识 3.编写atx的demo 4.跑几个apk的性能测试并做详细的性能分析 5.尝试实现一个uiautomator多个手机同时执行脚本的可能性(连线 ...

  2. spring boot学习笔记(一)

    (翻译看个人意愿) 官方介绍: Spring Boot makes it easy to create stand-alone, production-grade Spring based Appli ...

  3. 再论strlen sizeof

    今天,在使用字符串的时候,对sizeof和strlen的用法更加深入了,特此记录下. strlen是运行是计算的,不能放在函数外面计算的sizeof是预编译时运行的,可以放在函数外面计算. 对于cha ...

  4. light,node.js,webStorm 安装项目搭建

    light,是一个移动应用开发平台,旨在降低H5.APP的开发门槛.运维成本.提升移动应用产品的持续交付能力. 用light可以做什么 快速组织移动H5应用的协作开发.调试.应用发布,发布的应用可直接 ...

  5. Greenplum FTS故障检测原理

    前言 FTS(Fault Tolerance Serve)是GreenPlum中的故障检测服务,是保证GP高可用的核心功能.GreenPlum的Segment的健康检测及HA是由GP Master实现 ...

  6. shell编程题(三)

    将一目录下所有的文件的扩展名改为bak #! /bin/bash for i in `ls` do mv $i ${i%%.*}.bak done ${i%%.*} 截掉一个变量字符串第一个" ...

  7. LOJ#2983. 「WC2019」数树 排列组合,生成函数,多项式,FFT

    原文链接www.cnblogs.com/zhouzhendong/p/LOJ2983.html 前言 我怎么什么都不会?贺忙指导博客才会做. 题解 我们分三个子问题考虑. 子问题0 将红蓝共有的边连接 ...

  8. JS 中的prototype、__proto__与constructor

    我们需要牢记两点: ①__proto__和constructor属性是对象所独有的: ② prototype属性是函数所独有的,因为函数也是一种对象,所以函数也拥有__proto__和construc ...

  9. OpenFOAM-圆柱绕流

    原版视频下载地址:https://yunpan.cn/c64yrdt9J5LmQ  访问密码 0128 首先进行建模操作,任何建模软件均可,本教程采用ICEM直接建模,模型尺寸如下: 建成的模型如下: ...

  10. 数据结构Java版之遍历二叉树(六)

    二叉树是我们在程序中用的最多的一种树(个人观点).最简单的一个二叉树是由一个根节点,两个子节点(一左一右成左右孩子节点)组成.二叉树是数组和链表的结合,即包含了数组的快速查找优点,又包含了链表的快速添 ...