1、创建eureka-server注册中心工程,配置跟之前讲eureka文章中一样,这里不再赘述

1.1、端口8888

2、创建一个demo-client工程

2.1、demo-client启动类跟之前一样,其配置文件也一样,没有做太多配置,这里不再赘述,端口:7070,服务名:client-a。

2.2、编写一个测试接口:

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController; @RestController
public class TestController { @GetMapping("/add")
public Integer add(Integer a, Integer b) {
return a + b;
}
}

3、创建一个zuul-gateway工程

3.1、pom配置:

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
</parent>
<!-- 管理依赖 -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement> <dependencies>
     <!-- springboot web -->
     <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-web</artifactId>
     </dependency>
<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-client</artifactId>
</dependency>

</dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

3.2、工程启动类:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy; @SpringBootApplication
@EnableDiscoveryClient
@EnableZuulProxy
public class ZuulServerApplication { public static void main(String[] args) {
SpringApplication.run(ZuulServerApplication.class, args);
}
}

3.3、配置文件:

spring:
application:
name: zuul-gateway
server:
port: 5555 #指定网关服务端口
eureka:
client:
serviceUrl:
defaultZone: http://${eureka.host:127.0.0.1}:${eureka.port:8888}/eureka/ #指定注册中心
instance:
prefer-ip-address: true # 将所有/client开头的URL映射到client-a这个服务中
zuul:
routes:
client-a:
path: /client/**
serviceId: client-a

3、启动三个工程:eureka-server、zuul-gateway、demo-client

1、直接访问服务接口:localhost:7070/add?a=100&b=1

2、通过zuul网关访问接口:localhost:5555/client/add?a=100&b=100

可以看到通过网关也能正常访问到demo-client服务中的接口,这是因为在网关项目配置文件中,指定了路由规则,当浏览器向网关发送请求的时候,它会去注册中心拉取服务列表,如果发现有指定的映射规则,就会按照我们配置的规则路由到对应的服务接口上。

Zuul【入门】的更多相关文章

  1. Spring Cloud 微服务笔记(七) Zuul入门

    Zuul入门 Zuul是从设备和网站到后端应用程序所有请求的前门,为内部服务提供可配置的对外URL到服务的 映射关系,基于JVM的后端路由器.其具备一下功能: 1)认证与授权 2)压力控制 3)金丝雀 ...

  2. zuul入门(5)zuul 处理异常

    Object accessToken = request.getParameter("accessToken"); if(accessToken==null) { // 设置zuu ...

  3. zuul入门(4)zuul的注解@EnableZuulServer和@EnableZuulProxy

    @EnableZuulServer.@EnableZuulProxy两个注解 @EnableZuulProxy简单理解为@EnableZuulServer的增强版,当Zuul与Eureka.Ribbo ...

  4. zuul入门(2)zuul的过滤器分类和加载

    一.Groovy编写的Filter 1.可以放到指定目录加载 创建一个pre类型的filter,在run方法中获取HttpServletRequest 然后答应header信息 在代码中加入groov ...

  5. zuul入门(3)开发zuul的过滤器

    1.编写Zuul过滤器(Java&Groovy) 理解过滤器类型和请求生命周期后,我们来编写一个Zuul过滤器.编写Zuul的过滤器非常简单,我们只需继承抽象类ZuulFilter,然后实现几 ...

  6. zuul入门(1)zuul 的概念和原理

    一.zuul是什么 zuul 是netflix开源的一个API Gateway 服务器, 本质上是一个web servlet应用. Zuul 在云平台上提供动态路由,监控,弹性,安全等边缘服务的框架. ...

  7. 第二章 微服务网关基础组件 - zuul入门

    一.zuul简介 1.作用 zuul使用一系列的filter实现以下功能 认证和安全 - 对每一个resource进行身份认证 追踪和监控 - 实时观察后端微服务的TPS.响应时间,失败数量等准确的信 ...

  8. SpringCloud入门(七): Zuul 简介与使用

    Zuul 简介 Zuul 微服务网关是为Spring Cloud Netflix提供动态路由,监控,弹性,安全等服务的框架.可以和Eureka.Ribbon.Hystrix等组件配合使用. Zuul ...

  9. 对于zuul服务网关框架资料整理

    本次博客只是整理了一些 看过的博客.源码等 zuul入门(1)zuul 的概念和原理 https://www.cnblogs.com/lexiaofei/p/7080257.html 深入理解Zuul ...

  10. 八、springcloud之服务网关zuul(一)

    一.Zuul简介 zuul 是netflix开源的一个API Gateway 服务器, 本质上是一个web servlet应用. Zuul是Netflix出品的一个基于JVM路由和服务端的负载均衡器. ...

随机推荐

  1. Java 基础:单例模式 Singleton Pattern

    1.简介 单例模式(Singleton Pattern)是 Java 中最简单的设计模式之一.这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式. 这种模式涉及到一个单一的类,该类负责创 ...

  2. arts打卡13周

    算法: 报数序列是一个整数序列,按照其中的整数的顺序进行报数,得到下一个数.其前五项如下: 1. 12. 113. 214. 12115. 1112211 被读作  "one 1" ...

  3. spark_API

    1.概述 总的来讲,每一个spark驱动程序应用都由一个驱动程序组成,该驱动程序包含一个由用户编写的main方法,该方法会在集群上执行一些并行计算操作.Spark最重要的一个概念是弹性分布式数据集,简 ...

  4. #C++初学记录(算法测试2019/5/5)(深度搜索)

    深度搜索:Oil Deposits GeoSurvComp地质调查公司负责探测地下石油储藏. GeoSurvComp现在在一块矩形区域探测石油,并把这个大区域分成了很多小块.他们通过专业设备,来分析每 ...

  5. 如何登陆Tomcat的控制台

    当我们成功安装启动Tomcat服务后,在浏览器输入http://localhost:8080(8080是Tomcat的默认端口,可自行修改)回车 右上角可以看到三个控制台:Server Status. ...

  6. Android Mboot mmc命令介绍

    mmc command.         目前Mboot支持以下mmc命令: 1) mmc read/write.    读写命令.Addr = 内存地址, blk# = 起始block数, size ...

  7. C#文件或文件夹压缩和解压

    C#文件或文件夹压缩和解压方法有很多,本文通过使用ICSharpCode.SharpZipLib.dll来进行压缩解压 1.新建一个winform项目,选择项目右键 管理NuGet程序包,搜索ICSh ...

  8. lintcode 394. Coins in a Line 、leetcode 292. Nim Game 、lintcode 395. Coins in a Line II

    变型:如果是最后拿走所有石子那个人输,则f[0] = true 394. Coins in a Line dp[n]表示n个石子,先手的人,是必胜还是必输.拿1个石子,2个石子之后都是必胜,则当前必败 ...

  9. 【转载】 clusterdata-2011-2 谷歌集群数据分析(一)

    原文地址: https://blog.csdn.net/yangss123/article/details/78298679 由于原文声明其原创文章不得允许不可转载,故这里没有转载其正文内容. --- ...

  10. 报错:MetaException(message:Version information not found in metastore. )

    报错背景: CDH安装完成hive后启动失败. 报错现象: [main]: Metastore Thrift Server threw an exception... MetaException(me ...