Based on the. net core microservice framework and the. netcore framework
1Preface
Surging has suffered a lot. I also raised a lot of questions regarding the favor of the net students. The most important question is when to integrate API gateway. Here, I will answer the question that we have recently started research and development, there should be an API gateway of the first version within one or two months. Other functions such as Token Authentication and throttling and degradation will be pushed back.
A new version of surging has also been updated recently.
Update content:
1. the Cache middleware has converted servicestack. Redis to stackexchange based on the third-party library on which redis depends.
2. Increase cache degradation
3. added an example of blocking cache degradation.
Open Source Address: https://github.com/dotnetcore/surging
2
,Use of Cache Middleware
This article mentioned the concept of surging architecture. We also have a preliminary impression on the Cache middleware. In this section, we will talk about how to use the Cache middleware.
1. Create a configuration file
CacheSettings. json
{ "CachingSettings": [ { "Id": "ddlCache", "Class": "Surging.Core.Caching.RedisCache.RedisContext,Surging.Core.Caching", "Properties": [ { "Name": "appRuleFile", "Ref": "rule" }, { "Name": "dataContextPool", "Ref": "ddls_sample", "Maps": [ { "Name": "Redis", "Properties": [ { "value": "127.0.0.1:6379::1" } ] }, { "Name": "MemoryCache" } ] }, { "Name": "defaultExpireTime", "value": "120" }, { "Name": "connectTimeout", "Value": "120" }, { "Name": "minSize", "Value": "1" }, { "Name": "maxSize", "Value": "10" } ] } ]}
You can configure multiple instances to identify unique IDs, configure Maps to initialize the service node list, and use Hash consistency to select service nodes.
Configuration Parameter List
Parameters |
Function |
CachingSettings |
Parent configuration section that contains multiple instances |
Id |
Unique Identifier |
Class |
Adaptation to Context |
Properties |
Cache-related configuration section |
Maps |
Configure Service Section list |
MinSize |
Minimum number of object pools |
MaxSize |
Maximum number of objectpools |
2. Code Configuration
/// <Summary> /// configure the cache service /// </summary> public static void ConfigureCache (IConfigurationBuilder build) {build. addCacheFile ("cacheSettings. json ", optional: false );}
The above completes the cache middleware configuration, and then you can use the following code to call
Redis-based Cache call
cacheProvider = CacheContainer.GetInstances<ICacheProvider>(“ddlCache.Redis”);
MemoryCache-based Cache call
CacheContainer.GetInstances<ICacheProvider>("MemoryCache")
3
,Cache downgrade
Some data returned by the service does not need to be modified frequently. data can be returned through the cache, such as the product application, product category, product list, user information, order information, and shopping cart, through the above requirements, we need to intercept the proxy, return the cache or delete the cache.
Questions about cache
Cache downgrade
The proxy is used to intercept the surging cache. If there is cached data, the cache data is returned. Otherwise, the Proceed method is called to call the remote service.
When data is updated and deleted, the dependent cache needs to be deleted. You can use the proxy to intercept the cache.
Cache downgrade has the following advantages:
1. High Performance: reduces response time and increases Throughput
2. High Efficiency: for business logic design, seamless integration is achieved without the need to consider caching.
The following example shows how to use
Add the following features to the service interface method:
[Command (Strategy = StrategyType. Failover, FailoverCluster = 3, RequestCacheEnabled = true)] // RequestCacheEnabled = true indicates that cache is enabled.
Intercept and obtain Cache
[InterceptMethod(CachingMethod.Get, Key = "GetUser_id_{0}", Mode = CacheTargetType.Redis, Time = 480)]
Intercept and delete Cache
[InterceptMethod(CachingMethod.Remove, "GetUser_id_{0}", "GetUserName_name_{0}", Mode = CacheTargetType.Redis)]
Use [CacheKey] to identify the cache Key, as shown in figure
public class UserModel { [CacheKey(1)] public int UserId { get; set; } public string Name { get; set; } public int Age { get; set; } }
Configure interceptor
.AddClientIntercepted(typeof(CacheProviderInterceptor))
4. Test
Test Environment
CPU: Intel Core i7-4710MQ
Memory: 16 GB
Hard Disk: 1 tb ssd + 512 GB HDD
Network: Lan
The test result of enabling redis is as follows: 5. Summary
API gateway has been developed. The latest update only fixes bugs and submits unit tests. If you are interested, please pay more attention or join the QQ group: 615562965