You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Ocelot.Provider.Nacos/README.md

70 lines
1.9 KiB
Markdown

5 years ago
# Ocelot.Provider.Nacos
5 years ago
Ocelot集成Nacos注册中心组件
5 years ago
### 开发环境
+ .Net Core 3.1 因为最新稳定版的Ocelot是在.Net Core 3.1上构建的
+ Ocelot版本 v16.0.1
5 years ago
+ Nacos访问组件 [nacos-sdk-csharp](https://github.com/catcherwong/nacos-sdk-csharp)
```
<PackageReference Include="nacos-sdk-csharp-unofficial" Version="0.2.7" />
```
5 years ago
它其实是有一个asp.net core版本的组件但是我没有选用虽然那个用起来功能很强大但是我需要自己改造一下让它能更好的适配Ocelot
5 years ago
### 添加引用
```
<PackageReference Include="Ocelot.Provider.Nacos" Version="1.0.0" />
```
```
dotnet add package Ocelot.Provider.Nacos --version 1.0.0
```
5 years ago
### 使用方式
在已有的Ocelot的项目上添加以下内容具体操作可查看[demo](https://github.com/softlgl/Ocelot.Provider.Nacos/tree/master/demo/ApiGatewayDemo)
```cs
public void ConfigureServices(IServiceCollection services)
{
//注册服务发现
services.AddOcelot().AddNacosDiscovery();
}
```
再已有的ocelot配置文件上添加
```json
{
"Routes": [
{
5 years ago
// 用于服务发现的名称也就是注册到nacos上的名称
5 years ago
"ServiceName": "productservice",
"DownstreamScheme": "http",
"DownstreamPathTemplate": "/productapi/{everything}",
"UpstreamPathTemplate": "/productapi/{everything}",
"UpstreamHttpMethod": [ "Get", "Post" ],
"LoadBalancerOptions": {
5 years ago
"Type": "RoundRobin"
5 years ago
},
// 使用服务发现
"UseServiceDiscovery": true
}
],
"GlobalConfiguration": {
"ServiceDiscoveryProvider": {
//这里是重点
"Type": "Nacos"
}
}
}
```
然后添加在appsettings.json文件中添加,具体配置字段和nacos-sdk-csharp是保持一致的
```json
"nacos": {
"ServerAddresses": [ "http://localhost:8848" ],
"DefaultTimeOut": 15000,
"Namespace": "",
"ListenInterval": 1000,
5 years ago
// 网关服务名称
5 years ago
"ServiceName": "apigateway"
}
```