SpringCloud Zuul的使用 SpringCloud Zuul的使用简介

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

SpringCloud Zuul的使用 SpringCloud Zuul的使用简介

遇见_line   2021-04-16 我要评论
想了解SpringCloud Zuul的使用简介的相关内容吗,遇见_line在本文为您仔细讲解SpringCloud Zuul的使用的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:SpringCloud,Zuul,SpringCloud,Zuul使用,下面大家一起来学习吧。

简介

  • Zuul是Spring Cloud全家桶中的微服务API网关。 所有从设备或网站来的请求都会经过Zuul到达后端的Netflix应用程序
  • Zuul 主要提供路由(请求转发)和过滤
  • Zuul 最终会注入Eureka

提供: 代理,过滤和路由三大功能

使用

导入依赖

    <!--zuul组件、zuul需要注册至eureka中-->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-zuul</artifactId>
        <version>1.4.6.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        <version>2.0.2.RELEASE</version>
    </dependency>

配置文件

server:
  port: 9527
spring:
  application:
    name: springcloud-zuul
eureka:
  client:
    service-url:
      defaultZone: http://eureka1:7001/eureka/,http://eureka2:7002/eureka/,http://eureka3:7003/eureka/

  instance:
    instance-id: zull9527.com #别名

开启支持

@SpringBootApplication
@EnableZuulProxy//开启zuul支持,默认注册到Eureka
public class Zuul9527Application {
    public static void main(String[] args) {
        SpringApplication.run(Zuul9527Application.class,args);
    }
}

此时我们可以通过 地址:端口号/服务名称/服务 来访问了

注意:此处需在host文件添加 127.0.01 www,zuultest.com

为了不使我们的服务名称暴露我们可以在配置文件中添加

zuul:
  routes:
    xxx.serviceId: provider-name # xxx代表任意名称
    xxx.path: /mydept/** 
  ignored-services: provider-name # 不再通过这个路径访问即不允许通过服务名直接访问
  prefix: /lin # 访问路径必须加上前缀/lin

此时只有通过 地址:端口号/lin/mydept/服务 来访问

补充: 为什么在配置文件中是使用serviceId和path 在zull中route是以键值对的形式存放的

public void setRoutes(Map<String, ZuulRoute> routes) {
		this.routes = routes;
	}

而在ZuulProperties中

/**
 * Represents a Zuul route.
 */
public static class ZuulRoute {
        /**
         * The path (pattern) for the route, e.g. /foo/**.
         */
        private String path;
        /**
	* The service ID (if any) to map to this route. You can specify a physical URL or
        * a service, but not both.
	*/
         private String serviceId;
      }

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们