路由网关Zuul

SpringCloud视频教程:

https://ke.qq.com/course/2805647?tuin=a3e3fb1&from_uin=171851697&from=1000201007

个人博客纯净版

http://www.51ufo.cn/%E5%BE%AE%E6%9C%8D%E5%8A%A1/%E5%88%86%E5%B8%83%E5%BC%8F/2020/07/11/SpringCloud%E5%85%A5%E9%97%A810-%E8%B7%AF%E7%94%B1%E7%BD%91%E5%85%B3Zuul.html

 

本文代码git地址 https://gitee.com/xmingtx/springcloud-lesson.git

 

Zuul简介

Zuul的主要功能是路由转发和过滤器。路由功能是微服务的一部分,比如/api/user转发到到user服务,/api/shop转发到到shop服务。zuul默认和Ribbon结合实现了负载均衡的功能。

准备工作

首先在父模块下,创建一个名称为service-zuul,pom.xml文件配置如下:

<parent>
    <artifactId>springcloud-lesson</artifactId>
    <groupId>com.mk</groupId>
    <version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>service-zuul</artifactId>

<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
    <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>
</dependencies>

创建启动类ZuulApplication,并添加@EnableZuulProxy注解开启zuul网关的功能,如下:

@SpringBootApplication
@EnableZuulProxy
@EnableEurekaClient
public class ZuulApplication {

    public static void main(String[] args) {
        SpringApplication.run(ZuulApplication.class);
    }
}

新建配置文件application.yml,并添加以下的配置:

server:
  port: 8769
spring:
  application:
    name: service-zuul

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
  instance:
    prefer-ip-address: true
    instance-id: ${spring.cloud.client.ip-address}:${server.port}

zuul:
  routes:
    api-a:
      path: /api-a/**
      serviceId: consumer-ribbon
    api-b:
      path: /api-b/**
      serviceId: consumer-fegin

首先指定服务注册中心的地址为http://localhost:8761/eureka/,服务的端口为8769,服务名为service-zuul;以/api-a/ 开头的请求都转发给consumer-ribbon服务;以/api-b/开头的请求都转发给consumer-feign服务;

启动服务;打开浏览器访问:http://localhost:8769/api-a/hello?name=xiaoming;浏览器显示:

hello, xiaoming, 我是服务提供者:端口为:8080

启动服务;打开浏览器访问:http://localhost:8769/api-b/hello?name=xiaoming;浏览器显示:

通过fegin客户端调用结果为:hello, xiaoming, 我是服务提供者:端口为:8080

这说明zuul的路由转发功能生效了

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值