当前仓库属于暂停状态,部分功能使用受限,详情请查阅 仓库状态说明
2 Star 0 Fork 1

JUMEI_ARCH/go-plugins
暂停

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
broker
client
codec
examples
kv
metrics
micro
bot
disable_rpc
gzip
index
ip_whitelist
router
examples
README.md
options.go
router.go
router_test.go
routes.go
routes_test.go
sort.go
sort_test.go
trace
whitelist
README.md
registry
selector
server
sync
trace/zipkin
transport
wrapper
.travis.yml
LICENSE
README.md
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README

Router Plugin

The router plugin is a HTTP handler plugin for the Micro API which enables you to define routes via go-os/config. This is dynamic configuration that can then be leveraged via anything that implements the go-os/config interface e.g file, etcd, consul or the config service.

Features

  • Request Matching
  • Weighted Routing
  • Reverse Proxying
  • Priority Rules
  • Configurable via go-os/Config
  • Pluggable via micro/plugins

TODO

  • Regex Matching Host/Path

Usage

Register the plugin before building Micro

package main

import (
	"github.com/micro/micro/plugin"
	"github.com/micro/go-plugins/micro/router"
)

func init() {
	plugin.Register(router.NewRouter())
}

Config

Configuring the router is done via a go-os/Config source. Here's an example using the File source.

// Create Config Source
f := file.NewSource(
	// Use routes.json file
	config.SourceName("routes.json"),
)

// Create Config
c := config.NewConfig(
	// With Source
	config.WithSource(f),
)

// Create Router
r := router.NewRouter(
	// With Config
	router.Config(c),
)

Routes

Routes are used to config request to match and the response to return. Here's an example.

{
	"api": {
		"routes": [
			{
				"request": {
					"method": "GET",
					"host": "127.0.0.1:10001",
					"path": "/"
				},
				"response": {
					"status_code": 302,
					"header": {
						"location": "http://example.com"
					}
				},
				"weight": 1.0
			},
			{
				"request": {
					"method": "POST",
					"host": "127.0.0.1:10001",
					"path": "/foo"
				},
				"response": {
					"status_code": 301,
					"header": {
						"location": "http://foo.bar.com"
					}
				},
				"weight": 1.0
			}
		]
	}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/JMArch/go-plugins.git
git@gitee.com:JMArch/go-plugins.git
JMArch
go-plugins
go-plugins
v0.3.3

搜索帮助