springcloud通过zuul进行服务路由

zuul 是netflix开源的一个API Gateway 服务器, 本质上是一个web servlet应用。

Zuul 在云平台上提供动态路由,监控,弹性,安全等边缘服务的框架。Zuul 相当于是设备和 Netflix 流应用的 Web 网站后端所有请求的前门。

下面介绍如何在springcloud项目中使用zuul进行服务路由
1.pom.xml中引入zuul与eureka依赖

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-zuul</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>

2.在application.yml文件中配置服务发现中心与服务路由配置(这里直接使用了与eureka集成的配置没有进行传统路由的配置)

spring:
  application:
    name: gateway


#zuul与eureka整合
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:50001/eureka/

#配置hello-service与feign-consumer服务的路由
zuul:
  routes:
    hello-service:
      serviceId: hello-service
    feign-consumer:
      serviceId: feign-consumer

server:
  port: 5555

3.在主类中开启zuul路由服务

package com.cfh.gateway;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;

@SpringBootApplication
@EnableZuulProxy//开启Zuul路由
public class GatewayApplication {

    public static void main(String[] args) {
        SpringApplication.run(GatewayApplication.class, args);
    }
}

在浏览器访问zuul gateway服务(http://localhost:5555/路由名称/接口)。zuul会根据服务注册中心提供的详细路由提供服务的实例。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值