[Spring cloud 一步步实现广告系统] 4. 通用代码模块设计

一个大的系统,在代码的复用肯定是必不可少的,它能解决:

  1. 统一的响应处理(可以对外提供统一的响应对象包装)

  1. 统一的异常处理(可以将业务异常统一收集处理)

  1. 通用代码定义、配置定义(通用的配置信息放在统一的代码管理中,便于维护和更新)

创建项目mscx-ad-common

POM文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>mscx-ad</artifactId>
        <groupId>com.sxzhongf</groupId>
        <version>1.0-SNAPSHOT</version>
        <relativePath>../pom.xml</relativePath>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <packaging>jar</packaging>

    <groupId>com.sxzhongf</groupId>
    <artifactId>mscx-ad-common</artifactId>
    <version>1.0-SNAPSHOT</version>
    <name>Common-Service</name>
    <description>公共逻辑 and 帮助类</description>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- fastjson是阿里巴巴的开源JSON解析库,它可以解析JSON格式的字符串,支持将Java Bean序列化为JSON字符串,也可以从JSON字符串反序列化到JavaBean -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.58</version>
        </dependency>
        <!--  -->
        <dependency>
            <groupId>commons-codec</groupId>
            <artifactId>commons-codec</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
        </dependency>
    </dependencies>
        <!--maven编译插件-->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

项目结构

  • vo (统一响应对象package)
  • advice (bean 增强package )

    Spring支持五种类型的增强或通知(Advice)

    • Before(方法执行前) org.apringframework.aop.MethodBeforeAdvice
    • AfterReturning(方法返回后) org.springframework.aop.AfterReturningAdvice
    • After-throwing(异常抛出后) org.springframework.aop.ThrowsAdviceArround环绕,即方法前后 org.aopaliance.intercept.MethodInterceptor
      引介,不常用 org.springframework.aop.IntroductionInterceptor
      具体可参考:细说advice,advisor
  • annotation
  • config
  • exception
  • utils
  • export
    通用响应编码
  1. 创建通用返回对象
/**
* @Data是下属注解的组合注解
*
* @see Getter
* @see Setter
* @see RequiredArgsConstructor
* @see ToString
* @see EqualsAndHashCode
* @see lombok.Value
*/
@Data
@NoArgsConstructor //无参构造函数
@AllArgsConstructor //全参构造函数
public class CommonResponse<T> implements Serializable {
   private Integer code = 0;
   private String message = "success";
   /**
    * 具体的数据对象信息
    */
   private T data;

   public CommonResponse(Integer code, String message) {
       this.code = code;
       this.message = message;
   }

   public CommonResponse(T data) {
       this.data = data;
   }
}
  1. 在advice包中实现对响应的统一拦截com.sxzhongf.ad.common.advice.CommonResponseDataAdvice,参考 ResponseBodyAdvice, RestControllerAdvice 可查看源码org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice
   @RestControllerAdvice
   public class CommonResponseDataAdvice implements ResponseBodyAdvice<Object> {

       /**
        * 判断是否需要对响应进行处理
        *
        * @return false -> 不处理,true -> 处理
        */
       @Override
       public boolean supports(MethodParameter methodParameter, Class<? extends HttpMessageConverter<?>> converterType) {
   //
   //        //获取当前处理请求的controller的方法
   //        String methodName = methodParameter.getMethod().getName().toLowerCase();
   //        // 不拦截/不需要处理返回值 的方法
   //        String method = "login"; //如登录
   //        //不拦截
   //        return !method.equals(methodName);

           // 如果类上标记了@IgnoreResponseAdvice,则不拦截
           if (methodParameter.getDeclaringClass().isAnnotationPresent(IgnoreResponseAdvice.class)) {
               return false;
           }

           // 如果方法上标记了@IgnoreResponseAdvice,则不拦截
           if (methodParameter.getMethod().isAnnotationPresent(IgnoreResponseAdvice.class)) {
               return false;
           }

           //对响应进行处理,执行beforeBodyWrite方法
           return true;
       }

       /**
        * 目的 拦截CommonResponse
        *
        * @param body 原始的Controller需要返回的数据
        */
       @Override
       public Object beforeBodyWrite(Object body, MethodParameter returnType,
                                     MediaType selectedContentType,
                                     Class<? extends HttpMessageConverter<?>> selectedConverterType,
                                     ServerHttpRequest request,
                                     ServerHttpResponse response) {

           CommonResponse<Object> commonResponse = new CommonResponse<>();

           if (null == body) {
               return commonResponse;
           } else if (body instanceof CommonResponse) {
               commonResponse = (CommonResponse<Object>) body;
           } else {
               commonResponse.setData(body);
           }
           return commonResponse;
       }
   }

我们在annotation包下面添加一个注解com.sxzhongf.ad.common.annotation.IgnoreResponseAdvice,用它来标柱是否需要支持上面的统一返回拦截。

   /**
    * IgnoreResponseAdvice for 标示需要忽略拦截动作
    *
    * @author <a href="mailto:[email protected]">Isaac.Zhang</a>
    */
   //ElementType.TYPE 表示该注解可用于class
   //ElementType.METHOD 表示可用于方法
   @Target({ElementType.TYPE, ElementType.METHOD})
   @Retention(RetentionPolicy.RUNTIME)
   public @interface IgnoreResponseAdvice {
   }
通用异常处理

异常处理也是统一的,那么同样就要使用到RestControllerAdvice,同时,需要使用的Spring 的ExceptionHandler进行异常处理

  1. 创建统一异常拦截类
/**
 * GlobalExceptionAdvice for 全局统一异常拦截
 *
 * @author <a href="mailto:[email protected]">Isaac.Zhang</a>
 * @see RestControllerAdvice
 * @see ExceptionHandler
 */
@RestControllerAdvice
public class GlobalExceptionAdvice {

    /**
     * 对 {@link AdException} 进行统一处理
     * {@link ExceptionHandler}  对指定的异常进行拦截
     * 可优化:
     * 定义多种类异常,实现对应的异常处理,
     * 例如:
     * <ul>
     * <li>
     * 推广单元操作异常,抛出 AdUnitException
     * </li>
     * <li>
     * Binlog 解析异常,抛出 BinlogException
     * </li>
     * </ul>
     * 拦截Spring Exception 使用 {@link ExceptionHandler}注解
     */
    @ExceptionHandler(value = AdException.class)
    public CommonResponse<String> handlerAdException(HttpServletRequest request, AdException ex) {
        CommonResponse<String> response = new CommonResponse<>(-1, "business error");
        response.setData(ex.getMessage());
        return response;
    }
}
  1. 创建通用异常类
/**
 * AdException for 统一异常处理类
 *
 * @author <a href="mailto:[email protected]">Isaac.Zhang</a>
 */
public class AdException extends Exception {
    public AdException(String message) {
        super(message);
    }
}
通用配置信息

通过HTTP消息转换器HttpMessageConverter,实现对象转换,Java Object -> HTTP 数据流

  1. 新增WebConfiguration,我们通过实现org.springframework.web.servlet.config.annotation.WebMvcConfigurer来定制和修改Spring MVC的配置信息。
/**
 * WebConfiguration for 对Spring的配置和行为进行定制修改
 *
 * @author <a href="mailto:[email protected]">Isaac.Zhang</a>
 * @see WebMvcConfigurer
 */
@Configuration
public class WebConfiguration implements WebMvcConfigurer {
    /**
     * 匹配路由请求规则
     */
    @Override
    public void configurePathMatch(PathMatchConfigurer configurer) {

    }
    /**
     * 注册自定义的Formatter 和 Convert
     */
    @Override
    public void addFormatters(FormatterRegistry registry) {

    }
    /**
     * 添加静态资源处理器
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {

    }
    /**
     * 添加自定义视图控制器
     */
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {

    }
    /**
     * 添加自定义方法参数处理器
     */
    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {

    }
    /**
     * 配置消息转换器
     */
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        //清空所有转换器
        converters.clear();
        // Java Obj -> Json Obj (http header: application/json)
        converters.add(new MappingJackson2HttpMessageConverter());
    }
}

原文地址:https://blog.51cto.com/1917331/2424181

时间: 2024-08-29 12:52:14

[Spring cloud 一步步实现广告系统] 4. 通用代码模块设计的相关文章

[Spring cloud 一步步实现广告系统] 11. 使用Feign实现微服务调用

上一节我们使用了Ribbon(基于Http/Tcp)进行微服务的调用,Ribbon的调用比较简单,通过Ribbon组件对请求的服务进行拦截,通过Eureka Server 获取到服务实例的IP:Port,然后再去调用API.本节课我们使用更简单的方式来实现,使用声明式的Web服务客户端Feign,我们只需要使用Feign来声明接口,利用注解来进行配置就可以使用了,是不是很简单?实际工作中,我们也只会用到Feign来进行服务之间的调用(大多数).接下来,我们来实例操作一把. 为了代码的重用性,我们

[Spring cloud 一步步实现广告系统] 配置项目结构 &amp; 实现Eureka服务

父项目管理 首先,我们在创建投放系统之前,先看一下我们的工程结构: mscx-ad-sponsor就是我们的广告投放系统.如上结构,我们需要首先创建一个Parent Project mscx-ad 来编写父项目的pom,来管理我们的统一依赖信息. <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xm

[Spring cloud 一步步实现广告系统] 业务架构分析

什么是广告系统? 主要包含: 广告主投放广告的<广告投放系统> 媒体方(广告展示媒介-<地铁广告屏幕>)检索广告用的<广告检索系统> 广告计费系统(按次,曝光量等等) 报表系统 Etc. 使用技能栈 JDK1.8 MySQL 8+ Maven 3+ Spring cloud Greenwich.SR2 Eureka Zuul / gateway Feign ... Spring boot 2.1.5 Kafka 2.2.0 MySQL Binlog 项目结构 项目架构

[Spring cloud 一步步实现广告系统] 7. 中期总结回顾

在前面的过程中,我们创建了4个project: 服务发现 我们使用Eureka 作为服务发现组件,学习了Eureka Server,Eureka Client的使用. Eureka Server 加依赖 <dependency> <groupId>org.springframework.cloud</groupId> <!--<artifactId>spring-cloud-netflix-eureka-server</artifactId>

[Spring cloud 一步步实现广告系统] 21. 系统错误汇总

广告系统学习过程中问题答疑 博客园 Eureka集群启动报错 Answer 因为Eureka在集群启动过程中,会连接集群中其他的机器进行数据同步,在这个过程中,如果别的服务还没有启动完成,就会出现Connection refused: connecterror,当其他节点启动完成之后,报错就会消失. AdSearch 服务启动报错 2019-08-16 10:27:57.038 ERROR 73180 --- [ main] o.s.boot.SpringApplication : Applic

[Spring cloud 一步步实现广告系统] 22. 广告系统回顾总结

到目前为止,我们整个初级广告检索系统就初步开发完成了,我们来整体回顾一下我们的广告系统. 整个广告系统编码结构如下: 1.mscx-ad 父模块 主要是为了方便我们项目的统一管理 2.mscx-ad-db 这个模块主要有2个作用,本身只应该作为数据库脚本管理package来使用,但是我们在生成索引文件的过程中,为了方便,我就直接将导出全量索引的json文件生成也写在了该项目中. 主要目的还是通过flyway进行数据库脚本的管理. 3.mscx-ad-common 这个主要是一些通用工具类的存放

[Spring cloud 一步步实现广告系统] 12. 广告索引介绍

索引设计介绍 在我们广告系统中,为了我们能更快的拿到我们想要的广告数据,我们需要对广告数据添加类似于数据库index一样的索引结构,分两大类:正向索引和倒排索引. 正向索引 通过唯一键/主键生成与对象的映射关系. 比如,我们从数据库中查询数据的时候,根据数据主键ID查询当前记录,其实就是一个正向索引的过程. 根据这个描述,很明显,我们的正向索引适用于推广计划,推广单元 和 创意这几张表的数据上,因为广告检索的请求信息,不可能是请求具体的计划或推广单元,它的检索请求一定是限制条件. 倒排索引 也叫

[Spring cloud 一步步实现广告系统] 6. Service实现&amp;Zuul配置&amp;Test

DAO层设计实现 这里我们使用Spring DATA JPA来实现数据库操作,当然大家也可以使用Mybatis,都是一样的,我们依然以用户表操作为例: /** * AdUserRepository for 用户数据库操作接口 * 继承自JpaRepository<AdUser, Long>,第一个参数AdUser代表当前要操作的实体类的class定义,第二个参数Long表示该类的主键类型 * * @author <a href="mailto:[email protected]

[Spring cloud 一步步实现广告系统] 3. 网关路由

Zuul(Router and Filter) WIKI: 传送门 作用 认证,鉴权(Authentication/Security) 预判(Insights) 压力测试(Stress Testing) 灰度/金丝雀测试(Canary Testing) 动态路由(Dynamic Routing) 服务迁移(Service Migration) 降低负载(Load Shedding) 静态响应处理(Static Response handling) 主动/主动交换管理(Active/Active