I. Overview
Wooden boat platform is divided into microservices platform and IoT platform, the above articles are introduced how to access the device through the network components, then this article will be detailed in the wooden boat platform how to build microservices.
What is Kayak?
Kayak (Kayak) is based on .NET6.0 software environment surging microservices engine for development, the platform contains microservices and Internet of Things platform. Support for asynchronous and responsive programming development , the function contains a model of things , devices , products , network components of unified management and microservices platform under the registry , service routing , modules , intermediate services and other management. There are also multi-protocol adaptation (TCP, MQTT, UDP, CoAP, HTTP, Grpc, websocket, rtmp, httpflv, webservice, etc.), through a variety of flexible configurations adapted to access different manufacturers of different protocols and other equipment. And through the device alarms, message notification, data visualization and other functions. Enable you to quickly set up a microservice IoT platform system.
So here's how to go from creating components, protocols, and device gateways, devices to device gateway access, and then to device data reporting, putting the whole process through this article.
II. Building services
Create service interface, inherit IServiceKey, add feature [ServiceBundle("api/{Service}/{Method}")] to configure routepath, code as below:
[ServiceBundle("api/{Service}/{Method}")] public interface ITestApiService:IServiceKey { public Task<string> SayHello(string name); }
Create a service instance, inherit ProxyServiceBase, ITestApiService, ISingleInstance, if only business processing only need to inherit ProxyServiceBase, inherit ISingleInstance indicates the injected life cycle For the singleton model, add features ModuleName identifies multiple instances of a service, you can pass in the ServiceKey when calling.
[ModuleName("Test")] public class TestService : ProxyServiceBase, ITestApiService, ISingleInstance { public Task<string> SayHello(string name) { return ($"{name} say:hello world"); } }
II. Identification
Webapi calls will necessarily involve identity authentication, user login issues, and surging has integrated a set of jwt authentication mechanisms
Then configure ApiGetWay on the Stage configuration section
"ApiGetWay": { "AccessTokenExpireTimeSpan": "240", "AuthorizationRoutePath": "api/sysuser/authentication",//Identity Authentication Service routepath "AuthorizationServiceKey": null, "TokenEndpointPath": "api/oauth2/token",//Mapping the routepath of a call "CacheMode": "MemoryCache" //MemoryCache or save token }
Then add the [Authorization(AuthType = )] feature to the interface method, and the service call is authenticated.
public interface IModuleService : IServiceKey { [Authorization(AuthType = )] Task<ApiResult<bool>> Add(ModuleModel model); [Authorization(AuthType = )] Task<ApiResult<bool>> Modify(ModuleModel model); [Authorization(AuthType = )] Task<ApiResult<Page<ModuleModel>>> GetPageAsync(ModuleQuery query); }
III. Cache Interception
surging can support intercepting caches, which can be done via theThe ServiceCacheIntercept feature is configured so that getting the cache can be done via theThe cache can be deleted with theIt can support MemoryCache, Redis, one or two level cache.
Enabling EnableStageCache indicates that gateway calls can also go for cache interception (note: model parameters are not supported)
[ServiceBundle("api/{Service}/{Method}")] public interface IProductService : IServiceKey { [Authorization(AuthType = )] [ServiceCacheIntercept(, "GetProducts", CacheSectionType = "ddlCache", Mode = , EnableStageCache = true)] Task<ApiResult<bool>> Add(ProductModel model); [Authorization(AuthType = )] Task<ApiResult<ProductModel>> GetProduct(int id); [Authorization(AuthType = )] Task<ApiResult<Page<ProductModel>>> GetPageAsync(ProductQuery query); [Authorization(AuthType = )] Task<ApiResult<List<ProductModel>>> GetProductByCondition(ProductQuery query); [Authorization(AuthType = )] [ServiceCacheIntercept(, "GetProducts", CacheSectionType = "ddlCache", Mode = , EnableStageCache = true)] [ServiceLogIntercept] Task<ApiResult<bool>> DeleteById(List<int> ids); [Authorization(AuthType = )] [ServiceCacheIntercept(, "GetProducts", CacheSectionType = "ddlCache", Mode = , EnableStageCache = true)] Task<ApiResult<bool>> Modify(ProductModel model); [Authorization(AuthType = )] Task<ApiResult<bool>> Validate(ProductModel model); [Authorization(AuthType = )] [ServiceCacheIntercept(, "GetProducts", CacheSectionType = "ddlCache", Mode = , EnableStageCache = true)] Task<ApiResult<bool>> Stop(List<int> ids); [Authorization(AuthType = )] [ServiceCacheIntercept(, "GetProducts", CacheSectionType = "ddlCache", Mode = , EnableStageCache = true)] Task<ApiResult<bool>> Open(List<int> ids); [Authorization(AuthType = )] [ServiceCacheIntercept(, Key = "GetProducts", CacheSectionType = "ddlCache", EnableL2Cache = false, Mode = , Time = 480, EnableStageCache = true)] Task<ApiResult<List<ProductModel>>> GetProducts(); }
If the parameter is a non-model collection type parameter, the cache key will take the first parameter value, if it is a model parameter, you need to add the CacheKey feature, the code is as follows:
public class PropertyThresholdQuery { [CacheKey(1)] public string PropertyCode { get; set; } [CacheKey(2)] public string ProductCode { get; set; } [CacheKey(3)] public string DeviceCode { get; set; } }
IV. Service management
1. platform is to support service routing management, this feature in addition to viewing metadata, service nodes, service rules, but also in the weight polling load algorithm, change the weight to allow more access to this service node call, there can be elegantly removed service nodes
Select the weight polling load diversion algorithm with the following code:
[ServiceBundle("api/{Service}/{Method}")] public interface ITestApiService:IServiceKey { // [Authorization(AuthType = )] [Command(ShuntStrategy =)] public Task<string> SayHello(string name); }
Here are the editorial weights
2. Hot deployment of intermediate services
3. Black and white lists, adding IP addresses or IP segments to restrict access to the relevant IPs
For example, if you visit api/testapi, the result will be as follows:
4. Support swagger API documentation
V. Distributed link tracing
Support skywalking distributed link tracing
VII. Summary
Above is how the wooden boat platform to build services, the platform is scheduled to be released on November 20, 1.0 community version. Please also pay attention to the scene at that time.