semtech/mu-dispatchermu-dispatcher是mu.semte.ch架构中的核心元素之一。该服务根据传入请求的路径将请求分发到其他微服务。您可以通过Docker运行该服务,但通常建议使用mu-project进行配置,以应用自定义设置。
Dispatcher作为一个应用运行,其中Dispatcher模块可被覆盖。启动时,它期望在/config/dispatcher.ex路径下存在dispatcher.ex文件。
Dispatcher通过mu-project中的dispatcher.ex文件进行配置。
基本(默认)配置是一个名为Dispatcher的Elixir模块,它使用Matcher功能。需要定义空的接受类型集(define_accept_types [])。
elixirdefmodule Dispatcher do use Matcher define_accept_types [] ... end
use Matcher宏用于设置匹配器,它会导入Matcher、send_resp和forward函数。
支持以下HTTP方法宏:get、put、post、delete、patch、head、options、match。其中match宏匹配所有方法。
参数:
path:字符串,可包含变量解构options:选项哈希,包含匹配条件:
accept:通过define_accept_types定义的接受类型简写last_call:设为true时用于fallback路由(如404页面)block:处理和发送请求的代码块提供变量:
conn:Plug连接对象,用于转发或响应path:当路径定义为"/something/*path"时,包含未使用的路径段提供一种将Accept类型映射到可读术语的方式,便于一致匹配。接收一个属性数组,描述每个使用的键及其对应的Accept头,支持通配符。
可将一个路径代理到另一个路径。例如,将/sessions请求转发到[***]:
elixirdefmodule Dispatcher do use Matcher define_accept_types [] match "/sessions", _ do forward conn, [], "[***]" end end
可在代码块中添加额外逻辑,如日志记录:
elixirdefmodule Dispatcher do use Matcher define_accept_types [] match "/sessions", _ do IO.inspect(conn, label: "conn for /sessions") forward conn, [], "[***]" end end
转发子路由,例如将所有/widgets开头的请求转发到mu-cl-resources服务:
elixirdefmodule Dispatcher do use Matcher define_accept_types [] match "/widgets/*path", _ do forward conn, path, "[***]" end end
可显式匹配HTTP方法。例如,仅转发POST请求:
elixirdefmodule Dispatcher do use Matcher define_accept_types [] post "/sessions", _ do forward conn, [], "[***]" end end
支持基于主机名的分发,支持字符串格式、数组格式和通配符。
匹配特定主机:
elixirget "/employees", %{ host: "api.redpencil.io" } do ... end
通配符匹配:
elixirget "/employees", %{ host: "*.redpencil.io" } do ... end
提取子域名:
elixirget "/employees", %{ host: ["io", "redpencil", subdomain | subsubdomains] } do IO.inspect(subdomain, "First subdomain") IO.inspect(subsubdomains, "Array of subdomains under subdomain") ... end
根据客户端请求的MIME类型分发请求。首先通过define_accept_types定义Accept类型简写:
elixirdefmodule Dispatcher do use Matcher define_accept_types [ json: ["application/json", "application/vnd.api+json"], img: ["image/jpg", "image/jpeg", "image/png"], gif: ["image/gif"] ] get "/images/*path", %{ accept: %{ json: true } } do forward conn, path, "[***]" end get "/images/*path", %{ accept: %{ img: true } } do forward conn, path, "[***]" end get "/images/*path", %{ accept: %{ gif: true } } do forward conn, path, "[***]" end end
当没有匹配的路由时,应提供404响应。使用last_call: true选项配置fallback路由:
elixirdefmodule Dispatcher do use Matcher define_accept_types [ text: ["text/*"], html: ["text/html", "application/xhtml+html"], json: ["application/json", "application/vnd.api+json"], img: ["image/jpg", "image/jpeg", "image/png"], gif: ["image/gif"], image: ["image/*"] ] # 其他路由规则... get "/*_", %{ last_call: true, accept: %{ json: true } } do send_resp(conn, 404, "{ \"error\": { \"code\": 404, \"message\": \"Route not found. See config/dispatcher.ex\" } }") end get "/*_", %{ last_call: true, accept: %{ image: true } } do forward conn, [], "[***]" end get "/*_", %{ last_call: true, accept: %{ text: true } } do send_resp(conn, 404, "404 - page not found\n\nSee config/dispatcher.ex") end get "/*_", %{ last_call: true, accept: %{ html: true } } do send_resp(conn, 404, "<html><head><title>404 - Not Found</title></head><body><h1>404 - Page not found</h1></body></html>") end end
可在转发前添加逻辑,如复用配置、日志记录或请求修改:
elixirdefmodule Dispatcher do use Matcher define_accept_types [ json: ["application/json", "application/vnd.api+json"] ] @json %{ accept: %{ json: true } } match "/sessions/*path", @json do IO.inspect(conn, label: "Connection for sessions service.") forward conn, path, "[***]" end match "/images/*path", @json do forward conn, path, "[***]" end match "/*_", %{ last_call: true, accept: %{ json: true } } do send_resp(conn, 404, "{ \"error\": { \"code\": 404, \"message\": \"Route not found. See config/dispatcher.ex\" } }") end end
配置Ember应用路由,优先提供HTML页面,同时处理资产请求:
elixirdefmodule Dispatcher do use Matcher define_accept_types [ json: ["application/json", "application/vnd.api+json"], html: ["text/html", "application/xhtml+html"], any: ["*/*"] ] @html %{ accept: %{ html: true } } @json %{ accept: %{ json: true } } @any %{ accept: %{ any: true } } # 其他路由规则... match "/assets/*path", @any do forward conn, path, "[***]" end match "/*_path", @html do forward conn, [], "[***]" end match "/*_", %{ last_call: true, accept: %{ json: true } } do send_resp(conn, 404, "{ \"error\": { \"code\": 404, \"message\": \"Route not found. See config/dispatcher.ex\" } }") end end
处理跨域请求的OPTIONS预检请求:
elixirdefmodule Dispatcher do use Matcher define_accept_types [] options "*path", _ do conn |> Plug.Conn.put_resp_header("access-control-allow-headers", "content-type,accept") |> Plug.Conn.put_resp_header("access-control-allow-methods", "*") |> send_resp(200, "{ \"message\": \"ok\" }") end end
为不同Accept类型提供404响应:
elixirdefmodule Dispatcher do use Matcher define_accept_types [ text: ["text/*"], html: ["text/html", "application/xhtml+html"], json: ["application/json", "application/vnd.api+json"], jpeg: ["image/jpg", "image/jpeg"], png: ["image/png"], gif: ["image/gif"], ] # 其他路由规则... get "/*_", %{ last_call: true, accept: %{ json: true } } do send_resp(conn, 404, "{ \"error\": { \"code\": 404, \"message\": \"Route not found. See config/dispatcher.ex\" } }") end get "/*_", %{ last_call: true, accept: %{ text: true } } do send_resp(conn, 404, "404 - page not found\n\nSee config/dispatcher.ex") end get "/*_", %{ last_call: true, accept: %{ html: true } } do send_resp(conn, 404, "<html><head><title>404 - Not Found</title></head><body><h1>404 - Page not found</h1></body></html>") end get "/*_", %{ last_call: true, accept: %{ jpeg: true } } do forward conn, [], "[***]" end get "/*_", %{ last_call: true, accept: %{ png: true } } do forward conn, [], "[***]" end get "/*_", %{ last_call: true, accept: %{ gif: true } } do forward conn, [], "[***]" end end
转发功能基于plug_mint_proxy构建,使用Mint库创建高效请求。请求接收基于Cowboy 2,支持HTTP/2。
Plug通过自身的匹配器和分发器匹配调用。本库在plug_router_dispatcher.ex中提供了额外支持,将请求分发到自定义匹配器。
Dispatcher支持以下头部操作,通过plug_mint_proxy的操作器配置:
Matcher模块包含核心逻辑,解析请求Accept头和定义的接受类型,寻找最佳分发方案。分发流程如下:
define_accept_types中定义的类型匹配(支持通配符),得到(B)last_call: true选项寻找fallback路由

manifest unknown 错误
TLS 证书验证失败
DNS 解析超时
410 错误:版本过低
402 错误:流量耗尽
身份认证失败错误
429 限流错误
凭证保存错误
来自真实用户的反馈,见证轩辕镜像的优质服务