Nacos集成Spring Cloud Gateway 基础使用
项目结构
| 项目 | 端口 | 描述 |
nacos-provider |
8000 | 服务 |
nacos-getway |
8001 | 网关 |
nacos-provider项目
依赖
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>0.2.1.RELEASE</version>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
启动类
package com.example.nacosprovider; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient; @SpringBootApplication
@EnableDiscoveryClient
public class NacosProviderApplication { public static void main(String[] args) {
SpringApplication.run(NacosProviderApplication.class, args);
} }
配置文件
server:
port: 8000
spring:
application:
name: nacos-provider
cloud:
nacos:
discovery:
server-addr: localhost:8848
创建控制类ProviderController,http://localhost:8000/provider/helloProvider
package com.example.nacosprovider.controller; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; @RestController
@RequestMapping("/provider")
public class ProviderController { @GetMapping("/helloProvider")
public String helloProvider(){
return "你好,我是服务提供者";
}
}
nacos-getway项目: 在这里用的是yml配置文件的方法
依赖
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>0.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
</dependencies>
启动类
package com.example.nacosgetway; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient; @SpringBootApplication
@EnableDiscoveryClient
public class NacosGetwayApplication { public static void main(String[] args) {
SpringApplication.run(NacosGetwayApplication.class, args);
}
}
配置文件
server:
port: 8001
spring:
application:
name: nacos-getway
cloud:
nacos:
discovery:
server-addr: localhost:8848
gateway:
discovery:
locator:
enabled: true
lower-case-service-id: true
routes:
- id: nacos-getway-provider
uri: lb://nacos-provider
predicates:
- Path=/provider/**
- id: nacos-getway-consumer
uri: lb://nacos-consumer
predicates:
- Path=/**
各字段含义如下: id:我们自定义的路由 ID,保持唯一
uri:目标服务地址,以lb://开头(lb代表从注册中心获取服务)
predicates:路由条件,Predicate 接受一个输入参数,返回一个布尔值结果。
该接口包含多种默认方法来将 Predicate 组合成其他复杂的逻辑(比如:与,或,非)。
filters:过滤规则,本示例暂时没用。
启动测试
启动Nacos-server服务,nacos-provider服务以及nacos-getway服务
http://localhost:8000/provider/helloProvider

http://localhost:8001/provider/helloProvider

出现问题
1,nacos-getway服务启动不了
原因: 父工程的pom文件中引入了spring-boot-starter-web依赖

因为spring cloud gateway是基于webflux的,如果非要web支持的话需要导入spring-boot-starter-webflux而不是spring-boot-start-web。
解决方法: 将父工程中的spring-boot-starter-web依赖去掉,在需要这个依赖的子工程中单独添加
2,访问网关地址http://localhost:8001/provider/helloProvider返回404
原因: getway配置写的不对,将routes写成了route

Nacos集成Spring Cloud Gateway 基础使用的更多相关文章
- Spring Cloud Alibaba 初体验(二) Nacos 服务注册与发现 + 集成 Spring Cloud Gateway
一.服务注册 添加依赖: <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>s ...
- 通过Nacos动态刷新Spring Cloud Gateway的路由
通过Nacos动态刷新Spring Cloud Gateway的路由 一.背景 二.解决方案 三.实现功能 四.实现步骤 1.网关服务的实现 1.pom文件 2.bootstrap.yml配置文件 3 ...
- Nacos整合Spring Cloud Gateway实践
Spring Cloud Gateway官网:http://spring.io/projects/spring-cloud-gateway Eureka1.0的问题和Nacos对比:https://w ...
- Nacos整合Spring Cloud Gateway组件
一.什么是Spring Cloud Gateway Spring Cloud Gateway是Spring Cloud官方推出的网关框架,网关作为流量入口有着非常大的作用,常见的功能有路由转发.权限校 ...
- Spring Cloud Gateway + Nacos(1)简单配置
当初我学习时候就是参考这位大佬的博客: Nacos集成Spring Cloud Gateway 基础使用 现在学习到spring cloud alibaba 使用nacos做服务中心,dubbo做通信 ...
- 搭建一套ASP.NET Core+Nacos+Spring Cloud Gateway项目
前言 伴随着随着微服务概念的不断盛行,与之对应的各种解决方案也层出不穷.这毕竟是一个信息大爆发的时代,各种编程语言大行其道,各有各的优势.但是有一点未曾改变,那就是他们服务的方式,工作的时候各 ...
- Spring Cloud Alibaba基础教程-Nacos(三)
在Spring Cloud Alibaba基础教程-Nacos(二)当中学习了,如何使用 nacos图形化界面操作 ,使用Nacos部署集群,下面我们开始Nacos最后一篇的学习 ,如果对你有帮助,记 ...
- Dubbo想要个网关怎么办?试试整合Spring Cloud Gateway
一.背景 在微服务架构中 API网关 非常重要,网关作为全局流量入口并不单单是一个反向路由,更多的是把各个边缘服务(Web层)的各种共性需求抽取出来放在一个公共的"服务"(网关)中 ...
- Spring Cloud Gateway的动态路由怎样做?集成Nacos实现很简单
一.说明 网关的核心概念就是路由配置和路由规则,而作为所有请求流量的入口,在实际生产环境中为了保证高可靠和高可用,是尽量要避免重启的,所以实现动态路由是非常有必要的:本文主要介绍 Spring Clo ...
随机推荐
- 如何配置Memcached高速缓存,加快wordpress的速度
Memcached是什么 Memcached是一种高性能的分布式内存对象缓存系统.在动态应用,Memcached既能提高访问的速度,同时还减低了数据库的负载.DangaInteractive为提升Li ...
- 使用AspectCore实现AOP模式的Redis缓存
这次的目标是实现通过标注Attribute实现缓存的功能,精简代码,减少缓存的代码侵入业务代码. 缓存内容即为Service查询汇总的内容,不做其他高大上的功能,提升短时间多次查询的响应速度,适当减轻 ...
- 整理:VS常用快捷键
F1-F12 F1 帮助 F3: 查找下一个 Shift+F3: 查找上一个 F5: 启动调试 Ctrl+F5: 开始执行(不调试) Shift+F5: 停止调试 Ctrl+Shift+F5: 重启调 ...
- 三.基础部分+asp网站搭建
渗透测试流程:更全面地找出服务器的问题,更倾向保护 明确目标-->信息收集-->漏洞探测-->漏洞验证-->信息分析-->获取所需-->信息整理-->形成报告 ...
- 英语_金丝楠是紫楠(phoebeSheareri)的别名
姚黄魏紫俱凋零--红木家具今古谈(连载七) [上海木业网]楠木品种包括闽楠.细叶楠.红毛山楠.滇楠.白楠.紫楠.乌心楠.桢楠.水楠.香楠等二百余种之多,1997年的木材国家标准中就列入了八种.某些售卖 ...
- 动态样式语言less初识
资料参考与http://lesscss.cn/ Bootstrap--(less)下载安装配置 http://lesscss.cn Less使用两种方法 (1)在客户端使用LESS--学习 编写 x. ...
- shell脚本按当前日期输出日志
shell脚本基本技能是设置变量,输出信息.如下: sh脚本输出到控制台 将以上脚本保存到test.sh脚本文件中,并赋予可执行权限:sudo chmod +x test.sh 执行脚本:bash t ...
- 给普通用户赋予sudo权限后报错,提示/etc/sudoers文件权限拒绝
在Ubuntu 16.04系统里给普通用户赋予sudo权限,编辑vi /etc/sudoers 文件内容后发现执行sudo命令报错. 当我运行命令检查sudo权限的时候 sudo -i 输出错误提示: ...
- 13.生产环境中的 redis 是怎么部署的?
作者:中华石杉 面试题 生产环境中的 redis 是怎么部署的? 面试官心理分析 看看你了解不了解你们公司的 redis 生产集群的部署架构,如果你不了解,那么确实你就很失职了,你的 redis 是主 ...
- Nginx01(Nginx简介)
一:序言 Nginx是lgor Sysoev为俄罗斯访问量第二的rambler.ru站点设计开发的.从2004年发布至今,凭借开源的力量,已经接近成熟与完善. 二:Nginx常用功能 1.Http代理 ...