Period": "1m", // 统计时间段:1s, 5m, 1h, 1d
// "PeriodTimespan": 15, // 多少秒之后客户端可以重试
// "Limit": 5 // 在统计时间段内允许的最大请求数量
//},//负载均衡:
//RoundRobin轮流发送;
//LeastConnection – 将请求发往最空闲的那个服务器
//NoLoadBalance – 总是发往第一个请求或者是服务发现
"LoadBalancerOptions": {
"Type": "RoundRobin"
},
//上游地址配置
"UpstreamPathTemplate": "/test/{everything}",
//上游支持的请求类型
"UpstreamHttpMethod": [ "GET", "POST" ],
"AuthenticationOptions": {
"AuthenticationProviderKey": "TestKey",
"AllowedScopes": []
}
},
{
"DownstreamPathTemplate": "/api/Token",
"DownstreamScheme": "http",
"DownstreamHostAndPorts": [
{
"Host": "127.0.0.1",
"Port": 3322
}
],
"UpstreamPathTemplate": "/GetToken",
"UpstreamHttpMethod": [ "Get" ]
}
],
"GlobalConfiguration": {
"BaseUrl": "https://localhost:8596",
//consul服务器地址和ip
"ServiceDiscoveryProvider": {
"Host": "localhost",
"Port": 8500
}
}
}
3.4 修改program文件,添加访问地址,以及ocelot的配置文件
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
namespace IdentityServer4Test
{
public class Program
{
public static void Main(string[] args)
{
CreateWebHostBuilder(args).Build().Run();
}
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseUrls("http://localhost:8596")
.ConfigureAppConfiguration(conf =>
{
conf.AddJsonFile("ocelot.json", optional: false, reloadOnChange: true);
})
.UseStartup<Startup>();
}
}
测试
1.首先开启consul服务
2.接下来把服务注册到consul当中,启动ConsulServer
3.启动IdentityServer4Test和ocelotServerTest服务
4.通过postMan获取token(正式开发中不会如此使用)
5.根据获取的token去请求Consulserver当中的数据,可正常返回数据
|