好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

SpringCloud Zuul的使用简介

简介

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测试数据 #别名

开启支持

?

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测试数据

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

?

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/服务 来访问

补充: 为什么在配置文件中是使用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的使用的资料请关注其它相关文章!

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

查看更多关于SpringCloud Zuul的使用简介的详细内容...

  阅读:17次