SpringCloud系列十二:Zuul

Zuul包含了对请求的路由和过滤两个最主要的功能:
 其中路由功能负责将外部请求转发到具体的微服务实例上,是实现外部访问统一入口的基础而过滤器功能则负责对请求的处理过程进行干预,是实现请求校验、服务聚合等功能的基础。
 Zuul和Eureka进行整合,将Zuul自身注册为Eureka服务治理下的应用,同时从Eureka中获得其他微服务的消息,也即以后的访问微服务都是通过Zuul跳转后获得。
1、新建microservicecloud-zuul-gateway-9527工程
2、引入依赖

<dependencies>
    <dependency>
   	    <groupId>com.atguigu.springcloud</groupId>
        <artifactId>microservicecloud-api</artifactId>
        <version>${project.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jetty</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>
    </dependency>
    <!-- 热部署插件 -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>springloaded</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
    </dependency>
    <!-- eureka注册中心-->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-eureka</artifactId>
    </dependency>
    <!-- config服务配置中心 -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-config</artifactId>
    </dependency>
    <!--hystrix服务熔断-->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-hystrix</artifactId>
    </dependency>
    <!-- hystrix-dashboard相关: actuator监控 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <!-- zuul路由网关 -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-zuul</artifactId>
    </dependency>
</dependencies>

3、yml配置

server:
  port: 9527

spring:
  application:
    name: microservicecloud-zuul-gateway

eureka:
  client:
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka,http://eureka7003.com:7003/eureka
  instance:
    instance-id: gateway-9527.com
    prefer-ip-address: true

info:
  app.name: atguigu-microcloud
  company.name: www.atguigu.com
  build.artifactId: ${project.artifactId}
  build.version: ${project.version}

zuul:
  prefix: /atguigu     #设置统一公共前缀
  ignored-services: "*"    #禁止使用服务名访问
  routes:     #路由映射
    mydept.serviceId: microservicecloud-dept
    mydept.path: /mydept/**

4、找到C:\Windows\System32\drivers\etc路径下的hosts文件。添加域名映射

127.0.0.1  myzuul.com

5、在主启动类上添加@EnableZuulProxy注解

/**
 * Zuul包含了对请求的路由和过滤两个最主要的功能:
 *   1、路由功能负责将外部请求转发到具体的微服务实例上,是实现外部访问统一入口的基础。
 *   2、过滤功能负责对请求的处理过程进行干预,是实现请求校验、服务聚合等功能的基础。
 * Zuul和Eureka进行整合,将Zuul自身注册为Eureka服务治理下的应用,同时从Eureka中获取其他微服务的消息,以后访问微服务都是通过Zuul跳转后获得。
 */
@SpringBootApplication
@EnableZuulProxy   //开启Zuul路由网关
public class ZuulGateWay9527_App {
    public static void main(String[] args) {
        SpringApplication.run(ZuulGateWay9527_App.class, args);
    }
}

6、先启动3个eureka集群后,启动部门提供者8001,再启动zuul-gateway工程。
http://myzuul.com:9527/atguigu/mydept/dept/get/2
在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值