[Spring cloud 一步步实现广告系统] 17. 根据流量类型查询广告

简介: 广告检索服务功能介绍媒体方(手机APP打开的展示广告,走在路上看到的大屏幕广告等等)请求数据对象实现从上图我们可以看出,在媒体方向我们的广告检索系统发起请求的时候,请求中会有很多的请求参数信息,他们分为了三个部分,我们来编码封装这几个参数对象信息以及我们请求本身的信息。

广告检索服务

功能介绍

检索功能

媒体方(手机APP打开的展示广告,走在路上看到的大屏幕广告等等)

请求数据对象实现

从上图我们可以看出,在媒体方向我们的广告检索系统发起请求的时候,请求中会有很多的请求参数信息,他们分为了三个部分,我们来编码封装这几个参数对象信息以及我们请求本身的信息。Let's code.

  • 创建广告检索请求接口

/**
 * ISearch for 请求接口,
 * 根据广告请求对象,获取广告响应信息
 *
 * @author <a href="mailto:magicianisaac@gmail.com">Isaac.Zhang | 若初</a>
 */
@FunctionalInterface
public interface ISearch {

    /**
     * 根据请求返回广告结果
     */
    SearchResponse fetchAds(SearchRequest request);
}
  • 创建SearchRequest,包含三部分:mediaId,RequestInfo,FeatureInfo
@Data
@NoArgsConstructor
@AllArgsConstructor
public class SearchRequest {

    //媒体方请求标示
    private String mediaId;
    //请求基本信息
    private RequestInfo requestInfo;
    //匹配信息
    private FeatureInfo featureInfo;


    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public static class RequestInfo {
        private String requestId;

        private List<AdSlot> adSlots;
        private App app;
        private Geo geo;
        private Device device;
    }

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public static class FeatureInfo {

        private KeywordFeature keywordFeature;
        private DistrictFeature districtFeature;
        private HobbyFeatrue hobbyFeatrue;

        private FeatureRelation relation = FeatureRelation.AND;
    }
}

其他的对象大家可以去github传送门 & gitee传送门 下载源码。

UTOOLS1565403569539.png

检索响应对象定义
/**
 * SearchResponse for 检索API响应对象
 *
 * @author <a href="mailto:magicianisaac@gmail.com">Isaac.Zhang | 若初</a>
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class SearchResponse {

    //一个广告位,可以展示多个广告
    //Map key为广告位 AdSlot#adSlotCode
    public Map<String, List<Creative>> adSlotRelationAds = new HashMap<>();

    @Data
    @Builder
    @NoArgsConstructor
    @AllArgsConstructor
    public static class Creative {

        private Long adId;
        private String adUrl;
        private Integer width;
        private Integer height;
        private Integer type;
        private Integer materialType;

        //展示监控url
        private List<String> showMonitorUrl = Arrays.asList("www.life-runner.com", "www.babydy.cn");
        //点击监控url
        private List<String> clickMonitorUrl = Arrays.asList("www.life-runner.com", "www.babydy.cn");
    }

    /**
     * 我们的检索服务针对的是内存中的索引检索,那么我们就需要一个转换方法
     */
    public static Creative convert(CreativeIndexObject object) {
        
        return Creative.builder()
                       .adId(object.getAdId())
                       .adUrl(object.getAdUrl())
                       .width(object.getWidth())
                       .height(object.getHeight())
                       .type(object.getType())
                       .materialType(object.getMaterialType())
                       .build();
    }
}
根据流量类型广告过滤

流量类型本身属于推广单元下的类目,有很多种类贴片广告,开屏广告等等,这些类型需要同步到媒体方,媒体方会根据不同的流量类型发起不同的广告请求,我们需要先定义一个流量类型的信息类。

public class AdUnitConstants {
    public static class PositionType{
        //App启动时展示的、展示时间短暂的全屏化广告形式。
        private static final int KAIPING = 1;
        //电影开始之前的广告
        private static final int TIEPIAN = 2;
        //电影播放中途广告
        private static final int TIEPIAN_MIDDLE = 4;
        //暂停视频时候播放的广告
        private static final int TIEPIAN_PAUSE = 8;
        //视频播放完
        private static final int TIEPIAN_POST = 16;
    }
}

从上述类型的数字,我们可以看出是2的倍数,这是为了使用位运算提升性能。

com.sxzhongf.ad.index.adunit.AdUnitIndexObject中,我们添加类型校验方法:

public static boolean isAdSlotType(int adSlotType, int positionType) {
        switch (adSlotType) {
            case AdUnitConstants.PositionType.KAIPING:
                return isKaiPing(positionType);
            case AdUnitConstants.PositionType.TIEPIAN:
                return isTiePian(positionType);
            case AdUnitConstants.PositionType.TIEPIAN_MIDDLE:
                return isTiePianMiddle(positionType);
            case AdUnitConstants.PositionType.TIEPIAN_PAUSE:
                return isTiePianPause(positionType);
            case AdUnitConstants.PositionType.TIEPIAN_POST:
                return isTiePianPost(positionType);
            default:
                return false;
        }
    }

    /**
     * 与运算,低位取等,高位补零。
     * 如果 > 0,则为开屏
     */
    private static boolean isKaiPing(int positionType) {
        return (positionType & AdUnitConstants.PositionType.KAIPING) > 0;
    }
    private static boolean isTiePianMiddle(int positionType) {
        return (positionType & AdUnitConstants.PositionType.TIEPIAN_MIDDLE) > 0;
    }

    private static boolean isTiePianPause(int positionType) {
        return (positionType & AdUnitConstants.PositionType.TIEPIAN_PAUSE) > 0;
    }

    private static boolean isTiePianPost(int positionType) {
        return (positionType & AdUnitConstants.PositionType.TIEPIAN_POST) > 0;
    }

    private static boolean isTiePian(int positionType) {
        return (positionType & AdUnitConstants.PositionType.TIEPIAN) > 0;
    }

无所如何,我们都是需要根据positionType进行数据查询过滤,我们在之前的com.sxzhongf.ad.index.adunit.AdUnitIndexAwareImpl中添加2个方法来实现过滤:

/**
     * 过滤当前是否存在满足positionType的UnitIds
     */
    public Set<Long> match(Integer positionType) {
        Set<Long> adUnitIds = new HashSet<>();
        objectMap.forEach((k, v) -> {
            if (AdUnitIndexObject.isAdSlotType(positionType, v.getPositionType())) {
                adUnitIds.add(k);
            }
        });
        return adUnitIds;
    }

    /**
     * 根据UnitIds查询AdUnit list
     */
    public List<AdUnitIndexObject> fetch(Collection<Long> adUnitIds) {
        if (CollectionUtils.isEmpty(adUnitIds)) {
            return Collections.EMPTY_LIST;
        }
        List<AdUnitIndexObject> result = new ArrayList<>();
        adUnitIds.forEach(id -> {
            AdUnitIndexObject object = get(id);
            if (null == object) {
                log.error("AdUnitIndexObject does not found:{}", id);
                return;
            }
            result.add(object);
        });

        return result;
    }
  • 实现Search服务接口

上述我们准备了一系列的查询方法,都是为了根据流量类型查询广告单元信息,我们现在开始实现我们的查询接口,查询接口中,我们可以获取到媒体方的请求对象信息,它带有一系列查询所需要的过滤参数:

/**
 * SearchImpl for 实现search 服务
 *
 * @author <a href="mailto:magicianisaac@gmail.com">Isaac.Zhang | 若初</a>
 */
@Service
@Slf4j
public class SearchImpl implements ISearch {
    @Override
    public SearchResponse fetchAds(SearchRequest request) {

        //获取请求广告位信息
        List<AdSlot> adSlotList = request.getRequestInfo().getAdSlots();

        //获取三个Feature信息
        KeywordFeature keywordFeature = request.getFeatureInfo().getKeywordFeature();
        HobbyFeatrue hobbyFeatrue = request.getFeatureInfo().getHobbyFeatrue();
        DistrictFeature districtFeature = request.getFeatureInfo().getDistrictFeature();
        //Feature关系
        FeatureRelation featureRelation = request.getFeatureInfo().getRelation();


        //构造响应对象
        SearchResponse response = new SearchResponse();
        Map<String, List<SearchResponse.Creative>> adSlotRelationAds = response.getAdSlotRelationAds();

        for (AdSlot adSlot : adSlotList) {
            Set<Long> targetUnitIdSet;
            //根据流量类型从缓存中获取 初始 广告信息
            Set<Long> adUnitIdSet = IndexDataTableUtils.of(
                    AdUnitIndexAwareImpl.class
            ).match(adSlot.getPositionType());
        }
        return null;
    }
}
目录
相关文章
|
3天前
|
传感器 人工智能 前端开发
JAVA语言VUE2+Spring boot+MySQL开发的智慧校园系统源码(电子班牌可人脸识别)Saas 模式
智慧校园电子班牌,坐落于班级的门口,适合于各类型学校的场景应用,班级学校日常内容更新可由班级自行管理,也可由学校统一管理。让我们一起看看,电子班牌有哪些功能呢?
43 4
JAVA语言VUE2+Spring boot+MySQL开发的智慧校园系统源码(电子班牌可人脸识别)Saas 模式
|
4天前
|
消息中间件 负载均衡 Java
【Spring Cloud 初探幽】
【Spring Cloud 初探幽】
13 1
|
6天前
|
Java 开发者 微服务
Spring Cloud原理详解
【5月更文挑战第4天】Spring Cloud是Spring生态系统中的微服务框架,包含配置管理、服务发现、断路器、API网关等工具,简化分布式系统开发。核心组件如Eureka(服务发现)、Config Server(配置中心)、Ribbon(负载均衡)、Hystrix(断路器)、Zuul(API网关)等。本文讨论了Spring Cloud的基本概念、核心组件、常见问题及解决策略,并提供代码示例,帮助开发者更好地理解和实践微服务架构。此外,还涵盖了服务通信方式、安全性、性能优化、自动化部署、服务网格和无服务器架构的融合等话题,揭示了微服务架构的未来趋势。
31 6
|
10天前
|
JSON Java Apache
Spring Cloud Feign 使用Apache的HTTP Client替换Feign原生httpclient
Spring Cloud Feign 使用Apache的HTTP Client替换Feign原生httpclient
|
10天前
|
负载均衡 Java 开发者
Spring Cloud:一文读懂其原理与架构
Spring Cloud 是一套微服务解决方案,它整合了Netflix公司的多个开源框架,简化了分布式系统开发。Spring Cloud 提供了服务注册与发现、配置中心、消息总线、负载均衡、熔断机制等工具,让开发者可以快速地构建一些常见的微服务架构。
|
12天前
|
消息中间件 Java RocketMQ
Spring Cloud RocketMQ:构建可靠消息驱动的微服务架构
【4月更文挑战第28天】消息队列在微服务架构中扮演着至关重要的角色,能够实现服务之间的解耦、异步通信以及数据分发。Spring Cloud RocketMQ作为Apache RocketMQ的Spring Cloud集成,为微服务架构提供了可靠的消息传输机制。
27 1
|
12天前
|
Dubbo Java 应用服务中间件
Spring Cloud Dubbo: 微服务通信的高效解决方案
【4月更文挑战第28天】在微服务架构的发展中,服务间的高效通信至关重要。Spring Cloud Dubbo 提供了一种基于 RPC 的通信方式,使得服务间的调用就像本地方法调用一样简单。本篇博客将探讨 Spring Cloud Dubbo 的核心概念,并通过具体实例展示其在项目中的实战应用。
15 2
|
12天前
|
监控 Java Sentinel
Spring Cloud Sentinel:概念与实战应用
【4月更文挑战第28天】在分布式微服务架构中,确保系统的稳定性和可靠性至关重要。Spring Cloud Sentinel 为微服务提供流量控制、熔断降级和系统负载保护,有效预防服务雪崩。本篇博客深入探讨 Spring Cloud Sentinel 的核心概念,并通过实际案例展示其在项目中的应用。
23 0
|
12天前
|
Cloud Native Java Nacos
Spring Cloud Nacos:概念与实战应用
【4月更文挑战第28天】Spring Cloud Nacos 是一个基于 Spring Cloud 构建的服务发现和配置管理工具,适用于微服务架构。Nacos 提供了动态服务发现、服务配置、服务元数据及流量管理等功能,帮助开发者构建云原生应用。
20 0