net6 对接Nacos
服务发现和服务健康监测
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实例