写在前面

网关的作用不在此赘述,举个最常用的例子,我们搭建了微服务,前端调用各服务接口时,由于各服务接口不一样,如果让前端同事分别调用,前端同事会疯的。而网关就可以解决这个问题,网关屏蔽了各业务服务的端口,对前端同事来说,他们只负责调用网关服务端口下的服务就可以了。本文简单描述如何使用Spring Cloud全家桶中的网关服务,再配以Nacos。关于Nacos简单应用,可以看我其他博客。

服务提供者

https://start.spring.io/下载一个原始的spring boot工程,如何下载就不在这里说了。添加依赖:

  1. <dependency>
  2.   <groupId>org.springframework.boot</groupId>
  3.   <artifactId>spring-boot-starter-web</artifactId>
  4. </dependency>
  5. <dependency>
  6.   <groupId>com.alibaba.cloud</groupId>
  7.   <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
  8.   <version>2.2.1.RELEASE</version>
  9. </dependency>
  10. <dependency>
  11.   <groupId>com.alibaba.cloud</groupId>
  12.   <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
  13.   <version>2.2.1.RELEASE</version>
  14. </dependency>

分别添加了web依赖、配置中心依赖和注册中心依赖。

配置文件如下:

  1. server.port=8070
  2. spring.application.name=service-provider
  3. spring.cloud.nacos.config.server-addr=127.0.0.1:8848
  4. spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848

启动类如下:

  1. package com.chris.springboot;
  2.  
  3. import org.springframework.boot.SpringApplication;
  4. import org.springframework.boot.autoconfigure.SpringBootApplication;
  5. import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
  6.  
  7. @SpringBootApplication
  8. @EnableDiscoveryClient
  9. public class MySpringbootApplication {
  10.  
  11. public static void main(String[] args) {
  12. SpringApplication.run(MySpringbootApplication.class, args);
  13. }
  14. }

接口类如下:

  1. package com.chris.springboot.controller;
  2.  
  3. import org.springframework.beans.factory.annotation.Value;
  4. import org.springframework.cloud.context.config.annotation.RefreshScope;
  5. import org.springframework.web.bind.annotation.GetMapping;
  6. import org.springframework.web.bind.annotation.RequestMapping;
  7. import org.springframework.web.bind.annotation.RestController;
  8.  
  9. @RestController
  10. @RequestMapping("/provider")
  11. @RefreshScope
  12. public class ConfigController {
  13.  
  14. @Value(value = "${Hello:123}")
  15. private String hello;
  16.  
  17. @GetMapping("/helloProvider")
  18. public String helloProvider(){
  19. return hello;
  20. }
  21. }

此服务为我的博客:https://www.cnblogs.com/ncwuwsh/p/12732516.html中的服务,可参看。

网关服务

https://start.spring.io/下载一个原始的spring boot工程,如何下载就不在这里说了。添加依赖:

  1.      <dependency>
  2. <groupId>org.springframework.cloud</groupId>
  3. <artifactId>spring-cloud-starter-gateway</artifactId>
  4. </dependency>
  5. <dependency>
  6. <groupId>com.alibaba.cloud</groupId>
  7. <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
  8. <version>2.2.1.RELEASE</version>
  9. </dependency>

注意,千万不要添加web依赖。

配置文件可以使用properties,也可以使用yml格式。yml格式如下:

  1. server:
  2. port: 8080
  3. spring:
  4. application:
  5. name: api-gateway
  6. cloud:
  7. nacos:
  8. discovery:
  9. server-addr: 127.0.0.1:8848
  10. gateway:
  11. discovery:
  12. locator:
  13. enabled: true #表明gateway开启服务注册和发现的功能,并且spring cloud gateway自动根据服务发现为每一个服务创建了一个router,这个router将以服务名开头的请求路径转发到对应的服务。
  14. lower-case-service-id: true #是将请求路径上的服务名配置为小写(因为服务注册的时候,向注册中心注册时将服务名转成大写的了),比如以/service-hi/*的请求路径被路由转发到服务名为service-hi的服务上。
  15. routes:
  16. - id: gateway-service
  17. uri: lb://service-provider #此配置的值注册到Nacos中服务提供者的spring.application.name的值
  18. predicates:
  19. - Path=/provider/**

使用yml的同学,一定要去查下yml的一些规则,比如 :后面,值的前面,一定要有空格,缩进不要使用tab键,而要用两个空格缩进等

下面是properties格式配置文件:

  1. server.port=8080
  2. spring.application.name=api-gateway
  3. spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
  4. #表明gateway开启服务注册和发现的功能,并且spring cloud gateway自动根据服务发现为每一个服务创建了一个router,这个router将以服务名开头的请求路径转发到对应的服务。
  5. spring.cloud.gateway.discovery.locator.enabled=true
  6. #是将请求路径上的服务名配置为小写(因为服务注册的时候,向注册中心注册时将服务名转成大写的了),比如以/service-hi/*的请求路径被路由转发到服务名为service-hi的服务上。
  7. spring.cloud.gateway.discovery.locator.lower-case-service-id=true
  8. spring.cloud.gateway.routes[0].id=gateway-service
  9. spring.cloud.gateway.routes[0].uri=lb://service-provider
  10. spring.cloud.gateway.routes[0].predicates[0]=Path=/provider/**

下面是网关的启动类:

  1. package com.chris.gatewayrouter;
  2.  
  3. import org.springframework.boot.SpringApplication;
  4. import org.springframework.boot.autoconfigure.SpringBootApplication;
  5. import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
  6. import org.springframework.cloud.gateway.route.RouteLocator;
  7. import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
  8. import org.springframework.context.annotation.Bean;
  9.  
  10. @SpringBootApplication
  11. @EnableDiscoveryClient
  12. public class GatewayrouterApplication {
  13.  
  14. public static void main(String[] args) {
  15. SpringApplication.run(GatewayrouterApplication.class, args);
  16. }
  17.  
  18. @Bean
  19. public RouteLocator myRoutes(RouteLocatorBuilder builder) {
  20. return builder.routes().build();
  21. }
  22.  
  23. }

然后启动Nacos,服务提供者和网关服务,使用浏览器访问:http://127.0.0.1:8080/provider/helloProvider

搞定。

网关服务的其他高级应用,自己去看官网吧。

官网是最好的老师

Spring Cloud Gateway+Nacos,yml+properties两种配置文件方式搭建网关服务的更多相关文章

  1. springcloud3(五) spring cloud gateway动态路由的四类实现方式

    写这篇博客主要是为了汇总下动态路由的多种实现方式,没有好坏之分,任何的方案都是依赖业务场景需求的,现在网上实现方式主要有: 基于Nacos, 基于数据库(PosgreSQL/Redis), 基于Mem ...

  2. Spring Cloud Gateway + Nacos(1)简单配置

    当初我学习时候就是参考这位大佬的博客: Nacos集成Spring Cloud Gateway 基础使用 现在学习到spring cloud alibaba 使用nacos做服务中心,dubbo做通信 ...

  3. Spring Cloud实战: 基于Spring Cloud Gateway + vue-element-admin 实现的RBAC权限管理系统,实现网关对RESTful接口方法权限和自定义Vue指令对按钮权限的细粒度控制

    一. 前言 信我的哈,明天过年. 这应该是农历年前的关于开源项目 的最后一篇文章了. 有来商城 是基于 Spring Cloud OAuth2 + Spring Cloud Gateway + JWT ...

  4. 网关服务Spring Cloud Gateway(二)

    上一篇文章服务网关 Spring Cloud GateWay 初级篇,介绍了 Spring Cloud Gateway 的相关术语.技术原理,以及如何快速使用 Spring Cloud Gateway ...

  5. 微服务网关实战——Spring Cloud Gateway

    导读 作为Netflix Zuul的替代者,Spring Cloud Gateway是一款非常实用的微服务网关,在Spring Cloud微服务架构体系中发挥非常大的作用.本文对Spring Clou ...

  6. 跟我学SpringCloud | 第十三篇:Spring Cloud Gateway服务化和过滤器

    SpringCloud系列教程 | 第十三篇:Spring Cloud Gateway服务化和过滤器 Springboot: 2.1.6.RELEASE SpringCloud: Greenwich. ...

  7. Spring Cloud Alibaba学习笔记(16) - Spring Cloud Gateway 内置的路由谓词工厂

    Spring Cloud Gateway路由配置的两种形式 Spring Cloud Gateway的路由配置有两种形式,分别是路由到指定的URL以及路由到指定的微服务,在上文博客的示例中我们就已经使 ...

  8. Nacos整合Spring Cloud Gateway组件

    一.什么是Spring Cloud Gateway Spring Cloud Gateway是Spring Cloud官方推出的网关框架,网关作为流量入口有着非常大的作用,常见的功能有路由转发.权限校 ...

  9. 跟我学SpringCloud | 第十二篇:Spring Cloud Gateway初探

    SpringCloud系列教程 | 第十二篇:Spring Cloud Gateway初探 Springboot: 2.1.6.RELEASE SpringCloud: Greenwich.SR1 如 ...

随机推荐

  1. Python批量修改文件名模板

    源码如下:import os import re import sys filePath = r'F:\BaiduNetdiskDownload\COVID-19CTSeg\3DUNet-Pytorc ...

  2. go server框架学习之路 - 写一个自己的go框架

    go server框架学习之路 - 写一个自己的go框架 用简单的代码实现一个go框架 代码地址: https://github.com/cw731/gcw 1 创建一个简单的框架 代码 packag ...

  3. python使用镜像源安装库

    pip install django -i http://pypi.douban.com/simple --trusted-host pypi.douban.com 豆瓣 :http://pypi.d ...

  4. cut-trailing-bytes:二进制尾部去0小工具

    背景 之前的文章 二进制文件处理之尾部补0和尾部去0 中介绍了一种使用 sed 去除二进制文件尾部的 NULL(十六进制0x00)字节的方法. 最近发现这种方法有局限性,无法处理较大的文件.因为 se ...

  5. Python学习-第五节:面向对象

    概念: 核心是“过程”二字,“过程”指的是解决问题的步骤,即先干什么再干什么......,基于面向过程设计程序就好比在设计一条流水线,是一种机械式的思维方式.若程序一开始是要着手解决一个大的问题,面向 ...

  6. 使用ElasticSearch赋能HBase二级索引 | 实践一年后总结

    前言:还记得那是2018年的一个夏天,天气特别热,我一边擦汗一边听领导大刀阔斧的讲述自己未来的改革蓝图.会议开完了,核心思想就是:我们要搞一个数据大池子,要把公司能灌的数据都灌入这个大池子,然后让别人 ...

  7. 前端上传视频、图片、文件等大文件 组件Plupload使用指南

    demo:https://blog.csdn.net/qq_30100043/article/details/78491993 Plupload上传插件中文帮助文档网址:http://www.phpi ...

  8. 本地Vue项目跨域请求本地Node.js服务器的配置方法

    前言:跨域请求是在本地开发时经常遇到的需求,也很简单,只是几句代码配置一下的问题.我初次配置跨域请求时由于官方的说明太简洁,找到的教程又落伍,调试了一番并没有解决问题,到最后解决问题,已花费了很多时间 ...

  9. 关于android中数据库的创建以及基础的增删改查的相应操作

    这里主要是掌握一些基本的相应的知识,具体的是阿金是等到明天在进行. 相应的知识点如下: 对于数据库中的一些常识.SQLite 没有服务器进程,它通过文件保存数据,该文件是跨平台的,可以放在其他平台中使 ...

  10. 修改linux服务器名称

    临时修改: hostname test //退出shell,在进入即可修改成功 永久修改 CentOs: vi /etc/hostname //直接+名字即可 //Ubuntu 系统 /etc/hos ...