1

如何基于surging跨网关跨语言进行缓存降级 - fanly11

 1 week ago
source link: https://www.cnblogs.com/fanliang11/p/18173704
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

       surging是一款开源的微服务引擎,包含了rpc服务治理,中间件,以及多种外部协议来解决各个行业的业务问题,在日益发展的今天,业务的需求也更加复杂,单一语言也未必能抗下所有,所以在多语言行业解决方案优势情况下,那么就需要多语言的协同研发,而对于协同研发环境下,统一配置的网关,多语言访问调用必然会涉及到需要数据缓存的问题,那么怎么做到跨网关跨语言缓存降级呢?那么将在此篇文章中进行讲解。

如何创建拦截器

继承IInterceptor ,创建拦截,如下代码所示

public class LogProviderInterceptor : IInterceptor
    {
        public async Task Intercept(IInvocation invocation)
        { 
            await invocation.Proceed();
            var result = invocation.ReturnValue;
        }
    }

服务引擎针对于IInterceptor 扩展了CacheInterceptor用来做缓存拦截,如以下代码所示

  public class CacheProviderInterceptor : CacheInterceptor
    {
        public override async Task Intercept(ICacheInvocation invocation)
        { 
        }
}

如何使用缓存拦截器

通过设置特性Metadatas.ServiceCacheIntercept配置缓存拦截,如以下代码所示

 [Metadatas.ServiceCacheIntercept(Metadatas.CachingMethod.Get, Key = "GetUser_{0}_{1}", L2Key = "GetUser_{0}_{1}",EnableL2Cache =true, CacheSectionType = "ddlCache", Mode = Metadatas.CacheTargetType.Redis, Time = 480)]

在处理业务的修改,删除方法时候,需要移除依赖的缓存,那么可以设置CorrespondingKeys,如以下代码所示

 [Metadatas.ServiceCacheIntercept(CachingMethod.Remove, "GetUser_id_{0}", "GetUserName_name_{0}", CacheSectionType = SectionType.ddlCache, Mode = CacheTargetType.Redis)]

如何设置缓存Key

1.比如缓存设置为GetUserById_{0}, 传递的参数是int 类型,值为2199 ,那么产生的key就是GetUserById_2199.

2.比如缓存设置为GetUser_{0}_{1},传递的参数是UserModel类型,传递为new UserModel{ UserId=2199,Name="Fanly" }值,那么产生的Key就是GetUser_fanly_2199. 标识CacheKeyAttribute特性以生成缓存key, 并且设置SortIndex排序依次生成。

public class UserModel
    {
       [CacheKey(1)]
        public int UserId { get; set; }
        [CacheKey(2)]
public string Name { get; set; } public int Age { get; set; } }

创建拦截模块

通过以下代码,把拦截器注入到服务引擎中

public class IntercepteModule : SystemModule
{
public override void Initialize(CPlatformContainer serviceProvider)
{
base.Initialize(serviceProvider);
}
/// <summary>
/// Inject dependent third-party components
/// </summary>
/// <param name="builder"></param>
protected override void RegisterBuilder(ContainerBuilderWrapper builder)
{
base.RegisterBuilder(builder);
builder.AddClientIntercepted(typeof(CacheProviderInterceptor),typeof(LogProviderInterceptor));
}
}

 如何跨语言调用中开启缓存拦截降级

在surging 是调用分为二种

1.基于接口创建代理调用,可以作为同一语言的互相调用,性能上比第二种基于routepath要快,但是具有高耦合性

  var userProxy = ServiceLocator.GetService<IServiceProxyFactory>().CreateProxy<IUserService>("User");

2.基于routepath调用,可以作为跨语言调用,性能上比第一种基于接口创建代理要慢,但是具有低耦合性

  Dictionary<string, object> model = new Dictionary<string, object>();
  model.Add("name", name);
  string path = "api/hello/say";
   string result =await _serviceProxyProvider.Invoke<object>(model, path, null);

而在服务调用下,因为业务模型参数在基于routepath调用情况,做不到模型参数解析,只能支持单一参数和无参数的缓存拦截调用,而基于接口创建代理调用是可以支持业务模型缓存调用的,在以下特征情况下就需要在Metadatas.ServiceCacheIntercept特性下开启EnableStageCache,代码如下

[Metadatas.ServiceCacheIntercept(Metadatas.CachingMethod.Get, Key = "GetDictionary", L2Key = "GetDictionary", EnableL2Cache = true, CacheSectionType = "ddlCache", Mode = Metadatas.CacheTargetType.Redis, Time = 480, EnableStageCache = true)]      

通过以上的代码,运行后,在注册中心注册的服务路由下可以看到拦截器元数据,这样在其它语言通过元数据可以构造服务消费者的缓存拦截降级。

192878-20240505161535366-897566098.png

 以下是基于二种调用的缓存结果存储redis中

192878-20240505165114739-1628432663.png

如何处理缓存K/V 中Value 过大

缓存中间件Redis是一种高性能的内存数据库,用于存储键值对的数据结构。当value的大小超过一定限制时,一般超过10K就会影响查询的性能。这时候使用一二级缓存来解决,一级缓存用redis 存储标记,标记缓存是否失效,二级缓存用本地缓存存储,当标记失效不存在后,会远程调用服务,返回结果添加一级缓存标记,返回结果添加到二级缓存。

提示:大家可以按照自己的业务需求,研发缓存拦截,不一定非要使用CacheProviderInterceptor,按照CacheProviderInterceptor一二级缓存进行构建研发

      社区版:https://github.com/fanliang11/surging,如果需要其它版本,请联系作者。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK