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 ...
随机推荐
- python中的logging日志模块
日志是程序不可或缺的一部分.它可以记录程序的运行情况,帮助我们更便捷地发现问题,而python中的logging日志模块给我们提供了这个机会. logging给我们提供了五种函数用来输出日志:debu ...
- vue中显示markdown文件为html
1.安装插件 npm install marked -D npm install highlight.js -D npm install markdown-loader -D npm instal ...
- Mysql 的使用方法
一 .Mysql 优势: 1.程序稳定性 :这样任意一台服务所在的机器崩溃了都不会影响数据和另外的服务. 2.数据一致性 :所有的数据都存储在一起,所有的程序操作的数据都是统一的,就不会出现数据不一致 ...
- thrift 安装
1.下载 去官网 https://thrift.apache.org/download 下载两个文件,下载地址 http://archive.apache.org/dist/thrift/0.9.3/ ...
- python中的一切皆对象
python中一切皆对象是这个语言灵活的根本.函数和类也是对象,属于python的一等公民.包括代码包和模块也都是对象.python的面向对象更加彻底. 可以赋值给一个变量可以添加到集合对象中可以作为 ...
- git 在 A 项目中引用 B 项目
git 在 A 项目中引用 B 项目 场景: 需要在项目calcDLL(http://XXX/XXXA.git) 中 引用 项目libindex(http://XXX/XXXB.git). 解决方 ...
- ELK实践
一.ElasticSearch+FileBeat+Kibana搭建平台 在C# 里面运行程序,输出日志(xxx.log 文本文件)到FileBeat配置的路径下面. 平台搭建,参考之前的随笔. Fil ...
- 32、reduceByKey和groupByKey对比
一.groupByKey 1.图解 val counts = pairs.groupByKey().map(wordCounts => (wordCounts._1, wordCounts._2 ...
- 【一起来烧脑】一步学会HTML体系
[外链图片转存失败(img-zk4xNuy1-1563431241992)(https://upload-images.jianshu.io/upload_images/11158618-4e9cac ...
- 高可用Redis:Redis Cluster
转(https://www.cnblogs.com/renpingsheng/p/9862485.html) Redis Cluster是Redis官方提供的Redis集群功能 1.为什么要实现Red ...