springboot2 springcloud Greenwich.SR3 构建微服务--11.gateway网关实现

本文主要是介绍springboot2 springcloud Greenwich.SR3 构建微服务--11.gateway网关实现,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Spring Cloud已经放弃Netflix Zuul了。现在Spring Cloud中引用的还是Zuul 1.x版本,而这个版本是基于过滤器的,是阻塞IO,不支持长连接。Zuul 2.x版本跟1.x的架构大一样,性能也有所提升。既然Spring Cloud已经不再集成Zuul 2.x了,那么是时候了解一下Spring Cloud Gateway了。

Netflix Zuul 1.x  VS  Netflix Zuul 2.x

不过不用担心, 我们还有 Spring Cloud Gateway

  • 基于Spring Framework 5、Project Reactor和Spring Boot 2.0构建
  • 能够在任意请求属性上匹配路由
  • predicates(谓词) 和 filters(过滤器)是特定于路由的
  • 集成了Hystrix断路器
  • 集成了Spring Cloud DiscoveryClient
  • 易于编写谓词和过滤器
  • 请求速率限制
  • 路径重写

 

Route : 路由是网关的基本组件。它由ID、目标URI、谓词集合和过滤器集合定义。如果聚合谓词为true,则匹配路由

Predicate : This is a Java 8 Function Predicate

Filter : 是GatewayFilter的一个实例,在这里,可以在发送下游请求之前或之后修改请求和响应

 原理

 

客户端向Spring Cloud Gateway发出请求。如果Gateway Handler Mapping确定请求与路由匹配,则将其发送给Gateway Web Handler。这个Handler运行通过特定于请求的过滤器链发送请求。过滤器可以在发送代理请求之前或之后执行逻辑。执行所有的“pre”过滤逻辑,然后发出代理请求,最后执行“post”过滤逻辑。

 

下面开始干活了

1.新建一个项目, 添加依赖

    <dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-gateway</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId></dependency></dependencies>

2.修改配置文件

spring.application.name=ad-gateway2
#下面这个配置了,但是没有生效....
#server.servlet.context-path=/gateway2/
server.port: 9001
eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka
#启用基于服务发现的路由定位
spring.cloud.gateway.discovery.locator.enabled=true
#启用服务实例id名称小写支持
spring.cloud.gateway.discovery.locator.lowerCaseServiceId=true

3.添加启动类注解:

package com.onyx.ad.gateway2;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;/*** @author zk* @Description:* @date 2019-09-25 11:36*/
@SpringBootApplication
@EnableDiscoveryClient
public class GatewayApplication {public static void main(String[] args) {SpringApplication.run(GatewayApplication.class,args);}}

 

4.配置路由规则, 这里我就不写配置文件中写了,以bean的方式进行注入

package com.onyx.ad.gateway2.config;import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.server.RequestPredicates;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.RouterFunctions;
import org.springframework.web.reactive.function.server.ServerResponse;/*** @author zk* @Description:* @date 2019-09-25 12:16*/
@Configuration
public class RouteConfig {@Beanpublic RouterFunction<ServerResponse> testFunRouterFunction() {RouterFunction<ServerResponse> route = RouterFunctions.route(RequestPredicates.path("/test"), request -> ServerResponse.ok().body(BodyInserters.fromObject("I am testing")));return route;}/*** 路由规则* @param builder* @return*/@Beanpublic RouteLocator customRouteLocator(RouteLocatorBuilder builder) {return builder.routes().route(r -> r.path("/test/custom").uri("http://ww.baidu.com"))//路由规则            //eureka中注册的服务提供者id.route(r -> r.path("/ad-search/**").uri("lb://AD-SEARCH")).route(r->r.path("/ad-sponsor/**").uri("lb:/AD-SPONSOR")).build();}}

 

启动服务, 

访问localhost:9001/test  还有   localhost:9001/test/custom 就可以访问到对应的页面, 这里先跳到百度去.

 


整个代码地址是:

https://github.com/changhe626/micro-service

Java Framework,欢迎各位前来交流java相关
QQ群:965125360

 

 

这篇关于springboot2 springcloud Greenwich.SR3 构建微服务--11.gateway网关实现的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/986267

相关文章

JVM 的类初始化机制

前言 当你在 Java 程序中new对象时,有没有考虑过 JVM 是如何把静态的字节码(byte code)转化为运行时对象的呢,这个问题看似简单,但清楚的同学相信也不会太多,这篇文章首先介绍 JVM 类初始化的机制,然后给出几个易出错的实例来分析,帮助大家更好理解这个知识点。 JVM 将字节码转化为运行时对象分为三个阶段,分别是:loading 、Linking、initialization

Spring Security 基于表达式的权限控制

前言 spring security 3.0已经可以使用spring el表达式来控制授权,允许在表达式中使用复杂的布尔逻辑来控制访问的权限。 常见的表达式 Spring Security可用表达式对象的基类是SecurityExpressionRoot。 表达式描述hasRole([role])用户拥有制定的角色时返回true (Spring security默认会带有ROLE_前缀),去

浅析Spring Security认证过程

类图 为了方便理解Spring Security认证流程,特意画了如下的类图,包含相关的核心认证类 概述 核心验证器 AuthenticationManager 该对象提供了认证方法的入口,接收一个Authentiaton对象作为参数; public interface AuthenticationManager {Authentication authenticate(Authenti

Spring Security--Architecture Overview

1 核心组件 这一节主要介绍一些在Spring Security中常见且核心的Java类,它们之间的依赖,构建起了整个框架。想要理解整个架构,最起码得对这些类眼熟。 1.1 SecurityContextHolder SecurityContextHolder用于存储安全上下文(security context)的信息。当前操作的用户是谁,该用户是否已经被认证,他拥有哪些角色权限…这些都被保

Spring Security基于数据库验证流程详解

Spring Security 校验流程图 相关解释说明(认真看哦) AbstractAuthenticationProcessingFilter 抽象类 /*** 调用 #requiresAuthentication(HttpServletRequest, HttpServletResponse) 决定是否需要进行验证操作。* 如果需要验证,则会调用 #attemptAuthentica

Spring Security 从入门到进阶系列教程

Spring Security 入门系列 《保护 Web 应用的安全》 《Spring-Security-入门(一):登录与退出》 《Spring-Security-入门(二):基于数据库验证》 《Spring-Security-入门(三):密码加密》 《Spring-Security-入门(四):自定义-Filter》 《Spring-Security-入门(五):在 Sprin

Java架构师知识体认识

源码分析 常用设计模式 Proxy代理模式Factory工厂模式Singleton单例模式Delegate委派模式Strategy策略模式Prototype原型模式Template模板模式 Spring5 beans 接口实例化代理Bean操作 Context Ioc容器设计原理及高级特性Aop设计原理Factorybean与Beanfactory Transaction 声明式事物

Java进阶13讲__第12讲_1/2

多线程、线程池 1.  线程概念 1.1  什么是线程 1.2  线程的好处 2.   创建线程的三种方式 注意事项 2.1  继承Thread类 2.1.1 认识  2.1.2  编码实现  package cn.hdc.oop10.Thread;import org.slf4j.Logger;import org.slf4j.LoggerFactory

hdu1043(八数码问题,广搜 + hash(实现状态压缩) )

利用康拓展开将一个排列映射成一个自然数,然后就变成了普通的广搜题。 #include<iostream>#include<algorithm>#include<string>#include<stack>#include<queue>#include<map>#include<stdio.h>#include<stdlib.h>#include<ctype.h>#inclu

嵌入式QT开发:构建高效智能的嵌入式系统

摘要: 本文深入探讨了嵌入式 QT 相关的各个方面。从 QT 框架的基础架构和核心概念出发,详细阐述了其在嵌入式环境中的优势与特点。文中分析了嵌入式 QT 的开发环境搭建过程,包括交叉编译工具链的配置等关键步骤。进一步探讨了嵌入式 QT 的界面设计与开发,涵盖了从基本控件的使用到复杂界面布局的构建。同时也深入研究了信号与槽机制在嵌入式系统中的应用,以及嵌入式 QT 与硬件设备的交互,包括输入输出设