Viper 微服务框架 编写一个hello world 插件-02
1、Viper是什么?
Viper 是.NET平台下的Anno微服务框架的一个示例项目。入门简单、安全、稳定、高可用、全平台可监控。底层通讯可以随意切换thrift
grpc
。 自带服务发现、调用链追踪、Cron 调度、限流、事件总线、CQRS 、DDD、类似MVC的开发体验,插件化开发
github: https://github.com/duyanming/Viper
文档地址: https://duyanming.github.io/
体验地址: http://140.143.207.244/
如果对Viper不了解可以看:
1、 net core 微服务 快速开发框架 Viper 初体验
2、打不死的小强 .net core 微服务 快速开发框架 Viper 限流
4、hello world .net core 微服务框架 Viper-01
2、Viper微服务编写一个Hello World插件
前面章节我们已经创建好了一个HelloWorldService 服务,这一小节我们来编写一个HelloWorld插件。启动注册中心ViperCenter
修改HelloWorldService
注册中心地址。
2.1 新建一个HelloWorld插件
新建一个HelloWorld功能插件, 稍后我们以同样的方式添加一个SoEasy功能插件。最后我们让两个插件相互调用并输出结果。
添加Anno功能插件依赖
- Install-Package Anno.EngineData -Version 1.0.2.6
增加一个插件启动初始化配置类 HelloWorldBootStrap
- using Anno.EngineData;
- using System;
- namespace Anno.Plugs.HelloWorldService
- {
- /// <summary>
- /// 插件启动引导器
- /// DependsOn 依赖的类型程序集自动注入DI容器
- /// </summary>
- [DependsOn(
- //typeof(Domain.Bootstrap)
- //, typeof(QueryServices.Bootstrap)
- //, typeof(Repository.Bootstrap)
- //, typeof(Command.Handler.Bootstrap
- )]
- public class HelloWorldBootStrap : IPlugsConfigurationBootstrap
- {
- /// <summary>
- /// Service 依赖注入构建之后调用
- /// </summary>
- public void ConfigurationBootstrap()
- {
- //throw new NotImplementedException();
- }
- /// <summary>
- /// Service 依赖注入构建之前调用
- /// </summary>
- /// </summary>
- public void PreConfigurationBootstrap()
- {
- //throw new NotImplementedException();
- }
- }
- }
增加一个业务模块 HelloWorldViperModule
- /******************************************************
- Writer:Du YanMing
- Mail:dym880@163.com
- Create Date:2020/10/30 13:15:24
- Functional description: HelloWorldViperModule
- ******************************************************/
- using System;
- using System.Collections.Generic;
- using System.Text;
- namespace Anno.Plugs.HelloWorldService
- {
- using Anno.Const.Attribute;
- using Anno.EngineData;
- using HelloWorldDto;
- public class HelloWorldViperModule: BaseModule
- {
- [AnnoInfo(Desc = "世界你好啊SayHi")]
- public dynamic SayHello([AnnoInfo(Desc = "称呼")] string name, [AnnoInfo(Desc = "年龄")] int age) {
- Dictionary<string, string> input = new Dictionary<string, string>();
- input.Add("vName",name);
- input.Add("vAge", age.ToString());
- var soEasyMsg = Newtonsoft.Json.JsonConvert.DeserializeObject<ActionResult<string>>(this.InvokeProcessor("Anno.Plugs.SoEasy", "AnnoSoEasy", "SayHi", input)).OutputData;
- return new { HelloWorldViperMsg = $"{name}你好啊,今年{age}岁了", SoEasyMsg= soEasyMsg };
- }
- [AnnoInfo(Desc = "两个整数相减等于几?我来帮你算(x-y=?)")]
- public int Subtraction([AnnoInfo(Desc = "整数X")] int x, [AnnoInfo(Desc = "整数Y")] int y)
- {
- return x - y;
- }
- [AnnoInfo(Desc = "买个商品吧,双十一马上就来了")]
- public ProductDto BuyProduct([AnnoInfo(Desc = "商品名称")] string productName, [AnnoInfo(Desc = "商品数量")] int number)
- {
- double price = new Random().Next(2, 90);
- Dictionary<string, string> input = new Dictionary<string, string>();
- input.Add("productName", productName);
- input.Add("number", number.ToString());
- var product = Newtonsoft.Json.JsonConvert.DeserializeObject<ActionResult<ProductDto>>(this.InvokeProcessor("Anno.Plugs.SoEasy", "AnnoSoEasy", "BuyProduct", input)).OutputData;
- product.CountryOfOrigin = $"中国北京中转--{ product.CountryOfOrigin}";
- return product;
- }
- }
- }
增加一个业务模块 Anno.Plugs.SoEasyService
、AnnoSoEasyModule
- /******************************************************
- Writer:Du YanMing
- Mail:dym880@163.com
- Create Date:2020/10/30 13:16:23
- Functional description: AnnoSoEasyModule
- ******************************************************/
- using Anno.EngineData;
- using System;
- using System.Collections.Generic;
- using System.Text;
- namespace Anno.Plugs.SoEasyService
- {
- using Anno.Const.Attribute;
- using Anno.EngineData;
- using HelloWorldDto;
- public class AnnoSoEasyModule : BaseModule
- {
- [AnnoInfo(Desc = "AnnoSoEasy你好啊SayHi")]
- public dynamic SayHi([AnnoInfo(Desc = "称呼")] string vname, [AnnoInfo(Desc = "年龄")] int vage)
- {
- var msg = string.Empty;
- if (vage < 12)
- {
- msg = "小朋友年纪轻轻就就开始玩变成了啊!加油Baby!";
- }else if (vage < 23)
- {
- msg = "小兄弟,找女朋友了吗?没有的话赶紧找一个吧。别把心思都放在写代码上!";
- }
- else if (vage < 30)
- {
- msg = "兄弟,你家小孩几岁了?开始学编程了吗?";
- }
- else if (vage < 45)
- {
- msg = "大哥,你好能给我介绍个对象吗?";
- }
- else if (vage < 55)
- {
- msg = "大叔,你家邻居有小妹妹介绍吗?";
- }
- else
- {
- msg = "还不退休?别写代码了!";
- }
- return $"{vname}:你好,我是SoEasy,{msg}";
- }
- [AnnoInfo(Desc = "两个整数相加等于几?我来帮你算")]
- public int Add([AnnoInfo(Desc = "整数X")] int x, [AnnoInfo(Desc = "整数Y")] int y)
- {
- return x + y;
- }
- [AnnoInfo(Desc = "买个商品吧,双十一马上就来了")]
- public ProductDto BuyProduct([AnnoInfo(Desc = "商品名称")] string productName, [AnnoInfo(Desc = "商品数量")] int number)
- {
- double price = new Random().Next(2, 90);
- return new ProductDto() { Name=productName,Price=price ,Number=number, CountryOfOrigin="中国台湾"};
- }
- }
- }
至此两个插件已经编写完成,然后在上一章节中新建的 HelloWorldService 服务中引入我们编写的两个功能插件 Anno.Plugs.HelloWorldService、Anno.Plugs.SoEasyService。
启动 HelloWorldService 服务
我们看到服务已经正常启动,且已经成功注册到注册中心。下的章节我们一起来看一下Viper的 Api文档 ,并且增加 全局过滤器、模块过滤器、方法过滤器、异常过滤器、身份验证顾虑器、如何增加请求缓存、单个服务如何限流、单个服务限流和Api网关限流之间的关系。
github: https://github.com/duyanming/Viper
文档地址: https://duyanming.github.io/
体验地址: http://140.143.207.244/