Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

这是我写的一个SurgingDemo,还烦请问使用姿势是否正确 #54

Closed
billyang opened this issue Mar 1, 2018 · 12 comments
Closed

Comments

@billyang
Copy link

billyang commented Mar 1, 2018

作者你好,
这是我写的一个SurgingDemo,还烦请问使用姿势是否正确 -_-...
如果用的不对的地方,还请指正,谢谢
本示例git surging 到本地,再另外新建的Demo,通过引用Surging项目到Demo来实现,这样加深学习。
ORM使用的是Dapper

SurgingDemo

@fanliang11
Copy link
Owner

姿势很正确,如果有缓存拦截的demo就更好了

@billyang
Copy link
Author

billyang commented Mar 1, 2018

@fanliang11 其实我想把缓存拦截的写上去,刚在群里也看到了,但是有点小疑问,对“GetUserName_name_{0}“ 这个参数不理解

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

image

@fanliang11
Copy link
Owner

1.传递的方法参数如果是model类型,就需要设置 [CacheKey(1)]来标识缓存key, 比如传递UserModel,
设置UserId 为1,Name 为fanly, 设置的KEY为GetUserName_name_{1}
那么缓存的key就会生成GetUserName_name_fanly, key 如果设置为GetUserName_id_{0}
那么缓存的key就会生成GetUserName_id_1
2. 传递的方法参数是string,int 类型就不需要设置 [CacheKey(1)]

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

   }

@billyang
Copy link
Author

billyang commented Mar 1, 2018

哎呀,我好像理解错了,抱歉。
在Remove模式下,"GetUser_id_{0}", "GetUserName_name_{0}" 移除的缓存是一个数组,我看成了是多个参数。。。

@billyang
Copy link
Author

billyang commented Mar 2, 2018

我配的缓存拦截好像没起作用,没写入到Redis,是不是哪里还需要配置?拦截器配置我看到示例是放到Client 和ApiGateway

IUserService
[InterceptMethod(CachingMethod.Get, Key = "GetUser_id_{0}", CacheSectionType = SectionType.ddlCache, Mode = CacheTargetType.Redis, Time = 480)]
Task<UserDto> GetUserById(Int64 id);

[InterceptMethod(CachingMethod.Put, Key = "GetUser_id_{0}", CacheSectionType = SectionType.ddlCache, Mode = CacheTargetType.Redis)]
Task<Boolean> UpdateUser(UserDto user);


[InterceptMethod(CachingMethod.Remove, "GetUser_id_{0}", CacheSectionType = SectionType.ddlCache, Mode = CacheTargetType.Redis)]
Task<Boolean> DeleteUser(Int64 userId);

UserDto

[ProtoContract]
public class UserDto 
{
        [ProtoMember(1)]
        [CacheKey(1)]
        public long Id { get; set; }

        [ProtoMember(2)]
        public string PhoneNumber { get; set; }

        [ProtoMember(3)]
        public string Name { get; set; }
}

@fanliang11
Copy link
Owner

你需要设置RequestCacheEnabled = true和 option.AddClientIntercepted(typeof(CacheProviderInterceptor));

[Command(Strategy = StrategyType.Failover, RequestCacheEnabled = true, InjectionNamespaces = new string[] { "Surging.IModuleServices.Common" })]
builder.AddMicroService(option =>
{
option.AddClient();
option.AddClientIntercepted(typeof(CacheProviderInterceptor));
//option.UseZooKeeperManager(new ConfigInfo("127.0.0.1:2181"));
option.UseConsulManager(new ConfigInfo("127.0.0.1:8500"));
option.UseDotNettyTransport();
option.UseRabbitMQTransport();
//option.UseKafkaMQTransport(kafkaOption =>
//{
// kafkaOption.Servers = "127.0.0.1";
//});
//option.UseProtoBufferCodec();
option.UseMessagePackCodec();
builder.Register(p => new CPlatformContainer(ServiceLocator.Current));
});

@billyang
Copy link
Author

billyang commented Mar 2, 2018

缓存还是没起作用,是写入到Redis没错吧。
要是有时间看下Bill.Demo.IModuleServicesBill.Demo.Services.Server 看看配置是否正确,谢谢哈

[Service(Date = "2018-3-2", Director = "Bill", Name = "获取用户信息")]
[Command(Strategy = StrategyType.Failover, RequestCacheEnabled = true, InjectionNamespaces = new string[] { "Bill.Demo.IModuleServices.Users" })]
[InterceptMethod(CachingMethod.Get, Key = "GetUser_id_{0}", CacheSectionType = SectionType.ddlCache, Mode = CacheTargetType.Redis, Time = 480)]
Task<UserDto> GetUserById(Int64 id);

@fanliang11
Copy link
Owner

已修复不能获取服务条目的BUG,请下载最新的代码

@billyang
Copy link
Author

billyang commented Mar 4, 2018

你好,更新了代码出现了新的问题。Surging提供的Client跑起来没问题,但是在我的Web客户端有问题,
Client使用了ServiceHostBuilder,我的Web使用的是WebHostBuilder

发现使用ServiceHostBuilder 使用了

                .UseServiceCache()
                .UseProxy()
                .UseLog4net()
                .UseClient()

还麻烦看看Web下应该如何配置
再往下就不太会调试了

这是报错信息
image

@fanliang11
Copy link
Owner

不要用.UseServiceCache(),请更新master版本,请用原来的配置

@billyang
Copy link
Author

billyang commented Mar 4, 2018

更新了的。在 WebHostBuilder 使用不了 .UseServiceCache().UseProxy().UseLog4net().UseClient()

Surging.Services.Client 、Bill.Demo.Web 两者配置有些不同,Bill.Demo.Web我是根据Surging.ApiGateway来配的

@fanliang11
Copy link
Owner

用你原来的配置。不要去更改任何配置

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants