栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

微服务SpringCloud(zuul路由网关)整合十六

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

微服务SpringCloud(zuul路由网关)整合十六

我们之前学习完了Hystrix组件,今天我们学习一个新的组件zuul。为什么要学习zuul这个组件呢?还记得之前我们在案例里面有多个微服务吗?每个微服务都有自己的ip地址、端口号。对于我们开发人员来说这些还没什么,但是我们一些微服务是需要和前端同学进行对接的,我们不可能把所有的微服务地址给前端吧,一来这些地址太多了前端同学对接起来也很麻烦,另外这样把我们微服务地址暴露出去也很不安全。所以为了解决这个问题,Netfix公司专门开发了一个zuul组件。

那什么是zuul呢?

Zuul是一种提供动态路由、监视、弹性、安全性等功能的边缘服务,它为整个API网关提供了一个统一的路口。

zuul可以做什么呢?

1、路由:负责将外部请求转发到具体的服务实例上去,是实现统一访问入口的基础
2、过滤:对请求过程进行额外的处理,是请求校验过滤及服务聚合的基础。
3、负载均衡 :网关为入口,由网关与微服务进行交互,网关会获取微服务注册中心地址,再配合一些算法选择其中一个服务地址,进行处理业务。
这个属于客户端侧的负载均衡,由调用方去实现负载均衡逻辑。
4、可以进行灰度发布(又称为金丝雀发布),先启动一个新版本应用,但是并不直接将流量切过来,而是测试人员对新版本进行线上测试,启动的这个新版本应用,就是我们的金丝雀。如果没有问题,那么可以将少量的用户流量导入到新版本上,然后再对新版本做运行状态观察,收集各种运行时数据,如果此时对新旧版本做各种数据对比,就是所谓的A/B测试。新版本没什么问题,那么逐步扩大范围、流量,把所有用户都迁移到新版本上面来。

整合zuul

新建Module pcloud-zuul-gateway8848


修改pom文件:




  4.0.0

  
    pcloud
    com.younger.springcloud
    1.0-SNAPSHOT
  

  com.younger.springcloud
  pcloud-zuul-gateway8848
  1.0-SNAPSHOT
  war

  pcloud-zuul-gateway8848 Maven Webapp

  
    
      org.springframework.cloud
      spring-cloud-starter-netflix-eureka-client
    
    
      org.springframework.cloud
      spring-cloud-starter-netflix-zuul
    

    
      org.springframework.boot
      spring-boot-starter-actuator
    
    
      org.springframework.boot
      spring-boot-devtools
      runtime
      true
    
    
      org.projectlombok
      lombok
      true
    
    
      org.springframework.boot
      spring-boot-starter-test
      test
    
  


新增application.yml

server:
  port: 8848

spring:
  application:
    name: pcloud-zuul-gateway

eureka:
  client:
    service-url:
      defaultZone: http://eureka6001:6001/eureka/

新增启动类: ZuulGateWayMain8848

package com.younger.springcloud;

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

@SpringBootApplication
@EnableZuulProxy
public class ZuulGateWayMain8848 {

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

测试zuul路由功能

修改8848 的yml 文件:

zuul:
  routes: #配置路由映射
    user.serviceId: PCLOUD-USER-SERVICE  #Eureka注册的服务名称
    user.path: /gateway
    @Override
    public boolean shouldFilter() {
        return true;
    }

    @Override
    public Object run() throws ZuulException {
        RequestContext requestContext = RequestContext.getCurrentContext();
        HttpServletRequest request = requestContext.getRequest();
        String remoteHost = request.getRemoteHost();
        int localPort = request.getLocalPort();
        String requestURI = request.getRequestURI();
        System.out.println("remoteHost:" +remoteHost+ " localPort:" +localPort +" requestURI:" +requestURI);
        return null;
    }
}

重新启动 8848 测试一下:
测试地址:
http://localhost:8848/gateway/user/1

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/759434.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号