SpringCloud学习笔记(七)之路由网关Zuul

是什么

Zuul包含了对请求路由和过滤两个最主要的功能:

其中路由功能负责将外部请求转发到具体的微服务实例上,是实现外部访问统一入口的基础。而过滤功能则负责对请求的处理过程进行干预,是实现请求校验、服务聚合等功能的基础。Zuul和Eureka进行整合,将zuul自身注册为Eureka服务治理下的应用,同时从Eureka中获得其他微服务的消息,也即以后的访问微服务都是通过zuul跳转后获得。

注意:zuul服务最终还是会注册进Eureka

提供=代理+路由+过滤三大功能

基本配置

1.  创建maven工程micoservicecloud-zuul-gateway-9527

2.  配置其pom.xml文件

1<?xml version="1.0" encoding="UTF-8"?> 2<project xmlns="http://maven.apache.org/POM/4.0.0" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 5 <parent> 6 <artifactId>micoservicecloud</artifactId> 7 <groupId>com.minn.springcloud</groupId> 8 <version>1.0-SNAPSHOT</version> 9 </parent> 10 <modelVersion>4.0.0</modelVersion> 11 12 <artifactId>micoservicecloud-zuul-gateway-9527</artifactId> 13 14 <dependencies> 15 <dependency> 16 <groupId>org.springframework.boot</groupId> 17 <artifactId>spring-boot-starter-actuator</artifactId> 18 </dependency> 19 <dependency> 20 <groupId>org.springframework.cloud</groupId> 21 <artifactId>spring-cloud-starter-hystrix</artifactId> 22 </dependency> 23 <dependency> 24 <groupId>org.springframework.cloud</groupId> 25 <artifactId>spring-cloud-starter-config</artifactId> 26 </dependency> 27 <dependency> 28 <groupId>com.minn.springcloud</groupId> 29 <artifactId>micoservicecloud-api</artifactId> 30 <version>${project.version}</version> 31 </dependency> 32 <dependency> 33 <groupId>org.springframework.cloud</groupId> 34 <artifactId>spring-cloud-starter-eureka</artifactId> 35 </dependency> 36 <dependency> 37 <groupId>org.springframework.cloud</groupId> 38 <artifactId>spring-cloud-starter-zuul</artifactId> 39 </dependency> 40 <dependency> 41 <groupId>org.springframework.boot</groupId> 42 <artifactId>spring-boot-starter-web</artifactId> 43 </dependency> 44 <dependency> 45 <groupId>org.springframework</groupId> 46 <artifactId>springloaded</artifactId> 47 </dependency> 48 <dependency> 49 <groupId>org.springframework.boot</groupId> 50 <artifactId>spring-boot-devtools</artifactId> 51 </dependency> 52 <dependency> 53 <groupId>org.springframework.boot</groupId> 54 <artifactId>spring-boot-starter-jetty</artifactId> 55 </dependency> 56 </dependencies> 57</project>

3.  配置其application.yml

1server: 2 port: 9527 3 4spring: 5 application: 6 name: micoservicecloud-zuul-gateway 7eureka: 8 client: 9 service-url: 10 defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka,http://eureka7003.com:7003/eureka 11 instance: 12 instance-id: gateway.com 13 prefer-ip-address: true 14 15info: 16 app.name: minn-microservicecloud 17 company.name: www.minn.com 18 build.artifactId: $project.artifactId$ 19 build.version: $project.version$

4. 创建启动类

1package com.minn.springcloud; 2 3import org.springframework.boot.SpringApplication; 4import org.springframework.boot.autoconfigure.SpringBootApplication; 5import org.springframework.cloud.netflix.zuul.EnableZuulProxy; 6 7@SpringBootApplication 8@EnableZuulProxy 9public class Zuul_App { 10 public static void main(String[] args) { 11 SpringApplication.run(Zuul_App.class,args); 12 } 13}

5.  启动三个eureka集群,一个服务提供类micoservicecloud-provider-dept-8001,一个路由。

6.  在本地hosts文件添加:

localhost		myzuul.com

7. 启用路由

http://myzuul.com:9527/microservicecloud-dept/dept/get/1

访问映射规则

统一更改路由访问路径:

1zuul: 2 routes: 3 mydept.serviceId: microservicecloud-dept #http://myzuul.com:9527/microservicecloud-dept/dept/get/1 4 mydept.path: /mydept/** #将上面的路径中microservicecloud-dept变为mydept 5 ignored-services: "*" #屏蔽http://myzuul.com:9527/microservicecloud-dept这个访问路径,"*" 代表多个微服务名 6 prefix: /minn #统一访问路径:http://myzuul.com:9527/minn/mydept/dept/get/1
点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

springboot2.0下的zuul路由网关初探

Zuul作为微服务系统的网关组件,用于构建边界服务,致力于动态路由、过滤、监控、弹性伸缩和安全。为什么需要ZuulZuul、Ribbon以及Eureka结合可以实现智能路由和负载均衡的功能;网关将所有服务的API接口统一聚合,统一对外暴露。外界调用API接口时,不需要知道微服务系统中各服务相互调用的复杂性,保护了内部微服务单元的API接口;网关可以做

Nepxion Discovery 5.5.0 发布

!(https://oscimg.oschina.net/oscnet/f81c043194ef4732880459d00c1a720e.png)发布日志功能更新:增加基于Opentracing调用链的支持,目前支持UberJaeger,实现在SpringCloudGateway、Zuul和服务上的灰度