简介

主要是采用identity Server4 和ocelot 加上consul 实现简单的客户端模式

开发准备

 环境准备

  • 下载并安装Consul具体请参考前几篇的内容

项目介绍

  • 创建ocelotServerTest项目
  • 创建IdentityServer4Test项目
  • 创建consulServer项目(API项目)  

1.创建Consulserver项目

   参考该地址进行创建:微服务(入门二):netcore通过consul注册服务

2.创建identityServer项目

  参考该地址进行创建:微服务(入门四):identityServer的简单使用(客户端授权)

3.创建ocelotServerTest项目

 3.1创建一个webAPI项目

3.2 修改startUP配置,添加authentication认证

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using IdentityServer4.AccessTokenValidation;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using netCore;
using Ocelot.DependencyInjection;
using Ocelot.Middleware;
using Ocelot.Provider.Consul;
using Ocelot.Provider.Polly;
namespace IdentityServer4Test
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
} public IConfiguration Configuration { get; } // This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
services
.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)//添加认证
.AddIdentityServerAuthentication("TestKey", o =>
{
o.Authority = "http://127.0.0.1:3322";//要认证的服务器地址
o.RequireHttpsMetadata = false;//不启用https
o.ApiName = "api1";//要认证的服务名称
});
services.AddOcelot(Configuration).AddConsul().AddPolly();
} // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseHsts();
}
app.UseMvc(); app.UseOcelot().Wait();
app.UseAuthentication();
}
}
}

3.3创建ocelot.json文件并且添加AuthenticationOptions

 "AuthenticationOptions": {
"AuthenticationProviderKey": "TestKey",
"AllowedScopes": []
}
{
"ReRoutes": [ {
//下游路由模板,真实请求的路径
"DownstreamPathTemplate": "/api/{everything}",
//请求的方式,例如:http,https
"DownstreamScheme": "http",
//服务器名称
"ServiceName": "zyz1",
//启用consul服务
"UseServiceDiscovery": true,
//服务熔断
"QoSOptions": {
"ExceptionsAllowedBeforeBreaking": , //允许多少次异常请求
"DurationOfBreak": , //熔断时间,单位为秒
"TimeoutValue": //如果下游请求的处理时间超过多少则自动设置超时
},
//"RateLimitOptions": {
// "ClientWhitelist": [ "admin" ], // 白名单
// "EnableRateLimiting": true, // 是否启用限流
// "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":
}
],
"UpstreamPathTemplate": "/GetToken",
"UpstreamHttpMethod": [ "Get" ]
}
],
"GlobalConfiguration": {
"BaseUrl": "https://localhost:8596",
//consul服务器地址和ip
"ServiceDiscoveryProvider": {
"Host": "localhost",
"Port":
} }
}

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当中的数据,可正常返回数据

微服务(入门学习五):identityServer4+ocelot+consul实现简单客户端模式的更多相关文章

  1. .Net Core微服务入门全纪录(五)——Ocelot-API网关(下)

    前言 上一篇[.Net Core微服务入门全纪录(四)--Ocelot-API网关(上)]已经完成了Ocelot网关的基本搭建,实现了服务入口的统一.当然,这只是API网关的一个最基本功能,它的进阶功 ...

  2. .Net Core微服务入门全纪录(完结)——Ocelot与Swagger

    Tips:本篇已加入系列文章阅读目录,可点击查看更多相关文章. 前言 上一篇[.Net Core微服务入门全纪录(八)--Docker Compose与容器网络]完成了docker-compose.y ...

  3. .NET Core微服务架构学习与实践系列文章索引目录

    一.为啥要总结和收集这个系列? 今年从原来的Team里面被抽出来加入了新的Team,开始做Java微服务的开发工作,接触了Spring Boot, Spring Cloud等技术栈,对微服务这种架构有 ...

  4. .Net Core微服务入门全纪录(八)——Docker Compose与容器网络

    Tips:本篇已加入系列文章阅读目录,可点击查看更多相关文章. 前言 上一篇[.Net Core微服务入门全纪录(七)--IdentityServer4-授权认证]中使用IdentityServer4 ...

  5. .Net Core微服务入门全纪录(六)——EventBus-事件总线

    前言 上一篇[.Net Core微服务入门全纪录(五)--Ocelot-API网关(下)]中已经完成了Ocelot + Consul的搭建,这一篇简单说一下EventBus. EventBus-事件总 ...

  6. .Net Core微服务入门全纪录(七)——IdentityServer4-授权认证

    前言 上一篇[.Net Core微服务入门全纪录(六)--EventBus-事件总线]中使用CAP完成了一个简单的Eventbus,实现了服务之间的解耦和异步调用,并且做到数据的最终一致性.这一篇将使 ...

  7. .Net Core微服务入门全纪录(四)——Ocelot-API网关(上)

    前言 上一篇[.Net Core微服务入门全纪录(三)--Consul-服务注册与发现(下)]已经使用Consul完成了服务的注册与发现,实际中光有服务注册与发现往往是不够的,我们需要一个统一的入口来 ...

  8. 微服务入门三:SpringCloud Alibaba

    一.什么是SpringCloud Alibaba 1.简介 1)简介 阿里云未分布式应用开发提供了一站式解决方案.它包含了开发分布式应用程序所需的所有组件,使您可以轻松地使用springcloud开发 ...

  9. 微服务实践(五):微服务的事件驱动数据管理 - DockOne.io

    原文:微服务实践(五):微服务的事件驱动数据管理 - DockOne.io [编者的话]本文是使用微服务创建应用系列的第五篇文章.第一篇文章介绍了微服务架构模式,并且讨论了使用微服务的优缺点:第二和第 ...

随机推荐

  1. javaWeb核心技术第四篇之Javascript第二篇事件和正则表达式

    - 事件 - 表单提交(掌握) "onsubmit" - 单击事件(掌握) "onclick" - 页面加载成功事件(掌握) "onload" ...

  2. flex三个对齐属性的记忆方式

    今天在群里聊天有人说 flex的那几个居中属性好难记,时不时都要尝试一下,或者查看一下文档,现在我把我自己的记忆方式分享一下... 1. flex的居中主要是通过这三个属性来实现的: justify- ...

  3. easyui-datagrid 使用笔记

    1.清空所有行和所有列 $('#dgJGQuery').datagrid({ columns: [], url: '', data: [] }); 2.采用 datagrid-groupview 分组 ...

  4. oracle 锁用户

    oracle 线上用户老是被锁: 1,查明所有的用户哪些被锁了 SQL> select username,account_status,lock_date from dba_users; USE ...

  5. 向busybox中添加自己的applet

    关键词:buysbox.applet等. busybox常用于嵌入式环境,集成中断Linux命令和工具.这些工具简单高效. 下面从如下方面了解: 这些命令是一个软链接到busybox,那么是如何从软连 ...

  6. Druid-类图-属性表

    所属文章:池化技术(一)Druid是如何管理数据库连接的? 本篇为「工具人」文章,建议直接用「ctrl+f」进行查找属性.方法.类名,快速了解其含义和所属类. 主要流程里主要涉及到的类名称.类属性.类 ...

  7. Nginx 配置详细文件

    概述 Nginx 是使用一个 master 进程来管理多个 worker 进程提供服务.master 负责管理 worker 进程,而 worker 进程则提供真正的客户服务,worker 进程的数量 ...

  8. python通过重启线程,实现服务的热加载

    这个思路后来证明不能用于工作. 因为线程调用没有及时返回,所以不能用这种方式来重启服务. 但作为脑洞,也应该作个记录. import os import shutil import datetime ...

  9. Java之Scanner类

    Scanner类概述 一个可以解析基本类型和字符串的简单文本扫描器.简而言之,Scanner类的功能:可以实现键盘输入数据,到程序当中. 例如,以下代码使用户能够从 System.in 中读取一个数. ...

  10. django实现多种支付、并发订单处理

    django实现多种支付方式 ''' #思路 我们希望,通过插拔的方式来实现多方式登录,比如新增一种支付方式,那么只要在项目中新增一个py文件,导入里面的pay方法就可以了,这样在支付业务中支付语句是 ...