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/shenyu-client-dotnet.git


The following commit(s) were added to refs/heads/main by this push:
     new 83bf303  Support register by Nacos (#29)
83bf303 is described below

commit 83bf3039e208524296ced5c561cc316fd1ff6a5b
Author: Han Gao <[email protected]>
AuthorDate: Fri Aug 12 10:29:41 2022 +0800

    Support register by Nacos (#29)
---
 .../Apache.ShenYu.AspNetCore.csproj                |   2 +-
 .../Utils/ShenyuCollectionExtensions.cs            |   5 +
 .../Apache.ShenYu.Client.csproj                    |   3 +-
 .../Registers/ShenyuNacosRegister.cs               | 105 +++++++++++++++++++++
 client/Apache.ShenYu.Client/Utils/Constants.cs     |   1 +
 .../Utils/DefaultHttpClientFactory.cs              |  37 ++++++++
 6 files changed, 151 insertions(+), 2 deletions(-)

diff --git a/client/Apache.ShenYu.AspNetCore/Apache.ShenYu.AspNetCore.csproj 
b/client/Apache.ShenYu.AspNetCore/Apache.ShenYu.AspNetCore.csproj
index ad7387d..58a7174 100644
--- a/client/Apache.ShenYu.AspNetCore/Apache.ShenYu.AspNetCore.csproj
+++ b/client/Apache.ShenYu.AspNetCore/Apache.ShenYu.AspNetCore.csproj
@@ -44,7 +44,7 @@
     <PackageReference Include="Microsoft.AspNetCore.Mvc.Core" Version="2.2.0" 
/>
     <PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" 
Version="6.0.0" />
     <PackageReference Include="Microsoft.AspNetCore" Version="2.2.0" />
-    <PackageReference Include="Microsoft.Extensions.Configuration" 
Version="2.2.0" />
+    <PackageReference Include="Microsoft.Extensions.Configuration" 
Version="3.1.0" />
   </ItemGroup>
 
 </Project>
diff --git 
a/client/Apache.ShenYu.AspNetCore/Utils/ShenyuCollectionExtensions.cs 
b/client/Apache.ShenYu.AspNetCore/Utils/ShenyuCollectionExtensions.cs
index c6ab9b1..ee42795 100644
--- a/client/Apache.ShenYu.AspNetCore/Utils/ShenyuCollectionExtensions.cs
+++ b/client/Apache.ShenYu.AspNetCore/Utils/ShenyuCollectionExtensions.cs
@@ -69,6 +69,11 @@ namespace Apache.ShenYu.AspNetCore.Utils
                     services.AddSingleton<IShenyuRegister, 
ShenyuConsulRegister>();
                     break;
                 }
+                case Constants.RegisterType.Nacos:
+                {
+                    services.AddSingleton<IShenyuRegister, 
ShenyuNacosRegister>();
+                    break;
+                }
                 default:
                     throw new Exception($"not supported type 
{shenyuOptions.Register.RegisterType}");
             }
diff --git a/client/Apache.ShenYu.Client/Apache.ShenYu.Client.csproj 
b/client/Apache.ShenYu.Client/Apache.ShenYu.Client.csproj
index cba2a98..e2e1a6b 100644
--- a/client/Apache.ShenYu.Client/Apache.ShenYu.Client.csproj
+++ b/client/Apache.ShenYu.Client/Apache.ShenYu.Client.csproj
@@ -46,9 +46,10 @@
     <Folder Include="Models\DTO\" />
   </ItemGroup>
   <ItemGroup>
-    <PackageReference Include="Microsoft.Extensions.Logging.Abstractions" 
Version="2.2.0" />
+    <PackageReference Include="Microsoft.Extensions.Logging.Abstractions" 
Version="5.0.0" />
     <PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
     <PackageReference Include="ZooKeeperNetEx" Version="3.4.12.4" />
     <PackageReference Include="Consul" Version="1.6.10.7" />
+    <PackageReference Include="nacos-sdk-csharp" Version="1.3.3" />
   </ItemGroup>
 </Project>
diff --git a/client/Apache.ShenYu.Client/Registers/ShenyuNacosRegister.cs 
b/client/Apache.ShenYu.Client/Registers/ShenyuNacosRegister.cs
new file mode 100644
index 0000000..658a036
--- /dev/null
+++ b/client/Apache.ShenYu.Client/Registers/ShenyuNacosRegister.cs
@@ -0,0 +1,105 @@
+/*
+ * 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.Collections.Generic;
+using System.Net.Http;
+using System.Threading;
+using System.Threading.Tasks;
+using Apache.ShenYu.Client.Models.DTO;
+using Apache.ShenYu.Client.Options;
+using Apache.ShenYu.Client.Utils;
+using Microsoft.Extensions.Logging;
+using Microsoft.Extensions.Options;
+using Nacos.V2;
+using Nacos.V2.Config;
+using Nacos.V2.Naming;
+using Nacos.V2.Naming.Dtos;
+using Newtonsoft.Json;
+
+namespace Apache.ShenYu.Client.Registers
+{
+    public class ShenyuNacosRegister : ShenyuAbstractRegister
+    {
+        private readonly ILogger<ShenyuNacosRegister> _logger;
+        private readonly ILoggerFactory _loggerFactory;
+        private readonly IHttpClientFactory _httpClientFactory;
+        private NacosNamingService _namingService;
+        private NacosConfigService _configService;
+        private ShenyuOptions _shenyuOptions;
+        private HashSet<string> metadataSet = new HashSet<string>();
+
+        public ShenyuNacosRegister(ILoggerFactory loggerFactory, 
ILogger<ShenyuNacosRegister> logger)
+        {
+            this._logger = logger;
+            this._loggerFactory = loggerFactory;
+            this._httpClientFactory = new DefaultHttpClientFactory();
+        }
+
+        public override Task Init(ShenyuOptions shenyuOptions)
+        {
+            this._shenyuOptions = shenyuOptions;
+            NacosSdkOptions options = new NacosSdkOptions();
+            options.Namespace = 
this._shenyuOptions.Register.Props["Namespace"];
+            options.UserName = this._shenyuOptions.Register.Props["UserName"];
+            options.Password = this._shenyuOptions.Register.Props["Password"];
+            options.ServerAddresses = new List<string>() { { 
this._shenyuOptions.Register.ServerList } };
+            var op = Microsoft.Extensions.Options.Options.Create(options);
+
+            this._namingService =
+                new NacosNamingService(this._loggerFactory, op, 
this._httpClientFactory);
+            this._configService = new NacosConfigService(this._loggerFactory, 
op);
+
+            return Task.CompletedTask;
+        }
+
+        public override async Task PersistInterface(MetaDataRegisterDTO 
metadata)
+        {
+            var metadataStr = JsonConvert.SerializeObject(metadata, 
Formatting.None,
+                new JsonSerializerSettings { NullValueHandling = 
NullValueHandling.Ignore });
+            string configPath = 
$"shenyu.register.service.{metadata.rpcType}.{metadata.contextPath.Substring(1)}";
+            lock (this.metadataSet)
+            {
+                this.metadataSet.Add(metadataStr);
+            }
+
+            var set = JsonConvert.SerializeObject(this.metadataSet, 
Formatting.None,
+                new JsonSerializerSettings { NullValueHandling = 
NullValueHandling.Ignore });
+            await this._configService.PublishConfig(configPath, 
"DEFAULT_GROUP", set);
+        }
+
+        public override async Task PersistURI(URIRegisterDTO registerDTO)
+        {
+            string serviceName = 
$"shenyu.register.service.{registerDTO.rpcType}";
+            var uriRegString = JsonConvert.SerializeObject(registerDTO, 
Formatting.None,
+                new JsonSerializerSettings { NullValueHandling = 
NullValueHandling.Ignore });
+            Instance instance = new Instance();
+            instance.Ephemeral = true;
+            instance.Ip = registerDTO.host;
+            instance.Port = registerDTO.port;
+            instance.Metadata = new Dictionary<string, string>();
+            instance.Metadata.Add("contextPath", 
registerDTO.contextPath.Substring(1));
+            instance.Metadata.Add("uriMetadata", uriRegString);
+            await this._namingService.RegisterInstance(serviceName, instance);
+        }
+
+        public override async Task Close()
+        {
+            await this._configService.ShutDown();
+            await this._namingService.ShutDown();
+        }
+    }
+}
diff --git a/client/Apache.ShenYu.Client/Utils/Constants.cs 
b/client/Apache.ShenYu.Client/Utils/Constants.cs
index 376f055..fe4bc06 100644
--- a/client/Apache.ShenYu.Client/Utils/Constants.cs
+++ b/client/Apache.ShenYu.Client/Utils/Constants.cs
@@ -37,6 +37,7 @@ namespace Apache.ShenYu.Client.Utils
             public const string Http = "http";
             public const string Zookeeper = "zookeeper";
             public const string Consul = "consul";
+            public const string Nacos = "nacos";
         }
 
         public class RegisterRpcType
diff --git a/client/Apache.ShenYu.Client/Utils/DefaultHttpClientFactory.cs 
b/client/Apache.ShenYu.Client/Utils/DefaultHttpClientFactory.cs
new file mode 100644
index 0000000..b5ca00a
--- /dev/null
+++ b/client/Apache.ShenYu.Client/Utils/DefaultHttpClientFactory.cs
@@ -0,0 +1,37 @@
+/*
+ * 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;
+using System.Net.Http;
+
+namespace Apache.ShenYu.Client.Utils
+{
+    public sealed class DefaultHttpClientFactory : IHttpClientFactory, 
IDisposable
+    {
+        private readonly Lazy<HttpMessageHandler> _handlerLazy = new 
Lazy<HttpMessageHandler>(() => new HttpClientHandler());
+
+        public HttpClient CreateClient(string name) => new 
HttpClient(this._handlerLazy.Value, disposeHandler: false);
+
+        public void Dispose()
+        {
+            if (this._handlerLazy.IsValueCreated)
+            {
+                this._handlerLazy.Value.Dispose();
+            }
+        }
+    }
+}

Reply via email to