public async Task <RoutingKeyRegisterResponse> DeleteRoutingKeyAsync([FromBody] RoutingKeyRegisterRequest request) { RoutingKeyRegisterResponse response = new RoutingKeyRegisterResponse(); var routingKeyInfo = _context.RabbitMqRegisterInfo.SingleOrDefault(a => a.RoutingKey == request.RoutingKey); if (routingKeyInfo != null) { //删除Redis中的服务地址 var routingApiUrlInRedis = StackRedis.Current.Remove(request.RoutingKey); //解绑RabbitMQ _eventBus.UnSubscribe(routingKeyInfo.QueueName, routingKeyInfo.RoutingKey); //删除数据库数据 _context.RabbitMqRegisterInfo.Remove(routingKeyInfo); } return(response); }
public async Task <RoutingKeyRegisterResponse> RoutingKeyRegisterAsync([FromBody] RoutingKeyRegisterRequest request) { RoutingKeyRegisterResponse response = new RoutingKeyRegisterResponse(); try { if (string.IsNullOrEmpty(request.RoutingKey) || string.IsNullOrEmpty(request.ApiUrl)) { response.Successful = false; response.Message = "RoutingKey与ApiUrl不能为空!"; } if (string.IsNullOrEmpty(request.QueueName)) { request.QueueName = Configuration["SubscriptionClientName"]; } var isExistsRoutingKey = _context.RabbitMqRegisterInfo.Any(a => a.RoutingKey == request.RoutingKey); if (isExistsRoutingKey) { response.Successful = false; response.Message = "对应的RoutingKey已存在!"; return(response); } var rabbitMqRegister = new RabbitMQRegisterInfo { RoutingKey = request.RoutingKey, QueueName = request.QueueName, ApiUrl = request.ApiUrl, CreateTime = DateTime.Now, UpdateTime = DateTime.Now }; _context.Add(rabbitMqRegister); _context.SaveChanges(); //绑定RoutingKey和队列 _eventBus.Subscribe(request.QueueName, request.RoutingKey); } catch (Exception ex) { response.Successful = false; response.Message = ex.Message; } return(response); }
public async Task <RoutingKeyRegisterResponse> UpdateRoutingKeyInfoAsync([FromBody] RoutingKeyRegisterRequest request) { RoutingKeyRegisterResponse response = new RoutingKeyRegisterResponse(); var routingKeyInfo = _context.RabbitMqRegisterInfo.SingleOrDefault(a => a.RoutingKey == request.RoutingKey); if (routingKeyInfo != null) { //修改Redis内的服务地址 var redisRoutingApiUrls = await StackRedis.Current.GetAllList(request.RoutingKey); redisRoutingApiUrls.Remove(routingKeyInfo.ApiUrl); redisRoutingApiUrls.Add(request.ApiUrl); await StackRedis.Current.SetList(request.RoutingKey, redisRoutingApiUrls); routingKeyInfo.ApiUrl = request.ApiUrl; routingKeyInfo.UpdateTime = DateTime.Now; _context.Update(routingKeyInfo); _context.SaveChanges(); } return(response); }