責(zé)任鏈模式是一種對象的行為模式。在責(zé)任鏈模式里,很多對象由每一個對象對其下家的引用而連接起來形成一條鏈。請求在這個鏈上傳遞,直到鏈上的某一個對象決定處理此請求。發(fā)出這個請求的客戶端并不知道鏈上的哪一個對象最終處理這個請求,這使得系統(tǒng)可以在不影響客戶端的情況下動態(tài)地重新組織和分配責(zé)任。 -《JAVA與模式》
1. 什么是責(zé)任鏈
在《HeadFisrt設(shè)計模式》一書中并沒有關(guān)于責(zé)任鏈描述,再次驗證設(shè)計模式只是編碼的約定,某個通俗易懂的名稱,并不是某種蓋棺定論的東西。責(zé)任鏈模式和觀察者模式有相似的的地方,即事件(或責(zé)任)的傳遞。觀察者是發(fā)散式的,一對多;責(zé)任鏈?zhǔn)擎準(zhǔn)降模粋饕弧?/p>
/* 抽象的責(zé)任處理者 */
public abstract class AbstractHandler {
private AbstractHandler next;
public void handle(String param) {
/* 做自己的事情 讓別人無事可做 */
customized(param);
if (next != null) {
next.handle(param);
}
}
public abstract void customized(String param);
public void setNext(AbstractHandler next) {
this.next = next;
}
}
/* 具體的責(zé)任處理者 */
public class Handler1 extends AbstractHandler {
@Override
public void customized(String param) {
System.out.println("handler1: handle " + param);
}
}
public class Handler2 extends AbstractHandler {
@Override
public void customized(String param) {
System.out.println("handler2: handle " + param);
}
}
public class Handler3 extends AbstractHandler {
@Override
public void customized(String param) {
System.out.println("handler3: handle " + param);
}
}
/* 當(dāng)有大事要發(fā)生的時候 */
public class HandlerMain {
public static void main(String[] args) {
AbstractHandler chain = HandlerChainFactory.generate();
chain.handle("rabbit");
}
}
public class HandlerChainFactory {
public static AbstractHandler generate() {
AbstractHandler chain = new Handler1();
Handler2 handler2 = new Handler2();
Handler3 handler3 = new Handler3();
chain.setNext(handler2);
handler2.setNext(handler3);
return chain;
}
}
2. 為什么要用責(zé)任鏈
解耦:模塊間解耦,各模塊負(fù)責(zé)各自的業(yè)務(wù),職責(zé)明確單一
有序:模塊有序排布、執(zhí)行,方便跳轉(zhuǎn)
可擴(kuò):易于擴(kuò)展,項鏈表一樣快速插入
3. 怎么使用責(zé)任鏈
責(zé)任鏈在使用方式上,按控制邏輯的位置可以分為:內(nèi)控和外控。
3.1 內(nèi)控
內(nèi)控是指控制邏輯在模塊內(nèi)部,由各模塊來控制責(zé)任鏈的走向:能不能繼續(xù)走下去。代表有:Tomcat
Tomcat 中對 Servlet Filter 的處理
3.2 外控
外控是指控制邏輯在模塊外部,各模塊只負(fù)責(zé)各自的業(yè)務(wù),模塊跳轉(zhuǎn)邏輯單獨(dú)處理。代表有:Spring MVC Interceptor 和 Zuul
- Spring MVC
- Zuul
/*
* Copyright 2013 Netflix, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.netflix.zuul.http;
import com.netflix.zuul.FilterProcessor;
import com.netflix.zuul.ZuulRunner;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.runners.MockitoJUnitRunner;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.*;
/**
* Core Zuul servlet which intializes and orchestrates zuulFilter execution
*
* @author Mikey Cohen
* Date: 12/23/11
* Time: 10:44 AM
*/
public class ZuulServlet extends HttpServlet {
private static final long serialVersionUID = -3374242278843351500L;
private ZuulRunner zuulRunner;
@Override
public void init(ServletConfig config) throws ServletException {
super.init(config);
String bufferReqsStr = config.getInitParameter("buffer-requests");
boolean bufferReqs = bufferReqsStr != null && bufferReqsStr.equals("true") ? true : false;
zuulRunner = new ZuulRunner(bufferReqs);
}
@Override
public void service(javax.servlet.ServletRequest servletRequest, javax.servlet.ServletResponse servletResponse) throws ServletException, IOException {
try {
init((HttpServletRequest) servletRequest, (HttpServletResponse) servletResponse);
// Marks this request as having passed through the "Zuul engine", as opposed to servlets
// explicitly bound in web.xml, for which requests will not have the same data attached
RequestContext context = RequestContext.getCurrentContext();
context.setZuulEngineRan();
try {
preRoute();
} catch (ZuulException e) {
error(e);
postRoute();
return;
}
try {
route();
} catch (ZuulException e) {
error(e);
postRoute();
return;
}
try {
postRoute();
} catch (ZuulException e) {
error(e);
return;
}
} catch (Throwable e) {
error(new ZuulException(e, 500, "UNHANDLED_EXCEPTION_" + e.getClass().getName()));
} finally {
RequestContext.getCurrentContext().unset();
}
}
/**
* executes "post" ZuulFilters
*
* @throws ZuulException
*/
void postRoute() throws ZuulException {
zuulRunner.postRoute();
}
/**
* executes "route" filters
*
* @throws ZuulException
*/
void route() throws ZuulException {
zuulRunner.route();
}
/**
* executes "pre" filters
*
* @throws ZuulException
*/
void preRoute() throws ZuulException {
zuulRunner.preRoute();
}
/**
* initializes request
*
* @param servletRequest
* @param servletResponse
*/
void init(HttpServletRequest servletRequest, HttpServletResponse servletResponse) {
zuulRunner.init(servletRequest, servletResponse);
}
/**
* sets error context info and executes "error" filters
*
* @param e
*/
void error(ZuulException e) {
RequestContext.getCurrentContext().setThrowable(e);
zuulRunner.error();
}
}
4. 怎么選用責(zé)任鏈實(shí)現(xiàn)
洋蔥式
代表:Spring MVC Interceptor
優(yōu)點(diǎn):Interceptor 在方法界別分隔預(yù)處理、后置處理邏輯,職責(zé)明確
缺點(diǎn):大部分 Interceptor 只包含預(yù)處理或只包含后置處理,Interceptor 相對較重
半開式
代表:Zuul Filter
優(yōu)點(diǎn):輕量靈活,符合大部分業(yè)務(wù)處理邏輯(相對洋蔥式)
缺點(diǎn):不方便實(shí)現(xiàn)環(huán)裝邏輯(相對洋蔥式)
鏈表式
代表:Pigeon Filter 和 Servlet Filter
優(yōu)點(diǎn):支持環(huán)裝結(jié)構(gòu),可以實(shí)現(xiàn) try-catch 邏輯,控制更加靈活
缺點(diǎn):如果需要開放給外部使用,出錯風(fēng)險相對較高
一句話總結(jié):
- 開放給外部業(yè)務(wù)使用,推薦洋蔥式
- 內(nèi)部業(yè)務(wù)使用,推薦鏈表式
- 復(fù)雜的 Servlet 的邏輯,推薦半開式