河南创志威科技有限公司
支持.net core 2.x-7.x,正常使用
支持linux系统,centos,radhat,麒麟,统信,鲲鹏,X86_64,等系统,经过测试互认证。查看证书
业务开发业务逻辑API类时, 搜索nuget包,wRPCService。执行业务逻辑,搜索WeaveRemoteService包
WINX64 运行包 weave_win_1.0.0-1.X64.zip
RPM包下载
登录认证中心RPM包下载
主要目的,尽量简化和减少开发复杂度和难度,微服务由4部分组成。
注册分发中心,集成网关,认证中心,熔断机制,选举机制,架构实现了RPC相关功能。通信协议基于weaving-socket
安装 windows 直接运行发布后,从发布目录中拷贝。 linux 系统: 根据不通CPU选择不同的rpm包下载,
使用 yum install WeaveMicro--.*.rpm(WeaveMicro-1-1.arm64.rpm 或 WeaveMicro-1.0.0-1.aarch64.rpm 或 WeaveMicro-1.0.0-1.x86_64.rpm)进行安装。
设置服务随linux后台启动运行教程 linux服务设置教程
chmod 777 /microcenter/WeaveMicrocenter
然后在执行 /microcenter/WeaveMicrocenter
可以修改目录下config.json文件更改端口和API查询地址 {
"port": 9001,
"url": "http://*:5022"
}
API文档使用
找到config.json文件 修改配置
{
"port": 9001,
"url": "http://*:5022",
"SwaggerPath": "swagger"
}
可使用SwaggerPath配置文档查看地址,默认为根目录(无此配置或设置为空字符串同等效果),如不启用API文档,修改配置如下
{
"port": 9001,
"url": "http://*:5022",
"SwaggerPath": null
}
服务中心默认启用文件浏览功能(根目录),并可增加指定目录的静态文件服务。增加以下配置可自定义静态文件服务。
其中,MIME信息可自定义支持的文件类型,如需删除某一类型文件支持,将其设置为null,以下配置表示增加apk文件及取消dll文件支持。
{
//静态浏览目录
"StaticFilePath": "doc",
//静态浏览请求地址映射,以/开头
"StaticFileRequestPath": "/",
//MIME信息支持
"MIME": {
".apk": "application/vnd.android.package-archive",
".dll": null,
}
}
chmod 777 /gateway/gateway
然后在执行 /gateway/gateway
可以修改目录下config.json文件更改端口和服务中心地址,认证中心注册地址,本地网关找到config.json文件 修改配置{
"Authentication": true,//开启登录认证
"IdentityServer": "http://10.1.65.226",//登录认证地址
"Audience": "ac-cloud",
"defaultScheme": "Bearer",
"applicationUrl": "http://*:5221",//网关请求地址
//网关请求地址支持IP写为*,但必须增加配置bindIP告知服务中心网关实际请求地址,用于在复杂环境下的部署
"bindIP": "127.0.0.1",
"Microcenter": "127.0.0.1:9002",//注册中心
"filetype": ".jpg,.png,.doc,.txt",//指定可上传文件的后缀
"httpspassword": "linezero",//https证书密码
"Cookies": [ "Role", "UserId", "Name", "Phone" ],//开启认证后,token中所包含的指定信息,可明文带入远程服务中
"Headers": [ "token" ],//http指定头部内容可以带入,远程服务中
//设置不允许的HTTP谓词,如TRACE
"HttpDisableMethods": [ "Trace", "Debug" ],
//添加额外的头部
"AddHeaders": {
"Referrer-Policy": "no-referrer",
"X-Content-Type-Options": "nosniff",
"X-Download-Options": "noopen",
"X-Permitted-Cross-Domain-Policies": "master-only",
"X-Frame-Options": "sameorigin",
"X-XSS-Protection": "1; mode=block"
},
}
网关默认启用文件浏览功能(www=>/Static),并可修改指定目录的静态文件服务。增加以下配置可自定义静态文件服务。
其中,MIME信息可自定义支持的文件类型,如需删除某一类型文件支持,将其设置为null,以下配置表示增加apk文件及取消dll文件支持。
{
//静态浏览目录
"StaticFilePath": "www",
//静态浏览请求地址映射,以/开头
"StaticFileRequestPath": "/Static",
//MIME信息支持
"MIME": {
".apk": "application/vnd.android.package-archive",
".dll": null,
}
}
下载对应的打包发布程序您的服务器上 登录认证中心RPM包发行与使用说明
之前版本认证中心需要重新编译内容,自己提供实现,目前可通过接口继承进行实现。简化编写方法 新建类库 ,nuget 搜索 WeaveVerify,继承 IdentityBase,实现内部方法
public class Verifyabc : IdentityBase
{
public override string PrjName { get; set; } = "abc";//项目名称
public override Verifymode attestation(string Loginname, string Password)
{
Verifymode vm = new Verifymode();
if (true)
{ //认证成功赋值内容
vm.Verify = true;
vm.Claims = new Claim[] {
new Claim("UserId", "123"),
new Claim("Name", "admin"),
new Claim("GivenName", "sdfq") } ;
}
else
{
//认证失败提示错误内容
vm.Verify = false;
vm.ERRMessage = "XXX错误~!";
}
return vm;
}
}
为了保证认证逻辑插件的完整性,认证逻辑插件项目需要非标准库(如net5.0\net6.0)且添加true节
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<GenerateDocumentationFile>True</GenerateDocumentationFile>
<EnableDynamicLoading>true</EnableDynamicLoading>
</PropertyGroup>
每个认证逻辑部署时按每个project一个子文件夹。将生成的可执行文件复制到文件夹内即可。如\Plugins\KFQX\,目录结构如下:
认证服务主程序根路径
Plugins(插件总目录)
KFQX(插件目录)
runtimes/
IdentityServer_Verify.deps.json
IdentityServer_Verify.dll
dataconfig.json
LNQX
...
对于各插件,AppDomain.CurrentDomain.BaseDirectory依然是主程序根目录,因此如需要读取插件目录下的其他文件, 应将AppDomain.CurrentDomain.BaseDirectory 替换为 Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)
测试提交参数
{
RemoteService remoteService = new RemoteService("TEST");//TEST 为你注册的API名称
remoteService.Start();
}
修改配置文件
{
"ServerIP": "127.0.0.1",//本机需要绑定的IP
"Port": 10098,//需要绑定的端口用于网关调用
"Microcenter": "127.0.0.1:8001"//服务中心的IP和端口
}
- 新建CLASS继承 : FunctionBase 基类
- 基类介绍,FunctionBase 包含 Cookies,和 Headers 两个属性,Headers为http请求头可以被带入的内容,Cookies 为开启认证后,登录的token中所包含的指定信息,可明文带入远程服务中。
- 标识属性 Route , [Route("abcd")]放在类上面,标识次类的API路由地址,
- 标识属性 InstallFun , [InstallFun(FunAttribute.NONE, "此方法用于测试")] ,参数1:FunAttribute.NONE,标识此方法接收POST 传入的 json类型对象。参数2:用于API测试中的注释说明。FunAttribute { NONE,Get,POST ,file} 包含四种请求类型
- 统一响应返回格式 IApiResult接口,(可选)如果需要统一API响应格式,可通过继承IApiResult接口,定义返回格式。系统会自动获取T的类型并展示到Swagger文档中。
业务方法可以写成这样 `public async Task<IApiResult> Update(T_DisasterCase o)`
- async,支持方法类型为异步响应async。
[Route("abcd")]//路由地址
public class Class2: FunctionBase//FunctionBase可以继承,也可以不继承,方便以后扩展功能
{
[InstallFun(FunAttribute.NONE, "此方法用于测试")]//指定方法为远程方法,
//FunAttribute { NONE,Get,POST ,file} 包含四种请求类型,为方法写注释
public String ff(mode md)
{
// Console.WriteLine(md.name);
return "Class2.ff的返回值";
}
[Authorize]//此处表明此方法需要权限验证
[InstallFun(FunAttribute.NONE, "此方法用于测试")]
public String ff2([Param("用户名")] string name)//[Param("用户名")] 为每个参数写注释
{
/// Console.WriteLine(name);
return "Class2.f2f22的返回值";
}
}
[InstallFun(FunAttribute.file, "此方法用于测试")]
public String ff()//接收文件并保存
{
object obj= this.Cookies; object obj2 = this.Headers;
System.IO.FileStream streamWriter = new System.IO.FileStream(this.Filedata.filename,System.IO.FileMode.Create);
streamWriter.Write(this.Filedata.data,0, this.Filedata.data.Length);
streamWriter.Close();
return this.Filedata.filename;
}
(可选)如果需要统一API响应格式,可通过继承IApiResult接口,定义返回格式。系统会自动获取T的类型并展示到Swagger文档中 在wRPCService v1.0.15及之后版本中,内置了AOP特性标签UnifyResult实现统一回复格式,但此种方法不支持自动获取统一格式并展示到Swagger文档中。请参看“使用统一的回复数据规则-并简化使用方法”
/// <summary>
/// Api返回结果
/// </summary>
public class ApiResult<T> : IApiResult
{
/// <summary>
/// 状态码:200成功500异常0操作失败
/// </summary>
public int code { get; set; }
/// <summary>
/// 状态描述
/// </summary>
public string message { get; set; }
/// <summary>
/// 返回数据
/// </summary>
public T data { get; set; }
/// <summary>
/// 异常数据
/// </summary>
public dynamic errData { get; set; }
}
//API签名可以这样写(wRPCService v1.0.8 以上版本)
public async Task<IApiResult> Update(T_DisasterCase o)...
[InstallFun(FunAttribute.Get, "此方法用于测试")]//指定方法为远程方法,
//FunAttribute { NONE,Get,POST ,file} 包含四种请求类型,为方法写注释
public String ff(String md)
{
// Console.WriteLine(md);
return "Class2.ff的返回值";
}
[InstallFun(FunAttribute.POST, "此方法用于测试")]//指定方法为远程方法,
//FunAttribute { NONE,Get,POST ,file} 包含四种请求类型,为方法写注释
public String ff(String md,int a)
{
// Console.WriteLine(md);
return "Class2.ff的返回值";
}
[InstallFun(FunAttribute.NONE, "此方法用于测试")]//指定方法为远程方法,
//FunAttribute { NONE,Get,POST ,file} 包含四种请求类型,为方法写注释
public String ff(mode md)//只能传入一个对象
{
// Console.WriteLine(md.name);
return "Class2.ff的返回值";
}
[InstallFun(FunAttribute.file, "此方法用于测试")]
public String ff(string name)//接收文件并保存
{
object obj= this.Cookies; object obj2 = this.Headers;
System.IO.FileStream streamWriter = new System.IO.FileStream(this.Filedata.filename,System.IO.FileMode.Create);
streamWriter.Write(this.Filedata.data,0, this.Filedata.data.Length);
streamWriter.Close();
return this.Filedata.filename;
}
public async Task<String> ff()
{
object obj= this.Cookies; object obj2 = this.Headers;
await Task.Delay(1000);
return await Task.Run(() => { ; return "aaaaaa"; } ) ;
}
流式返回可以在数据量较大的时候,数据快速响应到页面
[InstallFun(FunAttribute.Get, "测试流传输")]
public void ff22()
{
int i = 0;
DateTime dt = DateTime.Now;
while (true)
{ i++;
this.PushStream("Weave微服务架构,是.net core下开发的由分发网关,服务中心,认证中心,服务API 组成,具有多负载分布式特点");
if (i > 20000)
break;
}
DateTime dt2 = DateTime.Now;
Console.WriteLine("ff22:"+(dt2-dt).TotalMilliseconds);
}
[InstallFun(FunAttribute.Get, "测试流传输", "application/octet-stream")]
public void ffs22()
{
DateTime dt = DateTime.Now;
int i = 0;
while (true)
{
i++;
this.PushStream(System.Text.UTF8Encoding.UTF8.GetBytes("Weave微服务架构,是.net core下开发的由分发网关,服务中心,认证中心,服务API 组成,具有多负载分布式特点"));
if (i > 100000)
break;
}
DateTime dt2 = DateTime.Now;
Console.WriteLine("ffs22:" + (dt2 - dt).TotalMilliseconds);
/// Console.WriteLine(name);
}
首先需要在gateway网关中修改配置文件,
"Headers": [ "token" ]//添加需要获取到的HTTP header
[InstallFun(FunAttribute.Get, "测试")]
public string ff22()
{
var value = this.Headers["对应KEY"];
return "";
}
认证中心需要返回对应的资料内容如下:
context.Result = new GrantValidationResult(
subject: context.UserName,
authenticationMethod: "custom",
claims: new Claim[] {
new Claim("UserId", "ceshi123"),
new Claim("Name","ceshi"),
new Claim("Phone", "135135"),
new Claim("Role","admin"),
new Claim("Area","41")
});
然后需要在gateway网关中修改配置文件,
"Cookies": [ "Areacode", "Role", "UserId", "Name", "Phone" ]
[InstallFun(FunAttribute.Get, "测试")]
public string ff22()
{
var value = this.Cookies["对应KEY"];
return "";
}
[Route("api/test")]
public class testclass : ApiBase<object>//继承ApiBase<object>基类
{
public async Task<ApiResult<String>> test()
{
return await TRY(async () =>
{
// 写自己的方法内容
return "";
});
}
public async Task<IApiResult> testapi()
{
return await TRY(async () =>
{
// 写自己的方法内容
return "";
});
}
}
在wRPCService v1.0.15及之后版本中,内置了AOP特性标签。可以使用以下方式实现统一回复格式(不支持自动获取统一格式并展示到Swagger文档中)
使用方法为:给类或者方法贴上UnifyResult特性标签即可,如果有某个方法不使用统一回复格式则贴上[UnifyResult(DontUse = true)]特性标签。
[Route("api/test")]
[UnifyResult]
public class testclass : ApiBase<object>//继承ApiBase<object>基类
{
[UnifyResult]
public async Task<string> test()
{
// 写自己的方法内容
return "";
}
[UnifyResult(DontUse = true)]//原样返回
public async Task<string> testapi()
{
// 写自己的方法内容
return "";
}
}
你也可以实现自己的统一回复格式(不支持自动获取统一格式并展示到Swagger文档中)。
以下是UnifyResult特性标签的代码,你可以自己实现一个,只需要继承AopAttribute就好了:
/// <summary>
/// 统一返回结果,你也可以自定义实现一个,也可以继续使用TRY
/// </summary>
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = false)]
public class UnifyResultAttribute : AopAttribute
{
public bool DontUse { get; set; } = false;
/// <summary>
/// Try放到第一位
/// </summary>
public override int Order { get; set; } = int.MinValue;
public override bool OnActionExecuted(AopContext context)
{
if (DontUse) return true;
var o = new ApiResult<object>() { code = 200, message = "操作成功" };
if (context.Exception is null)
{
o.data = context.Result;
}
else
{
context.Exception = context.Exception.GetBaseException();
//异步改同步在这里判断异常类型
if (context.Exception is ApiException e)
{
o.code = 0;
o.message = $"操作失败:{e.Message}";
o.errData = e.data;
}
else
{
o.code = 500;
o.message = $"接口调用异常:{context.Exception.Message}";
o.errData = context.Exception.StackTrace;
}
}
context.Result = o;
return base.OnActionExecuted(context);
}
}
在wRPCService v1.0.15及之后版本中,增加了基于Microsoft.Extensions.Caching.Memory的内存缓存机制。采用特性标签的形式实现。
[Route("api/test")]
[MemoryCache(AbsoluteExpiration =3600,SlidingExpiration =60)]
public class testclass : ApiBase<object>//继承ApiBase<object>基类
{
[MemoryCache(AbsoluteExpiration =600,SlidingExpiration =-1,Key ="键名", DependCache=new string[] { "其他缓存键"})]
public async Task<string> test()
{
// 写自己的方法内容
return "";
}
public async Task<string> testapi()
{
// 写自己的方法内容
return "";
}
}
特性的各参数如下:
在wRPCService v1.0.15及之后版本中,增加了AOP特性处理机制,你可以实现自己的AOP组件,例如缓存、统一处理、日志等。实现方式如下:
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = false)]
public class MemoryCacheAttribute : AopAttribute
{
}
/// <summary>
/// 排序,越小越外侧运行。
/// </summary>
public virtual int Order { get; set; } = 100;
/// <summary>
/// 在Action执行之前
/// </summary>
/// <param name="context">上下文</param>
/// <returns>是否继续执行,默认true</returns>
public virtual bool OnActionExecuting(AopContext context) => true;
/// <summary>
/// 在Action执行之后
/// </summary>
/// <param name="context">上下文</param>
/// <returns>是否继续执行,默认true</returns>
public virtual bool OnActionExecuted(AopContext context) => true;
无验证的调用
有验证的调用
wRPCclient.ClientChannel clientChannel = new wRPCclient.ClientChannel("127.0.0.1", 10098); //连接API服务的地址
String retun = clientChannel.Call<String>("api/abcd", "ff2","asdasd");//"api/abcd" 路由地址,ff2,调用的方法名称,"asdasd" 传入的参数,有几个就写几个,多个参数,可以写多个如,Call<String>("路由名", "方法名","参数1","参数2");
//Call<String> 为返回的值类型
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。
1. 开源生态
2. 协作、人、软件
3. 评估模型