1. 程式人生 > >基於springboot+redis+bootstrap+mysql開發一套屬於自己的分散式springcloud雲許可權架構(十四)【許可權架構消費者(通用類編寫)】

基於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;
    }

}

       到此為止我們已經完全了許可權架構服務消費者的高度抽象化的工作,再下一章我們將講訴我們如何基於我們本章的通用類來實現我們的許可權架構服務消費者的具體業務實現邏輯。


QQ交流群:578746866