public IActionResult Consumer() { var polly = PolicyBuilder.CreatePolly(); polly.Execute(() => { var service = _consul.Resolve(); //service.Address }); return(Ok(StatusCodes.Status200OK)); }
static void Main(string[] args) { var serviceProvider = new ConsulServiceProvider(new Uri("http://127.0.0.1:8500")); var myServiceA = serviceProvider.CreateServiceBuilder(builder => { builder.ServiceName = "ServiceA"; // 指定负载均衡器 builder.LoadBalancer = TypeLoadBalancer.RoundRobin; // 指定Uri方案 builder.UriScheme = Uri.UriSchemeHttp; }); var httpClient = new HttpClient(); var policy = PolicyBuilder.CreatePolly(); // 重试轮询+熔断降级 // 你去一个商场消费,某个商品卖断货,你可能寻求次级品,(提供的服务,就降级了) // 服务A访问服务B,这个B老是失败,你失败多了,如果是高并发就会引发雪崩,我降级服务,我自己准一套备用的。 // 返回一个个空数据 // 源码肯定有, for (int i = 0; i < 100; i++) { Console.WriteLine($"-------------第{i}次请求-------------"); policy.Execute(() => { try { // 根据负载均衡获取具体服务地址 // consul崩了,仍然会被我们的polly接住 // 服务和服务间调用也一样 // 服务间传消息?直接调用API,MQ // 每个有服务调用的服务应用,都用POLLY,服务挂了 var uri = myServiceA.BuildAsync("health").Result; Console.WriteLine($"{DateTime.Now} - 正在调用:{uri}"); var content = httpClient.GetStringAsync(uri).Result; Console.WriteLine($"调用结果:{content}"); } catch (Exception e) { // 如果你要在策略里捕捉异常,如果这个异常还是你定义的故障,一定要把这异常再抛出来 Console.WriteLine($"调用异常:{e.GetType()}"); throw; } }); Task.Delay(1000).Wait(); } }
public async Task <IActionResult> Index() { CoreResult coreResult = new CoreResult(); #region ServiceDiscovery var serviceProvider = new ConsulServiceProvider(new Uri("http://127.0.0.1:8500")); var _userServiceUrl = serviceProvider.CreateServiceBuilder(builder => { builder.ServiceName = "UserApi"; builder.LoadBalancer = TypeLoadBalancer.RoundRobin; builder.UriScheme = Uri.UriSchemeHttp; }).BuildAsync("/api/user/servicesdiscovery").Result; #endregion var polly = PolicyBuilder.CreatePolly(); polly.Execute(() => { try { var form = new Dictionary <string, string>() { { "phone", "18650482503" } }; var response = _httpClient.PostAsync(_userServiceUrl.ToString(), new FormUrlEncodedContent(form)).Result; if (response.StatusCode == HttpStatusCode.OK) { coreResult.Success(response.Content.ReadAsStringAsync().Result); } else { coreResult.Failed(response.Content.ReadAsStringAsync().Result); } } catch (Exception ex) { _logger.LogError("在重试之后失败"); throw new Exception(ex.Message); } }); return(Content(coreResult.Message)); }
public async Task GetServiceDiscovery() { var serviceDiscoveryProvider = new ConsulServiceProvider(new Uri("http://127.0.0.1:8500")); #region 获取服务节点列表 //var serviceAList = await serviceDiscoveryProvider.GetServicesAsync("ServiceA"); //return serviceAList; #endregion var serviceA = serviceDiscoveryProvider.CreateServiceBuilder(builder => { builder.ServiceName = "ServiceA"; // 指定负载均衡器 builder.LoadBalancer = TypeLoadBalancer.RoundRobin; // 指定Uri方案 builder.UriScheme = Uri.UriSchemeHttp; }); var httpClient = new HttpClient(); var policy = PolicyBuilder.CreatePolly(); for (int i = 0; i < 100; i++) { Console.WriteLine($"-------------第{i}次请求-------------"); policy.Execute(() => { try { var uri = serviceA.BuildAsync("health").Result; System.Diagnostics.Debug.WriteLine($"{DateTime.Now} - 正在调用:{uri}"); var content = httpClient.GetStringAsync(uri).Result; System.Diagnostics.Debug.WriteLine($"调用结果:{content}"); } catch (Exception e) { System.Diagnostics.Debug.WriteLine($"调用异常:{e.GetType()}"); } }); Task.Delay(1000).Wait(); } }
static void Main(string[] args) { var snowId = new SnowflakeId(1, 1); var id = snowId.NextId(); var serviceProvider = new ConsulServiceProvider(new Uri("http://127.0.0.1:8500")); var myServiceA = serviceProvider.CreateServiceBuilder(builder => { builder.ServiceName = "MyServiceA"; // 指定负载均衡器 builder.LoadBalancer = TypeLoadBalancer.RoundRobin; // 指定Uri方案 builder.UriScheme = Uri.UriSchemeHttp; }); var httpClient = new HttpClient(); var polly = PolicyBuilder.CreatePolly(); // for (int i = 0; i < 100; i++) { Console.WriteLine($"-------------第{i}次请求-------------"); polly.Execute(() => { try { var uri = myServiceA.BuildAsync("health").Result; Console.WriteLine($"{DateTime.Now} - 正在调用:{uri}"); var content = httpClient.GetStringAsync(uri).Result; Console.WriteLine($"调用结果:{content}"); } catch (Exception e) { Console.WriteLine($"调用异常:{e.GetType()}"); } }); Task.Delay(1000).Wait(); } }
static void Main(string[] args) { var serviceProvider = new ConsulServiceProvider(new Uri("http://127.0.0.1:8500")); var myServiceA = serviceProvider.CreateServiceBuilder((builder) => { builder.ServiceName = "MyServiceA"; builder.LoadBalancer = TypeLoadBalancer.RoundRobin; builder.UriScheme = Uri.UriSchemeHttp; }); var policy = PolicyBuilder.CreatePolly(); var httpClient = new HttpClient(); for (int i = 0; i < 100; i++) { policy.Execute(() => { try { var uri = myServiceA.BuildAsync("/health").Result; Console.WriteLine($"{DateTime.Now} - 正在调用:{uri}"); var content = httpClient.GetAsync(uri).Result; Console.WriteLine($"调用结果:{content?.StatusCode}"); } catch (Exception ex) { Console.WriteLine("业务逻辑异常 ex:" + ex.GetType()); throw; } }); Task.Delay(1000).Wait(); } Console.Read(); }