0.9.0.RELEASE版本的spring cloud alibaba nacos+gateway网关实例
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网关实例的更多相关文章
- 0.9.0.RELEASE版本的spring cloud alibaba sentinel+gateway网关实例
sentinel除了让服务提供方.消费方用之外,网关也能用它来限流.我们基于上次整的网关(参见0.9.0.RELEASE版本的spring cloud alibaba nacos+gateway网关实 ...
- 0.9.0.RELEASE版本的spring cloud alibaba nacos+feign实例
这里的feign依然是原来的feign,只不过将注册中心由eureka换成了nacos.服务提供方参见0.9.0.RELEASE版本的spring cloud alibaba nacos实例,消费方跟 ...
- 0.9.0.RELEASE版本的spring cloud alibaba nacos实例
简而言之,nacos与eureka的不同之处有三:后台老板.部署方式.功能.nacos是阿里的,eureka是奈飞的:nacos有自己的安装包,需要独立部署,eureka仅作为一个服务组件,引入jar ...
- 0.9.0.RELEASE版本的spring cloud alibaba sentinel+feign降级处理实例
既然用到了feign,那么主要是针对服务消费方的降级处理.我们基于0.9.0.RELEASE版本的spring cloud alibaba nacos+feign实例添油加醋,把sentinel功能加 ...
- 0.9.0.RELEASE版本的spring cloud alibaba sentinel实例
sentinel即哨兵,相比hystrix断路器而言,它的功能更丰富.hystrix仅支持熔断,当服务消费方调用提供方发现异常后,进入熔断:sentinel不仅支持异常熔断,也支持响应超时熔断,另外还 ...
- 0.9.0.RELEASE版本的spring cloud alibaba sentinel限流、降级处理实例
先看服务提供方的,我们在原来的sentinel实例(参见0.9.0.RELEASE版本的spring cloud alibaba sentinel实例)上加上限流.降级处理,三板斧只需在最后那一斧co ...
- Spring Cloud Alibaba | Nacos动态网关路由
Spring Cloud Alibaba | Gateway基于Nacos动态网关路由 本篇实战所使用Spring有关版本: SpringBoot:2.1.7.RELEASE Spring Cloud ...
- Spring Cloud Alibaba | Nacos服务中心初探
目录 Spring Cloud Alibaba | Nacos服务中心初探 1. 什么是Nacos? 1.1 Nacos 1.0 1.2 Nacos 2.0 2. Nacos 架构及概念 2.1 服务 ...
- Spring Cloud Alibaba | Nacos服务注册与发现
目录 Spring Cloud Alibaba | Nacos服务注册与发现 1. 服务提供者 1.1 pom.xml项目依赖 1.2 配置文件application.yml 1.3 启动类Produ ...
随机推荐
- P2939 [USACO09FEB]改造路[分层图最短路]
题意翻译 约翰一共有N)个牧场.由M条布满尘埃的小径连接.小径可 以双向通行.每天早上约翰从牧场1出发到牧场N去给奶牛检查身体. 通过每条小径都需要消耗一定的时间.约翰打算升级其中K条小径,使之成为高 ...
- 行为型模式(二) 命令模式(Command)
一.动机(Motivate) 在我们的现实生活中有很多例子可以拿来说明这个模式,我们还拿吃饺子这个事情来说.我的奶奶说了,今天想吃饺子,发出了命令,然后我奶奶就去看电视去了.我们夫妻俩收到命令就开始和 ...
- 集成腾讯Bugly日志- Android(1)
Bugly 是腾讯公司为移动开发者开放的服务之一,这里主要指 Crash 监控.崩溃分析等质量跟踪服务. 一.登录BUGLY官网 1.登录BUGLY官网以后,选择新建产品,选择IOS或ADNROID平 ...
- Three.js中的div标签跟随(模型弹框)
目录 Three.js中的div标签跟随(模型弹框) 参考官方案例 核心渲染器 用法 注意事项 Three.js中的div标签跟随(模型弹框) 参考官方案例 核心渲染器 three.js-master ...
- wpf防止界面卡死
AsyncInvokeHelper.CallbackInvoke(new Action(() => { System.Threading.Tasks.Task.Factory.StartNew( ...
- SQLServer 使用自定义端口连接的方法(转载)
使用过SQL Server的人大多都知道,SQL Server服务器默认监听的端口号是1433,但是我今天遇到的问题是我的机器上有三个数据库实例,这样使用TCP/IP远程连接时就产生了问题.如何在Mi ...
- 「SCOI2011」棘手的操作
传送门 Description 有\(N\)个节点,标号从\(1\)到\(N\),这\(N\)个节点一开始相互不连通.第$ i\(个节点的初始权值为\)a_i$ ,接下来有如下一些操作: U x y ...
- python3之线程(一)
线程的概念 现在的操作系统几乎都支持运行多个任务,而在操作系统内部,一个任务往往代表的执行的某一个程序,也就是运行中的程序,运行的程序是一个动态的概念,也就是所说的进程,而在进程内部,往往有许多顺序执 ...
- 【CSP模拟赛】Adore(状压dp 二进制)
题目描述 小w偶然间见到了一个DAG.这个DAG有m层,第一层只有一个源点,最后一层只有一个汇点,剩下的每一层都有k个节点.现在小w每次可以取反第i(1<i<n-1)层和第i+1层之间的连 ...
- K8S中POD节点状态ContainerCreating原因排查
现象: # kubectl get pods -n kube-system |grep dashboard kubernetes-dashboard-6685cb584f-dqkwk 0/1 Cont ...