BeetleX服务网关之服务发现与泛域名路由
<p><span style="font-size: 15px">在新版本的服务网关中提供了服务发现和泛域名路由解决功能,服务发现可以在无须配置的情况下实现服务自动注册到网关中解脱对服务配置的繁琐工作;而泛域名路由则可以针对不同的域名制定不同的负载规则。</span></p><h1 class="flex-auto min-width-0 mb-2 mb-md-0 mr-0 mr-md-2 gh-header-title instapaper_title"><span style="font-size: 18pt">使用consul服务发现插件</span></h1>
<p><span style="font-size: 15px">网关需要维护相关负载的服务器,手动添加相对来说是一件比较麻烦的工作;为了解决这一问题组件扩展了一个基于<code>consul</code>服务发现插件,通过配置这个插件和启用后网关会自动从<code>consul</code>服务中获取服务并添加到网关对应的路由负载规则中。</span></p>
<h2>引用插件</h2>
<p><span style="font-size: 15px"><code>Bumblebee</code>中使用服务发现需要引用两个插件,分别是<code>BeetleX.Bumblebee.Configuration</code>和<code>BeetleX.Bumblebee.BeetleX.Bumblebee.Consul</code>。加载启动后就可以通过管理工具进行插件配置.</span></p>
<div class="highlight highlight-source-cs">
<div class="cnblogs_code">
<pre> g = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Gateway();
g.HttpOptions(
o </span>=><span style="color: rgba(0, 0, 0, 1)">
{
o.Port </span>= <span style="color: rgba(128, 0, 128, 1)">80</span><span style="color: rgba(0, 0, 0, 1)">;
o.LogToConsole </span>= <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">;
o.LogLevel </span>=<span style="color: rgba(0, 0, 0, 1)"> BeetleX.EventArgs.LogType.Error;
});
g.Open();
g.LoadPlugin(
</span><span style="color: rgba(0, 0, 255, 1)">typeof</span><span style="color: rgba(0, 0, 0, 1)">(Bumblebee.Configuration.Management).Assembly,
</span><span style="color: rgba(0, 0, 255, 1)">typeof</span><span style="color: rgba(0, 0, 0, 1)">(Bumblebee.Consul.ConsulPlugin).Assembly
);</span></pre>
</div>
</div>
<p><span style="font-size: 15px">如果不想自己编写代码可以下载编译的运行包 https://github.com/IKende/Bumblebee/blob/master/bin/ 下载<code>1.1.9</code>或更高版本;运行后 访问 http://host/__system/bumblebee/</span></p>
<h2>插件管理</h2>
<p><span style="font-size: 15px">运行程序后进行配置管理工具的插件管理页面,可以看到Consul插件,组件默认是关闭需要进行一些配置。</span></p>
<p><img src="https://img2018.cnblogs.com/blog/254151/201910/254151-20191028111020493-408870217.png" alt=""></p>
<h2>插件配置</h2>
<div class="highlight highlight-source-json">
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">{
</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">ConsulAddress</span><span style="color: rgba(128, 0, 0, 1)">"</span>: <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">http://192.168.2.19:8500</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">,
</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">Services</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">: [
</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">bumblebee_services</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">
],
</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">Token</span><span style="color: rgba(128, 0, 0, 1)">"</span>: <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">,
</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">DataCenter</span><span style="color: rgba(128, 0, 0, 1)">"</span>: <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">dc1</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">
}</span></pre>
</div>
</div>
<ul>
<li><span style="font-size: 15px">ConsulAddress 对应<code>Consul</code>的服务地址</span></li>
<li><span style="font-size: 15px">Services 检索相应名称的服务列表一个或多个</span></li>
<li><span style="font-size: 15px">Token 访问<code>Consul</code>相应的Token信息</span></li>
<li><span style="font-size: 15px">DataCenter 检索相应的数据中心名称</span></li>
</ul>
<p><span style="font-size: 15px">配置完成后只需要启用插件即可</span></p>
<h2>注册要求</h2>
<div class="highlight highlight-source-cs">
<div class="cnblogs_code">
<pre>Dictionary<<span style="color: rgba(0, 0, 255, 1)">string</span>, <span style="color: rgba(0, 0, 255, 1)">string</span>> meta = <span style="color: rgba(0, 0, 255, 1)">new</span> Dictionary<<span style="color: rgba(0, 0, 255, 1)">string</span>, <span style="color: rgba(0, 0, 255, 1)">string</span>><span style="color: rgba(0, 0, 0, 1)">();
meta.Add(</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">path</span><span style="color: rgba(128, 0, 0, 1)">"</span>, <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">^/home.*</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">);
client.Agent.ServiceDeregister(</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">api_test1</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">).Wait();
client.Agent.ServiceDeregister(</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">api_test2</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">).Wait();
client.Agent.ServiceRegister(</span><span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> AgentServiceRegistration
{
Tags </span>= <span style="color: rgba(0, 0, 255, 1)">new</span> <span style="color: rgba(0, 0, 255, 1)">string</span>[] { <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">Bumblebee</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)"> },
Address </span>= <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">192.168.2.18</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">,
Port </span>= <span style="color: rgba(128, 0, 128, 1)">8080</span><span style="color: rgba(0, 0, 0, 1)">,
Name </span>= <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">bumblebee_services</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">,
Meta </span>=<span style="color: rgba(0, 0, 0, 1)"> meta,
ID </span>= <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">api_test1</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">
}).Wait();</span></pre>
</div>
</div>
<p><span style="font-size: 15px">以上是一个简单的注册代码,如果想服务需要注册到相应路由规则下的负载需要指定<code>path</code>,在不指写的情况<code>bumblebee</code>获取后会注册到默认路由规则上。</span></p>
<h2>状态跟踪和故障</h2>
<p><span style="font-size: 15px"><code>bumblebee</code>内部有一套服务监控和故障处理机制无须依据consul相关状态注册信息;<code>bumblebee</code>会根据自身的检测机制对应用进行一个负载迁移和恢复处理。</span></p>
<h1 class="flex-auto min-width-0 mb-2 mb-md-0 mr-0 mr-md-2 gh-header-title instapaper_title"><span style="font-size: 18pt">泛域名路由</span></h1>
<p><span style="font-size: 15px">泛域名解释在<code>web</code>服务代理中比较常用的功能,主要由代理服务器根据不同请求的域名来做不需同服务的转发。Bumblebee同样也支持泛域名路由解释,就是针对不同请求的域名来制定不同的转发规则。<code>Bumblebee</code>对泛域名的转发配置非常简单只需要加载<code>BeetleX.Bumblebee.Configuration</code>新版本插件在路由管理配置即可(由于组件支持标准的http1.1协议,<code>Bumblebee</code>不仅可以做webapi网关,还能做网站代理)。</span></p>
<h2>运行部署</h2>
<p><span style="font-size: 15px">Bumblebee配置路由解释,只需要加载<code>BeetleX.Bumblebee.Configuration</code>插件启动后在<code>Routes</code>中配置即可.</span></p>
<div class="cnblogs_code">
<pre> g = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Gateway();
g.HttpOptions(
o </span>=><span style="color: rgba(0, 0, 0, 1)">
{
o.Port </span>= <span style="color: rgba(128, 0, 128, 1)">80</span><span style="color: rgba(0, 0, 0, 1)">;
o.LogToConsole </span>= <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">;
o.LogLevel </span>=<span style="color: rgba(0, 0, 0, 1)"> BeetleX.EventArgs.LogType.Error;
});
g.Open();
g.LoadPlugin(
</span><span style="color: rgba(0, 0, 255, 1)">typeof</span><span style="color: rgba(0, 0, 0, 1)">(Bumblebee.Configuration.Management).Assembly
);</span></pre>
</div>
<p><span style="font-size: 15px">如果不想自己编写代码可以下载编译的运行包 https://github.com/IKende/Bumblebee/blob/master/bin/ 下载1.2或更高版本;运行后 访问 http://host/__system/bumblebee/</span></p>
<h2>配置</h2>
<p><span style="font-size: 15px">在<code>Routes</code>添加路由规则,如果转发规则需要对域名进行一个匹配那规则是<code>域名|路径</code></span></p>
<div class="cnblogs_code">
<pre>beetlexjs.ikende.com|.*</pre>
</div>
<p><span style="font-size: 15px">以上规则是配置经过<code>beetlexjs.ikende.com</code>域访问的所有请求都到这个规则里。 </span></p>
<p><img src="https://img2018.cnblogs.com/blog/254151/201910/254151-20191028111201943-1847796059.png" alt=""></p>
<p><span style="font-size: 15px"> 以上官网的路由配置规则 <code>beetlexjs.ikende.com|.*</code>路由到<code>http://localhost:8081/</code>,而其它则路由<code>http://localhost:8080/</code>。由于服务打开了处理服务地址输出所以访问这两个域的头信息可以看到:</span></p>
<ul>
<li>ikende.com</li>
</ul>
<p><img src="https://img2018.cnblogs.com/blog/254151/201910/254151-20191028111219535-923897293.png" alt=""></p>
<ul>
<li>beetlexjs.ikende.com</li>
</ul>
<p><img src="https://img2018.cnblogs.com/blog/254151/201910/254151-20191028111227036-1535547028.png" alt=""></p>
<p> </p>
<p><span style="font-size: 15px">更多资料可查看组件开源网站 https://github.com/IKende/Bumblebee/wiki</span></p>
</div>
<div id="MySignature" role="contentinfo">
访问Beetlex的Github<br><br>
来源:https://www.cnblogs.com/smark/p/11751113.html
頁:
[1]