温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

SpringCloud微服务架构中Zuul网关怎么使用

发布时间:2022-02-24 17:45:21 来源:亿速云 阅读:145 作者:iii 栏目:开发技术

这篇“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;
      }

以上就是关于“SpringCloud微服务架构中Zuul网关怎么使用”这篇文章的内容,相信大家都有了一定的了解,希望小编分享的内容对大家有帮助,若想了解更多相关的知识内容,请关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI