Bumblebee微服务网关之consul服务发现
source link: https://www.tuicool.com/articles/FVza2iv
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
网关需要维护相关负载的服务器,手动添加相对来说是一件比较麻烦的工作;为了解决这一问题组件扩展了一个基于 consul
服务发现插件,通过配置这个插件和启用后网关会自动从 consul
服务中获取服务并添加到网关对应的路由负载规则中。
引用插件
Bumblebee
使用服务发现需要引用两个插件,分别是 BeetleX.Bumblebee.Configuration
和 BeetleX.Bumblebee.BeetleX.Bumblebee.Consul
。加载启动后就可以通过管理工具进行插件配置.
g = new Gateway(); g.HttpOptions( o => { o.Port = 80; o.LogToConsole = true; o.LogLevel = BeetleX.EventArgs.LogType.Error; }); g.Open(); g.LoadPlugin( typeof(Bumblebee.Configuration.Management).Assembly, typeof(Bumblebee.Consul.ConsulPlugin).Assembly );
如果不想自己编写代码可以下载编译的运行包 https://github.com/IKende/Bumblebee/blob/master/bin/ 下载 1.1.9
或更高版本;运行后 访问 http://host/__system/bumblebee/
插件管理
运行程序后进行配置管理工具的插件管理页面,可以看到Consul插件,组件默认是关闭需要进行一些配置。
插件配置
{ "ConsulAddress": "http://192.168.2.19:8500", "Services": [ "bumblebee_services" ], "Token": null, "DataCenter": "dc1" }
-
ConsulAddress 对应
Consul
的服务地址 -
Services 检索相应名称的服务列表一个或多个
-
Token 访问
Consul
相应的Token信息 -
DataCenter 检索相应的数据中心名称
配置完成后只需要启用插件即可
注册要求
Dictionary<string, string> meta = new Dictionary<string, string>(); meta.Add("path", "^/home.*"); client.Agent.ServiceDeregister("api_test1").Wait(); client.Agent.ServiceDeregister("api_test2").Wait(); client.Agent.ServiceRegister(new AgentServiceRegistration { Tags = new string[] { "Bumblebee" }, Address = "192.168.2.18", Port = 8080, Name = "bumblebee_services", Meta = meta, ID = "api_test1" }).Wait();
以上是一个简单的注册代码,如果想服务需要注册到相应路由规则下的负载需要指定 path
,在不指写的情况 bumblebee
获取后会注册到默认路由规则上。
状态跟踪和故障
bumblebee
内部有一套服务监控和故障处理机制无须依据consul相关状态注册信息; bumblebee
会根据自身的检测机制对应用进行一个负载迁移和恢复处理。
关注公众号
https://github.com/IKende/
高性能的服务通讯框架 Beetlex(http,rpc,gateway的详细实现 )
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK