六、Spring Cloud Gateway路由

这篇文章讲述了如何简单地使用Spring Cloud Gateway,来源于Spring Cloud官方案例,
地址:https://spring.io/projects/spring-cloud-gateway#overview
文档:https://docs.spring.io/spring-cloud-gateway/docs/current/reference/html/

简介

Spring Cloud Gateway是Spring Cloud官方推出的第二代网关框架,取代Zuul网关。网关作为流量的,在微服务系统中有着非常作用,网关常见的功能有路由转发、权限校验、限流控制等作用。

This project provides an API Gateway built on top of the Spring
Ecosystem, including: Spring 5, Spring Boot 2 and Project Reactor.
Spring Cloud Gateway aims to provide a simple, yet effective way to
route to APIs and provide cross cutting concerns to them such as:
security, monitoring/metrics, and resiliency.

本项目提供了一个构建在 Spring 生态系统之上的 API 网关,包括:Spring 5、Spring Boot 2 和 Project
Reactor。Spring Cloud Gateway 旨在提供一种简单而有效的方式来路由到 API
并为它们提供交叉关注点,例如:安全性、监控/指标和弹性。

gateway可以路由到的三种目标地址:
1、websocket
2、http、https
3、微服务

在gateway中配置uri配置有三种方式,包括
第一种:ws(websocket)方式: uri: ws://localhost:9000
第二种:http方式: uri: http://localhost:8130/
第三种:lb(注册中心中服务名字)方式: uri: lb://brilliance-consumer

创建工程

nacos-provider
nacos-ribbon
nacos-feign
nacos-gateway

需要注意的是:nacos-ribbon、nacos-feign请求接口需要加上命令空间

nacos-ribbon

package com.nacosribbon;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RequestMapping("/ribbon")
@RestController
public class ConsumerController {
    @Autowired
    RestTemplate restTemplate;

    @GetMapping(value = "/hi-rest")
    public String hi(@RequestParam String name) {
        return restTemplate.getForObject("http://nacos-provider/hi?name="+name,String.class);
    }
}
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RequestMapping("/goods")
@RestController
public class GoodsController {
    @Autowired
    RestTemplate restTemplate;

    @GetMapping(value = "/goodsName")
    public String goodsName(@RequestParam String name) {
        return restTemplate.getForObject("http://nacos-provider/hi?name="+name,String.class);
    }
}

nacos-feign

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping("/feign")
@RestController
public class HiController {
    //编译器报错,无视。 因为这个Bean是在程序启动的时候注入的,编译器感知不到,所以报错。
    @Autowired
    ConsumerHi consumerHi;

    @GetMapping(value = "/hi-feign")
    public String sayHi(@RequestParam String name) {
        return consumerHi.sayHiFromConsumer( name );
    }
}

nacos-gateway配置

1、引入依赖

 <properties>
        <java.version>1.8</java.version>
        <spring.cloud-version>2020.0.4</spring.cloud-version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway</artifactId>
        </dependency>

        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
            <version>2021.1</version>
        </dependency>

        <!--fegin组件-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
        <!-- Feign Client for loadBalancing -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-loadbalancer</artifactId>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring.cloud-version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

2、nacos-gateway配置文件

在gateway中配置uri配置有三种方式,包括
第一种:ws(websocket)方式: uri: ws://localhost:9000
第二种:http方式: uri: http://localhost:8130/
第三种:lb(注册中心中服务名字)方式: uri: lb://brilliance-consumer

server.port=8764
spring.application.name=nacos-gateway

spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
spring.cloud.gateway.discovery.locator.enabled=true
spring.cloud.gateway.routes[0].id=NACOS-RIBBON
spring.cloud.gateway.routes[0].uri=lb://nacos-ribbon
spring.cloud.gateway.routes[0].predicates[0]= Path=/ribbon/**,/goods/**

spring.cloud.gateway.routes[1].id=NACOS-FEIGN
spring.cloud.gateway.routes[1].uri=lb://nacos-feign
spring.cloud.gateway.routes[1].predicates[0]= Path=/feign/**

3、启动类加上@EnableDiscoveryClient

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@EnableDiscoveryClient
@SpringBootApplication
public class NacosGatewayApplication {

    public static void main(String[] args) {
        SpringApplication.run(NacosGatewayApplication.class, args);
    }

}

访问gateway

请求ribbon的服务:

http://localhost:8764/ribbon/hi-rest?name=小火锅

服务端【8761】提供消息,客户端传过来的name= 小火锅

http://localhost:8764/goods/goodsName?name=小辣条

服务端【8761】提供消息,客户端传过来的name= 小辣条

请求feign的服务:

http://localhost:8764/feign/hi-feign?name=小蜘蛛

服务端【8761】提供消息,客户端传过来的name= 小蜘蛛

上面8761为nacos-provider的服务

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序员一灯

请给我打钱!!!谢谢,不客气!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值