【Spring Cloud学习之四】Zuul网关
环境
eclipse 4.7
jdk 1.8
Spring Boot 1.5.2
Spring Cloud 1.2
一、接口网关
接口网关:拦截所有的请求,交由接口网关,然后接口网关进行转发,类似nginx反向代理。
二、Zuul
Zuul的主要功能是路由转发和过滤器。路由功能是微服务的一部分,比如/api/user转发到到user服务,/api/shop转发到到shop服务, 类似于nginx转发。
zuul默认和Ribbon结合实现了负载均衡的功能。
三、搭建zuul网关工程(eureka client)
1、新建maven工程:service-zuul
2、pom.xml
- <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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0</modelVersion>
- <groupId>com.wjy</groupId>
- <artifactId>service-zuul</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- <parent>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-parent</artifactId>
- <version>1.5.2.RELEASE</version>
- <relativePath /> <!-- lookup parent from repository -->
- </parent>
- <properties>
- <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
- <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
- <java.version>1.8</java.version>
- </properties>
- <dependencies>
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-eureka</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-zuul</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>
- </dependencies>
- <dependencyManagement>
- <dependencies>
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-dependencies</artifactId>
- <version>Dalston.RC1</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>
- <repositories>
- <repository>
- <id>spring-milestones</id>
- <name>Spring Milestones</name>
- <url>https://repo.spring.io/milestone</url>
- <snapshots>
- <enabled>false</enabled>
- </snapshots>
- </repository>
- </repositories>
- </project>
3、application.yml
- eureka:
- client:
- serviceUrl:
- defaultZone: http://localhost:8888/eureka/
- server:
- port: 8769
- spring:
- application:
- name: service-zuul
- zuul:
- routes:
- api-a:
- path: /api-member/**
- service-id: service-member
- api-b:
- path: /api-order/**
- service-id: service-order
4、路由转发
启动类需要加@EnableZuulProxy 开启路由转发功能
- package com.wjy;
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
- import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
- import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
- @SpringBootApplication
- @EnableEurekaClient
- @EnableZuulProxy
- public class ZuulApp {
- public static void main(String[] args) {
- SpringApplication.run(ZuulApp.class, args);
- }
- }
修改MemberController:
- package com.wjy.controller;
- import java.util.ArrayList;
- import java.util.List;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RestController;
- @RestController
- public class MemberController {
- @Value("${server.port}")
- private String serverPort;
- @RequestMapping("/getUserList")
- public List<String> getUserList() {
- List<String> listUser = new ArrayList<String>();
- listUser.add("zhangsan");
- listUser.add("lisi");
- listUser.add("wjy");
- listUser.add("端口号:"+serverPort);
- return listUser;
- }
- @RequestMapping("/getMemberServiceApi")
- public String getMemberServiceApi() {
- return "this is 会员 服务工程";
- }
- }
修改OrderController:
- package com.wjy.controller;
- import java.util.List;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RestController;
- import com.wjy.service.OrderMemberService;
- @RestController
- public class OrderController {
- @Autowired
- private OrderMemberService orderMemberService;
- @RequestMapping("/getOrderUserAll")
- public List<String> getOrderUserAll() {
- System.out.println("订单服务开始调用会员服务");
- return orderMemberService.getOrderUserAll();
- }
- @RequestMapping("/getOrderServiceApi")
- public String getOrderServiceApi() {
- return "this is 订单服务工程";
- }
- }
测试验证:
发送请求http://127.0.0.1:8769/api-member/getMemberServiceApi转发到http://127.0.0.1:8762/getMemberServiceApi
发送请求http://127.0.0.1:8769/api-order/getOrderServiceApi转发到http://127.0.0.1:8764/getOrderServiceApi
6、服务过滤类:
- package com.wjy;
- import javax.servlet.http.HttpServletRequest;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springframework.stereotype.Component;
- import com.netflix.zuul.ZuulFilter;
- import com.netflix.zuul.context.RequestContext;
- @Component
- public class MyFilter extends ZuulFilter {
- private static Logger log = LoggerFactory.getLogger(MyFilter.class);
- @Override
- public String filterType() {
- return "pre";
- }
- @Override
- public int filterOrder() {
- return 0;
- }
- public boolean shouldFilter() {
- return true;
- }
- public Object run() {
- RequestContext ctx = RequestContext.getCurrentContext();
- HttpServletRequest request = ctx.getRequest();
- log.info(String.format("%s >>> %s", request.getMethod(), request.getRequestURL().toString()));
- Object accessToken = request.getParameter("token");
- String refer = request.getHeader("refer");
- if (accessToken != null) {
- return null;
- }
- log.warn("token is empty");
- ctx.setSendZuulResponse(false);
- ctx.setResponseStatusCode(401);
- try {
- ctx.getResponse().getWriter().write("token is empty");
- } catch (Exception e) {
- }
- return null;
- }
- }
测试验证:
发送请求http://127.0.0.1:8769/api-member/getUserList?token=123456789
发送请求http://127.0.0.1:8769/api-member/getUserList
【Spring Cloud学习之四】Zuul网关的更多相关文章
- spring cloud 学习(6) - zuul 微服务网关
微服务架构体系中,通常一个业务系统会有很多的微服务,比如:OrderService.ProductService.UserService...,为了让调用更简单,一般会在这些服务前端再封装一层,类似下 ...
- spring cloud 学习之路由网关(zuul)
学习自方志朋的博客 http://blog.csdn.net/forezp/article/details/69939114 在微服务架构中,需要几个基础的服务治理组件,包括服务注册与发现.服务消费. ...
- spring cloud学习笔记五 网关服务zuul
网关服务是指,客户端发送的请求不用直接访问特定的微服务接口,而且是经过网关服务的接口进行交互,网关服务再去到特定的微服务中进行调用. 网关服务的路由功能和Nginx的反向代理一样,所有的服务都先会 ...
- Spring Cloud(Dalston.SR5)--Zuul 网关-过滤器
Spring Cloud 为 HTTP 请求的各个阶段提供了多个过滤器,这些过滤器的执行顺序由各自提供的一个 int 值决定,提供的值越小则优先级越高,默认的过滤器及优先级如下: 自定义过滤器 在默认 ...
- Spring Cloud(Dalston.SR5)--Zuul 网关-路由配置
Spring Cloud 在 Zuul 的 routing 阶段实现了几个过滤器,这些过滤器决定如何进行路由工作. 简单路由(SimpleHostRoutingFilter) 该过滤器运行后,会将 H ...
- Spring Cloud(Dalston.SR5)--Zuul 网关-微服务集群
通过 url 映射的方式来实现 zuul 的转发有局限性,比如每增加一个服务就需要配置一条内容,另外后端的服务如果是动态来提供,就不能采用这种方案来配置了.实际上在实现微服务架构时,服务名与服务实例地 ...
- Spring Cloud(Dalston.SR5)--Zuul 网关
我们使用 Spring Cloud Netflix 中的 Eureka 实现了服务注册中心以及服务注册与发现:而服务间通过 Ribbon 或 Feign 实现服务的消费以及均衡负载:使用Hystrix ...
- Spring Cloud 学习 (五) Zuul
Zuul 作为路由网关组件,在微服务架构中有着非常重要的作用,主要体现在以下 6 个方面: Zuul, Ribbon 以及 Eureka 相结合,可以实现智能路由和负载均衡的功能,Zuul 能够将请求 ...
- Spring Cloud(Dalston.SR5)--Zuul 网关-Hystrix 回退
当我们对网关进行配置让其调用集群的服务时,将会执行 Ribbon 路由过滤器,该过滤器在进行转发时会封装为一个 Hystrix 命令予以执行,Hystrix 命令具有容错的功能,如果"源服务 ...
随机推荐
- Elasticsearch 待办
日期格式:yyyy-MM-dd,改为 yyyy-MM-dd HH:mm:ss.SSS:实体类路径:https://github.com/cag2050/spring_boot_elasticsearc ...
- CodeChef Tree Palindromes
Tree Palindromes Given a tree rooted at node 1 with N nodes, each is assigned a lower case latin cha ...
- 用mysql实现类似于oracle dblink的功能
用mysql实现类似于oracle dblink的功能 首先看看有没有federated 引擎. mysql> show engines; +------------+----------+ ...
- 【每天学一点Linux】centos7 docker 启动cpu100% 飙升居高不下 无法关机 无法杀死进程
目前不知道什么原因. 重装了docker后仍然不行.安装方式为yum(在线和本地方式). 后来使用了下载static压缩包的方式来使用,就没有再出现如题的问题了.安装包地址为:https://down ...
- Python 鼠标键盘操作
1.鼠标操作 from pymouse import PyMouse myMouse = PyMouse() #获取当前的鼠标位置 # nowP = myMouse.position() # prin ...
- LeetCode 689. Maximum Sum of 3 Non-Overlapping Subarrays
原题链接在这里:https://leetcode.com/problems/maximum-sum-of-3-non-overlapping-subarrays/ 题目: In a given arr ...
- ssh配置连接远程主机 彻底解放你的双手
查看ssh支持配置 man ssh_config 打开ssh并配置 vi ~/.ssh/config 基本配置示例说明 密钥文件连接 Host <别名> Port <机器端口号> ...
- Ubuntu 14.04 安装python3.7
下载: https://www.python.org/ftp/python/3.7.4/ .tgz文件,解压后,进入该文件夹 编译./configuremakesudo make install 当 ...
- NULL与nullptr
[https://blog.csdn.net/weixin_40237626/article/details/82560012] 其实啊,在编译器进行解释程序时,NULL会被直接解释成0,所以这里的参 ...
- [技术博客]React-Native中的组件加载、卸载与setState问题
React-Native中的组件加载.卸载与setState问题. Warning: Can only update a mounted or mounting component. This usu ...