SpringCloud Zuul的使用简介

2025-05-29 0 81

简介

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

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

使用

导入依赖

?

1

2

3

4

5

6

7

8

9

10

11
<!--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>

配置文件

?

1

2

3

4

5

6

7

8

9

10

11

12
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 #别名

开启支持

?

1

2

3

4

5

6

7
@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

SpringCloud Zuul的使用简介

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

?

1

2

3

4

5

6
zuul:

routes:

xxx.serviceId: provider-name # xxx代表任意名称

xxx.path: /mydept/**

ignored-services: provider-name # 不再通过这个路径访问即不允许通过服务名直接访问

prefix: /lin # 访问路径必须加上前缀/lin

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

SpringCloud Zuul的使用简介

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

?

1

2

3
public void setRoutes(Map<String, ZuulRoute> routes) {

this.routes = routes;

}

而在ZuulProperties中

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14
/**

* 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的使用简介的详细内容,更多关于SpringCloud Zuul的使用的资料请关注快网idc其它相关文章!

原文链接:https://juejin.cn/post/6951321316200808479

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 SpringCloud Zuul的使用简介 https://www.kuaiidc.com/106812.html

相关文章

发表评论
暂无评论