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. 【opencv C++ linux】linux下编译含opencv的C++代码

    首先写一个简单的测试代码 #include <opencv2/opencv.hpp> #include <iostream> #include <string> u ...

  2. iOS开发之如何在用户删除应用后保持一些数据

    在开发过程中我们有时候在用户删除时候保存一些信息在用户下次安装应用时候使用,这个时候我们可以使用剪切版UIPasteboard的FindUIPasteboard和钥匙串keychain的使用 剪切版剪 ...

  3. 第十四周助教工作总结——NWNU李泓毅

    助教博客链接:https://www.cnblogs.com/NWNU-LHY/ 本次作业的要求:团队项目需求改进与系统设计:https://www.cnblogs.com/nwnu-daizh/p/ ...

  4. 我大概知道他在说什么了,是对内存单元的竞争访问吧。Python有GIL,在执行伪码时是原子的。但是伪码之间不保证原子性。 UDP丢包,你是不是做了盲发?没有拥塞控制的情况下,确实会出现丢包严重的情况。你先看看发送速率,还有是否带有拥塞控制。

    我大概知道他在说什么了,是对内存单元的竞争访问吧.Python有GIL,在执行伪码时是原子的.但是伪码之间不保证原子性.   UDP丢包,你是不是做了盲发?没有拥塞控制的情况下,确实会出现丢包严重的情 ...

  5. Android命名规范(重点讲解:包名)

    Android程序开发中,使用规范的命名有益于程序的开发和后期阅读.本文主要对Android程序包名的定义做详细介绍,并附带一些简单的命名规则. 一.标识符命名方法1 .小驼峰命名法,除首单词外,其余 ...

  6. mvn创建flink项目

    使用如下项目骨架创建flink项目,结果被官方的下面这个创建方式坑了.. mvn archetype:generate \ -DarchetypeGroupId=org.apache.flink \ ...

  7. Dart中的mixins

    /* mixins的中文意思是混入,就是在类中混入其他功能. 在Dart中可以使用mixins实现类似多继承的功能,with关键字 因为mixins使用的条件,随着Dart版本一直在变,这里讲的是Da ...

  8. shell编程系列4--有类型变量:字符串、只读类型、整数、数组

    shell编程系列4--有类型变量:字符串.只读类型.整数.数组 有类型变量总结: declare命令和typeset命令两者等价 declare.typeset命令都是用来定义变量类型的 decla ...

  9. mysql登录指令

    mysql -h 192.168.1.124 -u root -p -h后加mysql的ip,-u加用户名,-p会弹出输入密码

  10. 123457123457#0#-----com.tym.YuErBaiKeTYM--前拼后广--育儿百科

    com.tym.YuErBaiKeTYM--前拼后广--育儿百科