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. Union-Find(并查集): Quick find算法

    解决dynamic connectivity的一种算法:Quick find Quick find--Data sturcture 如果两个objects是相连的,则它们有相同的array value ...

  2. pycharm 远程修改服务器代码

    首先在本地和服务器上下载pydevd pip3 install pydevd 然后在 设置SSH连接, 出现:java.net.ConnectException:Connection refused ...

  3. 多线程实现的方式二实现Rannable

    package thread; class Thread2 implements Runnable{ private String name; public Thread2(String name) ...

  4. C# 异步的简单用法

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  5. (尚015)Vue过滤器(对显示的数据进行格式化)

    现在日期为:当前时间-1970年1月1日0时0分0秒的时间差 日期格式化:百度搜索moment 1.test015.html <!DOCTYPE html><html lang=&q ...

  6. shell命令的原理

    https://blog.csdn.net/m0_37925202/article/details/80258974 https://blog.csdn.net/a15929748502/articl ...

  7. 重写Dijkstra

    啊我沙雕了,竟然以为DJ的邻接矩阵不用初始化.. #include<bits/stdc++.h> #define R register int using namespace std; / ...

  8. Flask一种通用视图,增删改查RESTful API的设计

    模型设计是后端开发的第一步.数据模型反映了各种对象之间的相互关系. from app import db class Role(db.Model): """角色" ...

  9. Java RabbitMQ配置和使用,基于SpringBoot

    package rabbitmq.demo; import com.rabbitmq.client.AMQP; import org.junit.Test; import org.junit.runn ...

  10. Java 读写文件示例

    import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; public class T ...