基于springboot+redis+bootstrap+mysql开发一套属于自己的分布式springcloud云权限架构(十四)【权限架构消费者(通用类编写)】

       权限架构的消费者和权限架构的生产者一样可以高度抽象化我们的通用接口出来,因此本章我们将这些消费者接口高度抽象出来,理论上这些高度抽象出来的接口是可以作为一个独立的module需要的时候使用maven引入,不过此处就不再解耦出来,而是直接写在我们的权限架构服务的消费者项目中。

       直接在我们的工程中创建权限架构服务的消费者的modules如下所示:


        接着在我们的rbac-consumer项目中创建如下的包结构:


        在我们编写抽象类之前我们要把我们第二章写的实体模型通过maven引入到我们的rbac-consumer项目中,在我们pom.xml文件中加入以下配置以后的完整配置文件如下:

<?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">
	<modelVersion>4.0.0</modelVersion>

	<groupId>com.consumer</groupId>
	<artifactId>rbac-consumer</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>rbac-consumer</name>
	<description>权限架构消费者</description>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.10.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>

		<dependency>
			<groupId>com.base</groupId>
			<artifactId>model</artifactId>
			<version>[0.0.1-SNAPSHOT,)</version>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>

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

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

		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-zipkin</artifactId>
			<version>RELEASE</version>
		</dependency>

		<!-- 引入json的依赖 classifier必须要加这个是json的jdk的依赖-->
		<dependency>
			<groupId>net.sf.json-lib</groupId>
			<artifactId>json-lib</artifactId>
			<version>2.4</version>
			<classifier>jdk15</classifier>
		</dependency>

	</dependencies>

	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-dependencies</artifactId>
				<version>Edgware.RELEASE</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>


</project>

    现在开始编写我们的基础工具包的常量类,在我们的base包底下我们创建constant包,并在该包中创建SystemStaticConst.java类,文件内容如下:

package com.consumer.common.base.constant;

/**
 * Created by Administrator on 2017/8/7 0007.
 */
public class SystemStaticConst {

    public final static String RESULT = "result";
    public final static boolean SUCCESS = true;
    public final static boolean FAIL = false;
    public final static String MSG = "msg";

}

    接着再编写我们的基础工具包的service抽象类,在我们的base包底下我们创建service包,并在该包中创建GenericService.java抽象类,文件内容如下:

package com.consumer.common.base.service;


import com.base.common.QueryBase;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Map;

/**
 * Created by Administrator on 2018/1/30 0030.
 */
public interface GenericService<T, Q extends QueryBase> {

    /**
     * 功能描述:根据ID来获取数据
     * @param id
     * @return
     */
    @RequestMapping(value = "/getById",method = RequestMethod.POST,produces = MediaType.APPLICATION_JSON_VALUE)
    public Map<String,Object> getById(@RequestParam("id") int id);


    /**
     * 功能描述:获取数据
     * @param entity
     * @return
     */
    @RequestMapping(value = "/get",method = RequestMethod.POST,produces = MediaType.APPLICATION_JSON_VALUE)
    public Map<String,Object> get(@RequestBody T entity);

    /**
     * 功能描述:保存数据
     * @param entity
     * @return
     */
    @RequestMapping(value = "/save",method = RequestMethod.POST,produces = MediaType.APPLICATION_JSON_VALUE)
    Map<String,Object> save(@RequestBody T entity);

    /**
     * 功能描述:更新数据数据
     * @param entity
     * @return
     */
    @RequestMapping(value = "/update",method = RequestMethod.POST,produces = MediaType.APPLICATION_JSON_VALUE)
    Map<String,Object> update(@RequestBody T entity);

    /**
     * 功能描述:实现删除数据
     * @param entity
     * @return
     */
    @RequestMapping(value = "/remove",method = RequestMethod.POST,produces = MediaType.APPLICATION_JSON_VALUE)
    Map<String,Object> remove(@RequestBody T entity);


    /**
     * 功能描述:实现批量删除的记录
     * @param json
     * @return
     */
    @RequestMapping(value = "/removeBath",method = RequestMethod.POST,produces = MediaType.APPLICATION_JSON_VALUE)
    Map<String,Object> removeBath(@RequestParam("json") String json);

    /**
     * 功能描述:获取分页的数据
     * @param entity
     * @return
     */
    @RequestMapping(value = "/list",method = RequestMethod.POST,produces = MediaType.APPLICATION_JSON_VALUE)
    Map<String,Object> list(@RequestBody Q entity);

    /**
     * 功能描述:判断当前的元素是否已经存在
     * @param entity
     * @return
     */
    @RequestMapping(value = "/isExist",method = RequestMethod.POST,produces = MediaType.APPLICATION_JSON_VALUE)
    Map<String,Object> isExist(@RequestBody Q entity);

}

      接着再编写我们的基础工具包的controller抽象类,在我们的base包底下我们创建controller包,并在该包中创建GenericController.java抽象类,文件内容如下:

package com.consumer.common.base.controller;




import com.base.common.QueryBase;
import com.consumer.common.base.service.GenericService;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.Map;


public abstract class GenericController<T, Q extends QueryBase> {

	// 抽象方法
	protected abstract GenericService<T, Q> getService();

	/**
	 * 功能描述:根据ID来获取数据
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/getById",method = RequestMethod.POST,produces = MediaType.APPLICATION_JSON_VALUE)
	public Map<String,Object> getById(int id){
		return getService().getById(id);
	}

	/**
	 * 功能描述:获取数据
	 * @param entity
	 * @return
	 */
	@RequestMapping(value = "/get",method = RequestMethod.POST,produces = MediaType.APPLICATION_JSON_VALUE)
	public Map<String,Object> get(T entity)throws Exception {
		return getService().get(entity);
	}

	/**
	 * 功能描述:保存数据
	 * @param entity
	 * @return
	 */
	@RequestMapping(value = "/save",method = RequestMethod.POST,produces = MediaType.APPLICATION_JSON_VALUE)
	public Map<String,Object> save(T entity) throws Exception{
		return getService().save(entity);
	}

	/**
	 * 功能描述:更新数据
	 * @param entity
	 * @return
	 */
	@RequestMapping(value = "/update",method = RequestMethod.POST,produces = MediaType.APPLICATION_JSON_VALUE)
	public Map<String,Object> update(T entity)  throws Exception{
		return getService().update(entity);
	}

	/**
	 * 功能描述:实现删除数据
	 * @param entity
	 * @return
	 */
	@RequestMapping(value = "/remove",method = RequestMethod.POST,produces = MediaType.APPLICATION_JSON_VALUE)
	public Map<String,Object> remove(T entity) throws Exception{;
		return getService().remove(entity);
	}


	/**
	 * 功能描述:实现批量删除的记录
	 * @param json
	 * @return
	 */
	@RequestMapping(value = "/removeBath",method = RequestMethod.POST,produces = MediaType.APPLICATION_JSON_VALUE)
	public Map<String,Object> removeBath(String json) throws Exception{
		return getService().removeBath(json);
	}

	/**
	 * 功能描述:获取分页的数据
	 * @param entity
	 * @return
	 */
	@RequestMapping(value = "/list",method = RequestMethod.POST,produces = MediaType.APPLICATION_JSON_VALUE)
	public Map<String,Object> list(Q entity){
		return getService().list(entity);
	}

	/**
	 * 功能描述:判断当前的元素是否已经存在
	 * @param entity
	 * @return
	 */
	@RequestMapping(value = "/isExist",method = RequestMethod.POST,produces = MediaType.APPLICATION_JSON_VALUE)
	public Map<String,Object> isExist(Q entity){
		return getService().isExist(entity);
	}

}

       在我们的微服务运行的过程中我们需要获取相应的日志,因此我们需要配置一个获取相应日志级别日志的配置类,因此我们在config包底下创建了FullLogConfiguration.java实现相应日志级别的输出:

package com.consumer.common.config;

import feign.Logger;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/*
* 类描述:设置获取日志的级别为全部日志
* @auther linzf
* @create 2017/12/22 0022 
*/
@Configuration
public class FullLogConfiguration {

    @Bean
    Logger.Level feignLoggerLevel() {
        // • NONE: 不记录任何信息。
        // • BASIC: 仅记录请求方法、URL以及响应状态码和执行时间。
        // • HEADERS: 除了记录BASIC级别的信息之外, 还会记录请求和响应的头信息。
        // • FULL: 记录所有请求与响应的明细, 包括头信息、 请求体、 元数据等。
        return Logger.Level.FULL;
    }

}

       到此为止我们已经完全了权限架构服务消费者的高度抽象化的工作,再下一章我们将讲诉我们如何基于我们本章的通用类来实现我们的权限架构服务消费者的具体业务实现逻辑。

       到此为止的GitHub项目地址:https://github.com/185594-5-27/spring-cloud-rbac/tree/master-base-consumer

上一篇文章地址:基于springboot+redis+bootstrap+mysql开发一套属于自己的分布式springcloud云权限架构(十三)【权限架构生产者(改造角色管理)】

下一篇文章地址:基于springboot+redis+bootstrap+mysql开发一套属于自己的分布式springcloud云权限架构(十五)【权限架构消费者(完整实现)】


QQ交流群:578746866


猜你喜欢

转载自blog.csdn.net/linzhefeng89/article/details/79351122