服務發現和服務健康監測
Nacos 支持基於 DNS 和基於 RPC 的服務發現。服務提供者使用 原生SDK、OpenAPI、或一個獨立的Agent TODO注冊 Service 后,服務消費者可以使用DNS TODO 或HTTP&API查找和發現服務。
部署Nacos
1、搭建Nacos注冊中心
-
下載Nacos[https://github.com/alibaba/nacos/releases]
-
解壓后配置持久化
-
創建一個數據庫nacos_config,並執行紅色框內的2個Sql腳本
- 修改配置文件
application.properties
,主要配置連接db
```
### Count of DB:
db.num=1### Connect URL of DB:
db.url.0=jdbc:mysql://127.0.0.1:3306/nacos_config?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useUnicode=true&useSSL=false&serverTimezone=PRC
db.user.0=nacos
db.password.0=nacos
``` - 修改配置文件
-
-
進入nacos/bin目錄下,輸入cmd,執行
startup -m standalone
啟動 -
通過http://localhost:8848/nacos驗證,輸入賬號:nacos和密碼:nacos
-
NET6接入Nacos
Nacos添加配置文件
{ "nacos": { "ServerAddresses": [ "http://localhost:8848" ], "DefaultTimeOut": 15000, "Namespace": "01786d05-61ed-4d81-9a31-698faf51aa96", // Please set the value of Namespace ID !!!!!!!! "ListenInterval": 1000, "ServiceName": "UserService", "GroupName": "DEFAULT_GROUP", "ClusterName": "DEFAULT", "RegisterEnabled": true, "InstanceEnabled": true, "Ephemeral": true, "ConfigUseRpc": true, "NamingUseRpc": true, "LBStrategy": "WeightRoundRobin" //WeightRandom WeightRoundRobin } }
添加Nacos依賴
<PackageReference Include="nacos-sdk-csharp.AspNetCore" Version="1.3.1" /> <PackageReference Include="nacos-sdk-csharp.Extensions.Configuration" Version="1.3.1" />
修改appsettings.json
文件
// 注冊服務到Nacos builder.Services.AddNacosAspNet(builder.Configuration); //默認節點Nacos // 添加配置中心 builder.Host.ConfigureAppConfiguration((context, builder) => { var config = builder.Build(); builder.AddNacosV2Configuration(config.GetSection("NacosConfig")); });
整合到Ocelot
引入依賴
<PackageReference Include="nacos-sdk-csharp.AspNetCore" Version="1.3.1" /> <PackageReference Include="nacos-sdk-csharp.Extensions.Configuration" Version="1.3.1" /> <PackageReference Include="Ocelot.Provider.Nacos" Version="1.2.2" />
修改配置文件
{ "NacosConfig": { "Listeners": [ { "Optional": false, "DataId": "GatewayConfig", "Group": "DEFAULT_GROUP" } ], "Optional": false, "Namespace": "MiscroService", "ServerAddresses": [ "http://192.168.3.102:8848/" ] } }
修改Program.cs文件
// 添加Ocelot對應Nacos擴展 builder.Services.AddOcelot().AddNacosDiscovery(); // 添加配置中心 builder.Host.ConfigureAppConfiguration((context, builder) => { var config = builder.Build(); builder.AddNacosV2Configuration(config.GetSection("NacosConfig")); });
Nacos添加配置文件
{ "Routes": [ { "UpstreamPathTemplate": "/microservice/{url}", "UpstreamHttpMethod": [ "Get", "Post" ], "DownstreamPathTemplate": "/api/{url}", "DownstreamScheme": "http", "UseServiceDiscovery": true, "ServiceName": "OrderService", "LoadBalancerOptions": { "Type": "RoundRobin" } } ], "GlobalConfiguration": { "ServiceDiscoveryProvider": { "Type": "Nacos" } }, "nacos": { "ServerAddresses": [ "http://192.168.3.102:8848" ], "DefaultTimeOut": 15000, "Namespace": "MiscroService", "ListenInterval": 1500, "ServiceName": "Gateway", "GroupName": "DEFAULT_GROUP", "ClusterName": "DEFAULT", "RegisterEnabled": true, "InstanceEnabled": true, "ConfigUseRpc": true, "NamingUseRpc": true, "Ephemeral": true, "LBStrategy": "WeightRoundRobin" } }
測試是否讀取OrderService實例