SpringCloud(五)Zuul也就是网关(一个可用的demo)

SpringCloud-zuul

此demo源码https://github.com/fzy657300/SpringCloud-demo-all

!!!很多人的一般配置中心在Zuul前,但是我在写配置中心的demo时遇到一些问题,所以先写这个了(已经觉得过不去了时不能停留太久),请有配置中心的demo的大哥哥小姐姐们帮助一下我。

zuul暂时没有实际用到,不能形象的解释,凭感觉说一下
在这里插入图片描述

大概就是这个意思,但不然不止这样用的
实际在项目中接口不是直接对外开放的,而是对zuul开放的,而Zuul是对外开放的而且具体访问谁由zuul分配

为了方便等会测试我新增了一个工程demo-pcenter

商品中心,专门提过处理商品相关的服务
和ucneter一样的创建
我在ucenter和pcenter中都添加了一个CommonConterller
都只有一个add方法用来测试

使用Zuul需要新建一个工程 demo-zuul zull也是需要注册的

添加依赖

zuul也需要注册到eureka,这样他才能知道有哪些服务
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency>
--

启动类添加注解

@SpringBootApplication
@EnableZuulProxy
@EnableEurekaClient
@EnableDiscoveryClient
--

yml配置文件

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:9990/eureka/
server:
  port: 9995
spring:
  application:
    name: demo-zuul
zuul:
  routes:
    api-a:
      path: /api-ucenter/** 
      serviceId: demo-ucenter #所有http://localhost:9995/api-ucente/**的请求都会指向用户中心
    api-b:
      path: /api-pcenter/**
      serviceId: demo-pcenter #所有http://localhost:9995/api-pcenter/**的请求都会指向商品中心

在这里插入图片描述

启动eureka,ucenter,pcenter,zull
在这里插入图片描述

访问http://localhost:9995/api-ucenter/add?name=123

返回Holle 123 ucenter-server

访问http://localhost:9995/api-pcenter/add?name=123

返回Holle 123 pcenter-server

也就是以后的访问不用是不同的端口号,都是访问9995(Zuul的端口)

使用Zuul的滤器

自定义一个Filter 集成抽象类ZuulFilter 实现抽象方法
@Component
public class MyFilter extends ZuulFilter {

    //此方法返回不同的字符串代表了此过滤器的执行时机
    /**
     * pre:路由之前
     * routing:路由之时
     * post: 路由之后
     * error:发送错误调用
     * 此处模拟一个访问控制,所以在路由之前 返回pre
     * */
    public String filterType() {
        return "pre";
    }

    //过滤的顺序数字越大优先级越低
    public int filterOrder() {
        return 0;
    }

    //是否过滤 true为过滤 false不过滤
    public boolean shouldFilter() {
        return true;
    }


    //具体过滤内容如一些权限判断
    public Object run() throws ZuulException {
        RequestContext currentContext = RequestContext.getCurrentContext();
        HttpServletRequest request = currentContext.getRequest();
        String name = request.getParameter("name");
        if(!"zhangsan".equals(name)){
            try {
                //注意需要设置这两项
                //不然前台不会打印name error
                //会以为是500
                currentContext.setSendZuulResponse(false);
                currentContext.setResponseStatusCode(403);
                currentContext.getResponse().getWriter().write("name error");
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }

        return null;
    }

}

重启

再次访问http://localhost:9995/api-pcenter/add?name=123

返回name error

请求访问http://localhost:9995/api-pcenter/add?name=zhangsan

返回Holle zhangsan ucenter-server

SpringCloud-Zuul 的demo完成

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值