This is an automated email from the ASF dual-hosted git repository.
xiaoyu pushed a commit to branch main
in repository
https://gitbox.apache.org/repos/asf/incubator-shenyu-client-dotnet.git
The following commit(s) were added to refs/heads/main by this push:
new bba6546 parse real ip instead of the ip provided by ASP.NET Core
framework (#13)
bba6546 is described below
commit bba654675c17cbeec31c93d9375323c645df1db7
Author: Han Gao <[email protected]>
AuthorDate: Sat May 14 17:19:04 2022 +0800
parse real ip instead of the ip provided by ASP.NET Core framework (#13)
---
README.md | 33 +++++++++++++---
.../Services/ShenyuStartupService.cs | 22 +++++++++--
client/Apache.ShenYu.Client/Utils/IpUtils.cs | 44 ++++++++++++++++++++++
.../Properties/launchSettings.json | 2 +-
.../AspNetCoreExample/appsettings.Development.json | 10 +++--
5 files changed, 99 insertions(+), 12 deletions(-)
diff --git a/README.md b/README.md
index 968360b..0ec0d81 100644
--- a/README.md
+++ b/README.md
@@ -7,13 +7,13 @@
For ASP.NET Core project, we can refer to the example code at
`examples/AspNetCoreExample`. What you need to do is quite
simple and straightforward.
-First, add the Shenyu ASP.NET Core dependency into project.
+1. add the Shenyu ASP.NET Core dependency into project.
```shell
dotnet add package <todo>
```
-Then, in `Startup.ConfigureServices` method, add the `ShenyuRegister` service.
+2. in `Startup.ConfigureServices` method, add the `ShenyuRegister` service.
```c#
public void ConfigureServices(IServiceCollection services)
@@ -24,15 +24,17 @@ public void ConfigureServices(IServiceCollection services)
}
```
-Finnaly, set your `Shenyu` configurations in `appsettings.json`.
+3. set your `Shenyu` configurations in `appsettings.json`.
```json
{
"Shenyu": {
"Register": {
"ServerList": "http://localhost:9095",
- "UserName": "<your_admin_user>",
- "Password": "<your_admin_password>"
+ "Props": {
+ "UserName": "<your_admin_user>",
+ "Password": "<your_admin_password>"
+ }
},
"Client": {
"AppName": "dotnet-example",
@@ -44,4 +46,25 @@ Finnaly, set your `Shenyu` configurations in
`appsettings.json`.
}
```
+4. enable calling via ip.
+
+When running on your local machine, ASP.NET Core service can only be called
from `localhost`. To enable calling by IP, you can replace
`https://localhost:{port};http://localhost:{port}` with
`https://*:{port};http://*:{port}` by one of the following ways.
+
+- Setting in `launchSettings.json`. Replace for `applicationUrl` field.
+- Setting by environment variables `ASPNETCORE_URLS`. e.g. `ASPNETCORE_URLS
"http://*:5000"`
+- Adding `--urls` when start. e.g. `dotnet run --urls
"https://*:5001;http://*:5000"`
+- Setting progratically by `UseUrls()` in `Program.cs`.
+
+e.g.
+
+```csharp
+public static IHostBuilder CreateHostBuilder(string[] args) =>
+ Host.CreateDefaultBuilder(args)
+ .ConfigureWebHostDefaults(webBuilder =>
+ {
+ webBuilder.UseStartup<Startup>();
+ webBuilder.UseUrls("http://*:5000", "https://*:5001");
+ });
+```
+
That's all! After finished above steps, you can just start your project and
you can visit `shenyu-admin` portal to see the APIs have been registered in
Shenyu.
diff --git a/client/Apache.ShenYu.AspNetCore/Services/ShenyuStartupService.cs
b/client/Apache.ShenYu.AspNetCore/Services/ShenyuStartupService.cs
index 2388afc..f785af8 100644
--- a/client/Apache.ShenYu.AspNetCore/Services/ShenyuStartupService.cs
+++ b/client/Apache.ShenYu.AspNetCore/Services/ShenyuStartupService.cs
@@ -19,6 +19,7 @@ using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
+using System.Net.NetworkInformation;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
@@ -26,10 +27,9 @@ using Apache.ShenYu.Client.Attributes;
using Apache.ShenYu.Client.Models.DTO;
using Apache.ShenYu.Client.Options;
using Apache.ShenYu.Client.Registers;
+using Apache.ShenYu.Client.Utils;
using Microsoft.AspNetCore.Hosting.Server;
using Microsoft.AspNetCore.Hosting.Server.Features;
-using Microsoft.AspNetCore.Http;
-using Microsoft.AspNetCore.Http.Features;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ApplicationParts;
using Microsoft.AspNetCore.Mvc.Controllers;
@@ -73,11 +73,13 @@ namespace Apache.ShenYu.AspNetCore.Services
await this._shenyuRegister.Init(this._shenyuOptions.Value);
var addresses = this.GetAddresses();
+ var ipAddresses = GetIpAddresses(addresses);
+
var rpcTypes = this._shenyuOptions.Value.Client.ClientType
.Split(',')
.Select(type => type.Trim())
.ToList();
- foreach (var address in addresses)
+ foreach (var address in ipAddresses)
{
var uri = new Uri(address);
if (!rpcTypes.Contains(uri.Scheme))
@@ -143,6 +145,20 @@ namespace Apache.ShenYu.AspNetCore.Services
return Task.CompletedTask;
}
+ private ICollection<string> GetIpAddresses(ICollection<string>
addresses)
+ {
+ var ipAddresses = new HashSet<string>();
+ var ip = IpUtils.GetLocalIPv4(NetworkInterfaceType.Ethernet);
+ foreach (var address in addresses)
+ {
+ var uriBuilder = new UriBuilder(address);
+ uriBuilder.Host = ip;
+ var ipAddr = uriBuilder.Uri.ToString();
+ ipAddresses.Add(ipAddr);
+ }
+ return ipAddresses;
+ }
+
private ICollection<string> GetAddresses()
{
return
this._server.Features.Get<IServerAddressesFeature>().Addresses;
diff --git a/client/Apache.ShenYu.Client/Utils/IpUtils.cs
b/client/Apache.ShenYu.Client/Utils/IpUtils.cs
new file mode 100644
index 0000000..8b31bd7
--- /dev/null
+++ b/client/Apache.ShenYu.Client/Utils/IpUtils.cs
@@ -0,0 +1,44 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+using System.Net.NetworkInformation;
+using System.Net.Sockets;
+
+namespace Apache.ShenYu.Client.Utils
+{
+ public static class IpUtils
+ {
+ public static string GetLocalIPv4(NetworkInterfaceType type)
+ {
+ string output = "";
+ foreach (NetworkInterface item in
NetworkInterface.GetAllNetworkInterfaces())
+ {
+ if (item.NetworkInterfaceType == type &&
item.OperationalStatus == OperationalStatus.Up)
+ {
+ foreach (UnicastIPAddressInformation ip in
item.GetIPProperties().UnicastAddresses)
+ {
+ if (ip.Address.AddressFamily ==
AddressFamily.InterNetwork)
+ {
+ return ip.Address.ToString();
+ }
+ }
+ }
+ }
+ return output;
+ }
+ }
+}
\ No newline at end of file
diff --git a/examples/AspNetCoreExample/Properties/launchSettings.json
b/examples/AspNetCoreExample/Properties/launchSettings.json
index 2ee9ca8..c3961c3 100644
--- a/examples/AspNetCoreExample/Properties/launchSettings.json
+++ b/examples/AspNetCoreExample/Properties/launchSettings.json
@@ -22,7 +22,7 @@
"dotnetRunMessages": "true",
"launchBrowser": true,
"launchUrl": "swagger",
- "applicationUrl": "https://localhost:5001;http://localhost:5000",
+ "applicationUrl": "https://*:5001;http://*:5000",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
diff --git a/examples/AspNetCoreExample/appsettings.Development.json
b/examples/AspNetCoreExample/appsettings.Development.json
index 826b17e..091d877 100644
--- a/examples/AspNetCoreExample/appsettings.Development.json
+++ b/examples/AspNetCoreExample/appsettings.Development.json
@@ -8,9 +8,13 @@
},
"Shenyu": {
"Register": {
- "ServerList": "http://localhost:9095",
- "UserName": "admin",
- "Password": "123456"
+// "ServerList": "http://localhost:9095",
+ "ServerList": "localhost:2181",
+ "RegisterType": "zookeeper",
+ "Props": {
+ "UserName": "admin",
+ "Password": "123456"
+ },
},
"Client": {
"AppName": "dotnet-example",