dubbo-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From GitBox <...@apache.org>
Subject [GitHub] timestatic edited a comment on issue #1293: A question for ReferenceConfigCache.
Date Thu, 06 Sep 2018 15:37:44 GMT
timestatic edited a comment on issue #1293: A question for ReferenceConfigCache.
URL: https://github.com/apache/incubator-dubbo/issues/1293#issuecomment-419140572
 
 
   In my project, this method `ReferenceConfigCache` is used for genericService calls, caching
`ReferenceConfig` instances, for the user is knowing his the method arguments.Of course, it
also looks complicated to use.
   In my implementation, I use interfaceName, group and version as a key, used as follow:
   
   ```
           MyReferenceConfigCache referenceConfigCache = MyReferenceConfigCache.getCache();
           // 缓存reference实例
           GenericService genericService = referenceConfigCache.get(interfaceName, group,
version);
           if (genericService == null) {
               // 引用远程服务
               ReferenceConfig<GenericService> reference = new ReferenceConfig();
   
               reference.setInterface(interfaceName);
               reference.setVersion(version);
               // 服务分组
               if (StringUtils.isNotBlank(group)) {
                   reference.setGroup(group);
               }
               // 声明为泛化接口
               reference.setGeneric(true);
   
               // 连接注册中心配置
               RegistryConfig registry = new RegistryConfig();
               // 注册中心协议与地址, 根据实际修改
               registry.setAddress(registryAddress);
               // 当前消费者应用配置
               ApplicationConfig application = new ApplicationConfig();
               application.setName("consumer_name");
               application.setRegistry(registry);
               reference.setApplication(application);
   
               genericService = referenceConfigCache.putIfAbsentAndGet(reference);
           }
   
           Object result = genericService.$invoke(methodName, parameterTypes, args);
   
   ```
   `MyReferenceConfigCache` some modified methodis as follows:
   
   ```
       public <T> T get(String interfaceName, String group, String version) {
           String key = generator.generateKey(interfaceName, group, version);
   
           ReferenceConfig<?> config = cache.get(key);
           if(config != null) {
               return (T) config.get();
           }
   
           return null;
       }
   
   ```
   
   ```
       public <T> T putIfAbsentAndGet(ReferenceConfig<T> referenceConfig) {
           String iName = referenceConfig.getInterface();
           if(StringUtils.isBlank(iName)) {
               throw new IllegalArgumentException("No interface info in ReferenceConfig" +
referenceConfig);
           }
   
           String key = generator.generateKey(iName, referenceConfig.getGroup(), referenceConfig.getVersion());
   
           ReferenceConfig<?> config = cache.get(key);
           if(config != null) {
               return (T) config.get();
           }
   
           cache.putIfAbsent(key, referenceConfig);
           config = cache.get(key);
           return (T) config.get();
       }
   
   ```
   

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org


Mime
View raw message