一、概述

木舟平台分为微服务平台和物联网平台, 上面几篇都是介绍如何通过网络组件接入设备,那么此篇文章就细致介绍下在木舟平台下如何构建微服务。

木舟 (Kayak) 是什么?

木舟(Kayak)是基于.NET6.0软件环境下的surging微服务引擎进行开发的, 平台包含了微服务和物联网平台。支持异步和响应式编程开发,功能包含了物模型,设备,产品,网络组件的统一管理和微服务平台下的注册中心,服务路由,模块,中间服务等管理。还有多协议适配(TCP,MQTT,UDP,CoAP,HTTP,Grpc,websocket,rtmp,httpflv,webservice,等),通过灵活多样的配置适配能够接入不同厂家不同协议等设备。并且通过设备告警,消息通知,数据可视化等功能。能够让你能快速建立起微服务物联网平台系统。

那么下面就为大家介绍如何从创建组件、协议、设备网关,设备到设备网关接入,再到设备数据上报,把整个流程通过此篇文章进行阐述。

二、构建服务

创建服务接口,继承IServiceKey,添加特性[ServiceBundle("api/{Service}/{Method}")] 配置routepath,代码如下:

   [ServiceBundle("api/{Service}/{Method}")]
public interface ITestApiService:IServiceKey
{
public Task<string> SayHello(string name);
}

创建服务实例,继承ProxyServiceBase, ITestApiService, ISingleInstance,如果只是业务处理只需继承ProxyServiceBase,继承ISingleInstance表示注入的生命周期 为单例模式,添加特性ModuleName标识一个服务多个实例,可以在调用的时候传入ServiceKey

    [ModuleName("Test")]
public class TestService : ProxyServiceBase, ITestApiService, ISingleInstance
{
public Task<string> SayHello(string name)
{
return Task.FromResult($"{name} say:hello world");
}
}

二、身份鉴权

webapi调用必然会牵涉到身份鉴权,用户登录问题,而surging 已经集成了一套jwt验证机制

然后在Stage配置节上配置ApiGetWay

   "ApiGetWay": {
"AccessTokenExpireTimeSpan": "240",
"AuthorizationRoutePath": "api/sysuser/authentication",//身份鉴权服务的routepath
"AuthorizationServiceKey": null,
"TokenEndpointPath": "api/oauth2/token",//映射调用的routepath
"CacheMode": "MemoryCache" //MemoryCache or gateway.Redis save token
}

然后在接口方法上加上  [Authorization(AuthType = AuthorizationType.JWT)] 特性,服务调用就要进行身份鉴权

    public interface IModuleService : IServiceKey
{
[Authorization(AuthType = AuthorizationType.JWT)]
Task<ApiResult<bool>> Add(ModuleModel model); [Authorization(AuthType = AuthorizationType.JWT)]
Task<ApiResult<bool>> Modify(ModuleModel model); [Authorization(AuthType = AuthorizationType.JWT)]
Task<ApiResult<Page<ModuleModel>>> GetPageAsync(ModuleQuery query); }

三、缓存拦截

surging 可以支持拦截缓存,可以通过ServiceCacheIntercept特性进行配置,获取缓存可以通过CachingMethod.Get, 删除缓存可以通过CachingMethod.Remove,可以支持MemoryCache,Redis, 可以支持一,二级缓存,

启用EnableStageCache表示网关调用也可以走缓存拦截(注:不支持模型参数)

 [ServiceBundle("api/{Service}/{Method}")]
public interface IProductService : IServiceKey
{
[Authorization(AuthType = AuthorizationType.JWT)]
[ServiceCacheIntercept(CachingMethod.Remove, "GetProducts", CacheSectionType = "ddlCache", Mode = CacheTargetType.MemoryCache, EnableStageCache = true)]
Task<ApiResult<bool>> Add(ProductModel model); [Authorization(AuthType = AuthorizationType.JWT)]
Task<ApiResult<ProductModel>> GetProduct(int id); [Authorization(AuthType = AuthorizationType.JWT)]
Task<ApiResult<Page<ProductModel>>> GetPageAsync(ProductQuery query); [Authorization(AuthType = AuthorizationType.JWT)]
Task<ApiResult<List<ProductModel>>> GetProductByCondition(ProductQuery query); [Authorization(AuthType = AuthorizationType.JWT)]
[ServiceCacheIntercept(CachingMethod.Remove, "GetProducts", CacheSectionType = "ddlCache", Mode = CacheTargetType.MemoryCache, EnableStageCache = true)]
[ServiceLogIntercept]
Task<ApiResult<bool>> DeleteById(List<int> ids); [Authorization(AuthType = AuthorizationType.JWT)]
[ServiceCacheIntercept(CachingMethod.Remove, "GetProducts", CacheSectionType = "ddlCache", Mode = CacheTargetType.MemoryCache, EnableStageCache = true)]
Task<ApiResult<bool>> Modify(ProductModel model); [Authorization(AuthType = AuthorizationType.JWT)]
Task<ApiResult<bool>> Validate(ProductModel model); [Authorization(AuthType = AuthorizationType.JWT)]
[ServiceCacheIntercept(CachingMethod.Remove, "GetProducts", CacheSectionType = "ddlCache", Mode = CacheTargetType.MemoryCache, EnableStageCache = true)]
Task<ApiResult<bool>> Stop(List<int> ids); [Authorization(AuthType = AuthorizationType.JWT)]
[ServiceCacheIntercept(CachingMethod.Remove, "GetProducts", CacheSectionType = "ddlCache", Mode = CacheTargetType.MemoryCache, EnableStageCache = true)]
Task<ApiResult<bool>> Open(List<int> ids); [Authorization(AuthType = AuthorizationType.JWT)]
[ServiceCacheIntercept(CachingMethod.Get, Key = "GetProducts", CacheSectionType = "ddlCache", EnableL2Cache = false, Mode = CacheTargetType.MemoryCache, Time = 480, EnableStageCache = true)]
Task<ApiResult<List<ProductModel>>> GetProducts(); }

参数如果是非模型集合类型的参数,缓存key 会取第一个参数值,如果是模型参数就需要添加CacheKey特性,代码如下:

    public class PropertyThresholdQuery
{
[CacheKey(1)]
public string PropertyCode { get; set; }
[CacheKey(2)]
public string ProductCode { get; set; }
[CacheKey(3)]
public string DeviceCode { get; set; }
}

四、服务管理

1.平台是支持服务路由管理,此项功能除了可以查看元数据,服务节点,服务规则外,还可以在权重轮询负载算法情况下,改变权重可以让更多的访问调用到此服务节点上,还有可以优雅的移除服务节点

选择权重轮询负载分流算法,代码如下:

    [ServiceBundle("api/{Service}/{Method}")]
public interface ITestApiService:IServiceKey
{
// [Authorization(AuthType = AuthorizationType.JWT)]
[Command(ShuntStrategy =AddressSelectorMode.RoundRobin)]
public Task<string> SayHello(string name);
}

以下是编辑权重

2. 热部署中间服务

3. 黑白名单,添加IP地址或者IP段就能限制相关IP访问

就比如访问api/testapi,结果如下:

4.支持swagger API文档

五、分布式链路追踪

支持skywalking 分布式链路追踪

七、总结

以上是木舟平台如何构建服务,平台定于11月20日发布1.0社区版本。也请大家到时候关注捧场。

基于surging的木舟平台如何构建起微服务的更多相关文章

  1. Docker从入门到掉坑(二):基于Docker构建SpringBoot微服务

    本篇为Docker从入门到掉坑第二篇:基于Docker构建SpringBoot微服务,没有看过上一篇的最好读过 Docker 从入门到掉坑 之后,阅读本篇. 在之前的文章里面介绍了如何基于docker ...

  2. 微服务架构:构建PHP微服务生态

    微服务架构:构建PHP微服务生态   Linux系统技术交流QQ群(1675603)验证问题答案:刘遄 导读 诞生于 2014 年的“微服务架构”,其思想经由 Martin Fowler 阐述后,在近 ...

  3. Spring Cloud与微服务构建:微服务简介

    Spring Cloud与微服务构建:微服务简介 单体架构及其不足 1.单体架构简介 在软件设计中,经常提及和使用经典的3曾模型,即表示层.业务逻辑层和数据访问层. 表示层:用于直接和用户交互,也成为 ...

  4. Java生鲜电商平台-生鲜系统中微服务架构设计与分析实战

    Java生鲜电商平台-生鲜系统中微服务架构设计与分析实战 说明: Java生鲜系统中微服务的拆分应该如何架构设计与分析呢?以下是我的实战中的设计与经验分析. 目录 1. 微服务简介2. 当前现状3. ...

  5. 手把手0基础项目实战(一)——教你搭建一套可自动化构建的微服务框架(SpringBoot+Dubbo+Docker+Jenkins)...

    原文:手把手0基础项目实战(一)--教你搭建一套可自动化构建的微服务框架(SpringBoot+Dubbo+Docker+Jenkins)... 本文你将学到什么? 本文将以原理+实战的方式,首先对& ...

  6. 使用http://start.spring.io/构建maven微服务项目的几个坑及eclipse构建spring boot微服务项目

    一,使用http://start.spring.io/构建maven微服务项目 本来嘛,直接构建的项目导入时没有任何问题的导入就可以运行,可是最近构建好项目,然后导入,种种报错 1.导入之后POM报错 ...

  7. 微服务架构:基于微服务和Docker容器技术的PaaS云平台架构设计(微服务架构实施原理)

    版权声明:本文为博主原创文章,转载请注明出处,欢迎交流学习! 基于微服务架构和Docker容器技术的PaaS云平台建设目标是给我们的开发人员提供一套服务快速开发.部署.运维管理.持续开发持续集成的流程 ...

  8. 基于Spring Boot、Spring Cloud、Docker的微服务系统架构实践

    由于最近公司业务需要,需要搭建基于Spring Cloud的微服务系统.遍访各大搜索引擎,发现国内资料少之又少,也难怪,国内Dubbo正统治着天下.但是,一个技术总有它的瓶颈,Dubbo也有它捉襟见肘 ...

  9. 基于spring boot2.0+spring security +oauth2.0+ jwt微服务架构

    github地址:https://github.com/hankuikuide/microservice-spring-security-oauth2 项目介绍 该项目是一个演示项目,主要演示了,基于 ...

  10. Docker1.12 + Swarm 构建动态微服务应用

    导读 我们在之前提到过一个示例,即一款由前端与多项后端服务共同构成的微服务应用.其中前端为Traefik HTTP代理,负责将各项请求路由至后端服务.而后端则非常简单,是一套基于Go的HTTP Web ...

随机推荐

  1. C语言编程-GCC编译过程

    gcc编译 预处理 ->编译->汇编->链接 预处理 gcc -E helloworld.c -o helloworld.i 头文件展开:不检查语法错误,即可以展开任意文件: 宏定义 ...

  2. Linux——添加默认路由(能ping通本网段,但是ping不通其他网段)

    2024/07/15 1.问题描述 2.问题处理 3.其他问题 1.问题描述 昨天服务器突然断电,今天重启后,网络出了些问题,具体情况如下: 能ping通本机IP ping不通网关 ping不通本网段 ...

  3. 再探se

    对象 没有分配内存空间的对象是一个特殊的对象 null null是引用类型的,但是没有指向任何位置,所以是不能被访问的,强制访问会空指针异常 针对具体对象的属性称之为对象属性,成员属性,实例属性 针对 ...

  4. Windows添加软件开机自启动

    两种方式 1.添加快捷方式到开始菜单 打开我的电脑找到C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup 文件夹, 如果难找的话可 ...

  5. .NET Core3.1 跨域 Cors 找不到 “Access-Control-Allow-Origin”

    今天在做项目的时候遇到了调用WebAPI跨域的问题 No 'Access-Control-Allow-Origin' header is present on the requested resour ...

  6. Windows 设置 FRP 自动启动

    由于 frps/frpc 不是 Windows 服务应用程序,因此我们不能直接使用 New-Service 命令创建 frps/frpc 服务.我们可以使用下面的方法将 frps/frpc 封装为 W ...

  7. c++11 动态内存与智能指针详解

    c++ 动态内存与智能指针详解 一. 动态内存 (一)程序对象的生存期 全局对象在程序启动时分配,在程序结束时销毁. 对于局部自动对象,当我们进入其定义所在的程序块时被创建,在离开块时销毁. 局部st ...

  8. 画流程图、状态图、时序图、甘特图的JS库-mermaid-js

    参考地址:https://github.com/mermaid-js/mermaid 原生使用方式: <!DOCTYPE html> <html> <head> & ...

  9. SQL Server – Transaction & Isolation 事务与隔离

    前言 上回在谈到 Concurrency 并发控制 时, 有提到过事务的概念. 这篇就补上它具体的实现. 以前写过相关的文章: sql server 学习笔记 (nested transaction ...

  10. @RequestBody注意事项

    使用@RequestBody注解时,是用于接收Content-Type为application/json类型的请求,数据类型是JSON:{"aaa":"111" ...