Skip to content

Commit

Permalink
[feat] 新增 ExternalBroker 是否使用外部代理。有些RocketMQ的Broker部署在网关外部,需要使用映射地址,默…
Browse files Browse the repository at this point in the history
…认false 。#85
  • Loading branch information
nnhy committed Dec 10, 2024
1 parent ae97b0d commit 93cbb18
Showing 1 changed file with 16 additions and 10 deletions.
26 changes: 16 additions & 10 deletions NewLife.RocketMQ/MqBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ public abstract class MqBase : DisposeBase
/// </example>
public X509Certificate? Certificate { get; set; }

Check warning on line 69 in NewLife.RocketMQ/MqBase.cs

View workflow job for this annotation

GitHub Actions / build-publish

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 69 in NewLife.RocketMQ/MqBase.cs

View workflow job for this annotation

GitHub Actions / build-publish

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 69 in NewLife.RocketMQ/MqBase.cs

View workflow job for this annotation

GitHub Actions / build-publish

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 69 in NewLife.RocketMQ/MqBase.cs

View workflow job for this annotation

GitHub Actions / build-test

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 69 in NewLife.RocketMQ/MqBase.cs

View workflow job for this annotation

GitHub Actions / build-test

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 69 in NewLife.RocketMQ/MqBase.cs

View workflow job for this annotation

GitHub Actions / build-test

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 69 in NewLife.RocketMQ/MqBase.cs

View workflow job for this annotation

GitHub Actions / build-test

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 69 in NewLife.RocketMQ/MqBase.cs

View workflow job for this annotation

GitHub Actions / build-publish

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 69 in NewLife.RocketMQ/MqBase.cs

View workflow job for this annotation

GitHub Actions / build-publish

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 69 in NewLife.RocketMQ/MqBase.cs

View workflow job for this annotation

GitHub Actions / build-publish

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 69 in NewLife.RocketMQ/MqBase.cs

View workflow job for this annotation

GitHub Actions / build-publish

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

/// <summary>是否使用外部代理。有些RocketMQ的Broker部署在网关外部,需要使用映射地址,默认false</summary>
public Boolean ExternalBroker { get; set; }

//public Boolean VipChannelEnabled { get; set; } = true;

/// <summary>是否可用</summary>
Expand Down Expand Up @@ -283,19 +286,22 @@ protected BrokerClient GetBroker(String name)
{
if (_Brokers.TryGetValue(name, out client)) return client;

// broker可能在内网,转为公网地址
var uri = new NetUri(NameServerAddress.Split(";").FirstOrDefault());
var ext = uri.Host;
if (ext.IsNullOrEmpty()) ext = uri.Address.ToString();

var addrs = bk.Addresses.ToArray();
for (var i = 0; i < addrs.Length; i++)
if (ExternalBroker)
{
var addr = addrs[i];
if (addr.StartsWithIgnoreCase("10.", "192.", "172.") && !ext.IsNullOrEmpty())
// broker可能在内网,转为公网地址
var uri = new NetUri(NameServerAddress.Split(";").FirstOrDefault());
var ext = uri.Host;
if (ext.IsNullOrEmpty()) ext = uri.Address.ToString();

for (var i = 0; i < addrs.Length; i++)
{
var p = addr.IndexOf(':');
addrs[i] = p > 0 ? ext + addr[p..] : ext;
var addr = addrs[i];
if (addr.StartsWithIgnoreCase("10.", "192.", "172.") && !ext.IsNullOrEmpty())
{
var p = addr.IndexOf(':');
addrs[i] = p > 0 ? ext + addr[p..] : ext;
}
}
}

Expand Down

0 comments on commit 93cbb18

Please sign in to comment.