网关作为对外服务,在微服务架构中是一个很重要的组件,主要体现在动态路由和接入鉴权这两个功能上。现在我们通过Spring Cloud Zuul来实现对之前a-feign-client(参见Greenwich.SR2版本的Spring Cloud Feign实例)调用的路由映射,并对外部请求做一个简单的鉴权。三板斧祭出:

  1、pom里引入spring-cloud-starter-netflix-zuul:

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId>
<artifactId>gateway</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency> <dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Greenwich.SR2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> </project>

  2、application:

#本机端口
server.port=8765 #服务名
spring.application.name=api-gateway #注册中心地址
eureka.client.service-url.defaultZone=http://localhost:8888/eureka/ #路由规则
zuul.routes.hello.path=/hello/** #路由映射
zuul.routes.hello.service-id=a-feign-client

  3、主类通过@EnableZuulProxy启动网关服务,新增一个过滤器SimpleFilter:

package hello;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
import org.springframework.context.annotation.Bean;
import hello.filters.pre.SimpleFilter; @EnableZuulProxy
@SpringBootApplication
public class GatewayApplication { public static void main(String[] args) {
SpringApplication.run(GatewayApplication.class, args);
} @Bean
public SimpleFilter simpleFilter() {
return new SimpleFilter();
} }
package hello.filters.pre;

import javax.servlet.http.HttpServletRequest;

import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.ZuulFilter; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; public class SimpleFilter extends ZuulFilter { private static Logger log = LoggerFactory.getLogger(SimpleFilter.class); @Override
public String filterType() {
return "pre";
} @Override
public int filterOrder() {
return 1;
} @Override
public boolean shouldFilter() {
return true;
} @Override
public Object run() {
RequestContext ctx = RequestContext.getCurrentContext();
HttpServletRequest request = ctx.getRequest(); log.info(String.format("%s request to %s", request.getMethod(), request.getRequestURL().toString())); Object accessToken = request.getParameter("accessToken");
if (accessToken == null) {
log.warn("access token is empty");
ctx.setSendZuulResponse(false);
ctx.setResponseStatusCode(401);
ctx.setResponseBody("{\"result\":\"accessToken为空!\"}");
ctx.getResponse().setContentType("text/html;charset=UTF-8");
return null;
}
log.info("access token ok"); return null;
} }

  OK,我们在浏览器请求http://localhost:8765/hello/consumer/sayHi?name=world看看:

  加上accessToken参数(参数值随便填一个)再请求一次:

  我们看到路由和鉴权都实现了。

Greenwich.SR2版本的Spring Cloud Zuul实例的更多相关文章

  1. Greenwich.SR2版本的Spring Cloud Feign实例

    前面我们了解了Spring Cloud Ribbon和Hystrix,在使用上它们基本上会成队出现,那么是不是可以把它们组合起来使用?而且我们发现,在服务消费方a-beautiful-client里通 ...

  2. Greenwich.SR2版本的Spring Cloud Hystrix实例

    之前我们在eureka(参见Greenwich.SR2版本的Spring Cloud Eureka实例)中,服务消费方a-beautiful-client调用服务提供方a-bootiful-clien ...

  3. Greenwich.SR2版本的Spring Cloud Ribbon实例

    上次我们了解了eureka(参见Greenwich.SR2版本的Spring Cloud Eureka实例),里面的服务消费方(服务实例a-beautiful-client)我们其实已经用到了ribb ...

  4. Greenwich.SR2版本的Spring Cloud Eureka实例

    作为微服务架构中最为核心和基础的服务治理,注册中心提供了微服务实例的自动化注册与发现.而作为一个服务注册中心,eureka的作用与传统的zk.etcd的作用是一样的,同样也支持高可用(集群).不同之处 ...

  5. Greenwich.SR2版本的Spring Cloud Zipkin实例

    调用链跟踪是微服务架构中的基础能力,Spring Cloud Zipkin+Sleuth为我们提供了该能力.首先我们先建立Zipkin服务端,它需要集成Eureka,用于发现服务提供方和消费方,进行数 ...

  6. Greenwich.SR2版本的Spring Cloud Config+BUS实例

    Spring Cloud Config统一的配置中心同注册中心Eureka一样,也分服务端和客户端.服务端用来保存配置信息,客户端用来读取.它的优势是基于Git仓库,支持多环境.多分支配置.动态刷新. ...

  7. 0.9.0.RELEASE版本的spring cloud alibaba nacos+gateway网关实例

    gateway就是用来替换zuul的,功能都差不多,我们看下它怎么来跟nacos一起玩.老套路,三板斧: 1.pom: <?xml version="1.0" encodin ...

  8. 0.9.0.RELEASE版本的spring cloud alibaba nacos+feign实例

    这里的feign依然是原来的feign,只不过将注册中心由eureka换成了nacos.服务提供方参见0.9.0.RELEASE版本的spring cloud alibaba nacos实例,消费方跟 ...

  9. 0.9.0.RELEASE版本的spring cloud alibaba nacos实例

    简而言之,nacos与eureka的不同之处有三:后台老板.部署方式.功能.nacos是阿里的,eureka是奈飞的:nacos有自己的安装包,需要独立部署,eureka仅作为一个服务组件,引入jar ...

随机推荐

  1. rabbitmq二进制安装

    一.erland的安装 1.首先测试一下是否已经安装了erlang,命令 rpm -qa | grep erlang 2.没有安装则用yum安装 yum install erlang -y 二.下载r ...

  2. zoj 4099 Extended Twin Composite Number

    Do you know the twin prime conjecture? Two primes  and  are called twin primes if . The twin prime c ...

  3. c++ 流对象之streambuf(可当做缓冲区使用)

    在C++ 中引入了流的概念,我们很方便的通过流来读写文本数据和二进制数据,那么流对象的数据究竟是怎么存储的呢,为了搞清这个问题,先来看一看c++ 的 io 体系: 由图可以看出,在stream 的实现 ...

  4. 修改httpd端口

    修改httpd端口 默认httpd端口为80,现在改成800 修改两个地方: 1.修改配置文件httpd.conf listen 把80改成需要的端口 2.修改配置文件httpd-vhosts.con ...

  5. 【Java】用五种语言写数组(用于自己学习)

    C语言 #include<stdio.h> int main() { ] = {, , , , , , , , , }; int i; ;i<;i++) printf("% ...

  6. mac下解决中文乱码的问题

    最近在做自己的博客项目,需要在公司和家代码同步,当然代码托管到了码云上面,但是公司是win系统,家里是mac系统,奇怪的是在公司.....算了不废话了,直接正题吧. 就是代码在公司中文显示没毛病,然后 ...

  7. js字符串解析与转换成数字

    解析允许字符串中含有非法数字字符,解析按从左至右的顺序,如果遇到非数字字符就停止.而转换不允许出现非数字字符,否则会失败并返回NaN

  8. Django REST framework+Vue 打造生鲜电商项目(笔记九)

    (from:http://www.cnblogs.com/derek1184405959/p/8859309.html) 十二.支付宝沙箱环境配置 12.1.创建应用 进入蚂蚁金服开放平台(https ...

  9. 淘宝上的大智慧L2数据,月卡最便宜是8元钱,这个也可以获取BBD、DDX等数据!

    Want:从顶牛股网上下载DDX数据. 1.下载历史DDE数据:获取最近120个交易日的DDE数据 #define SFURL_DNG_SINGLEL"http://www.dingniug ...

  10. Codeforces Round #597 (Div. 2) C. Constanze's Machine

    链接: https://codeforces.com/contest/1245/problem/C 题意: Constanze is the smartest girl in her village ...