博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring Boot 集成 Hystrix
阅读量:6338 次
发布时间:2019-06-22

本文共 7858 字,大约阅读时间需要 26 分钟。

续:

Hystrix使用

1 package com.cjs.example; 2  3 import com.netflix.hystrix.HystrixCommand; 4 import com.netflix.hystrix.HystrixCommandGroupKey; 5  6 public class CommandHelloWorld extends HystrixCommand
{ 7 8 private String name; 9 10 public CommandHelloWorld(HystrixCommandGroupKey group, String name) {11 super(group);12 this.name = name;13 }14 15 public CommandHelloWorld(Setter setter, String name) {16 super(setter);17 this.name = name;18 }19 20 @Override21 protected String run() throws Exception {22 if ("Alice".equals(name)) {23 throw new RuntimeException("出错了");24 }25 return "Hello, " + name;26 }27 28 @Override29 protected String getFallback() {30 return "Failure, " + name;31 }32 33 }
1 package com.cjs.example; 2  3 import com.netflix.hystrix.HystrixCommand; 4 import com.netflix.hystrix.HystrixCommandGroupKey; 5 import com.netflix.hystrix.HystrixCommandProperties; 6 import com.netflix.hystrix.HystrixThreadPoolProperties; 7 import org.junit.Test; 8 import rx.Observable; 9 import rx.Observer;10 11 import java.util.concurrent.ExecutionException;12 import java.util.concurrent.Future;13 14 import static org.junit.Assert.*;15 import static org.junit.Assert.assertEquals;16 17 public class CommandHelloWorldTest {18 19     @Test20     public void testSync() {21         HystrixCommandGroupKey hystrixCommandGroupKey = HystrixCommandGroupKey.Factory.asKey("ExampleGroup");22         CommandHelloWorld command = new CommandHelloWorld(hystrixCommandGroupKey, "World");23         String result = command.execute();24         assertEquals("Hello, World", result);25     }26 27     @Test28     public void testAsync() throws ExecutionException, InterruptedException {29         HystrixCommandGroupKey groupKey = HystrixCommandGroupKey.Factory.asKey("ExampleGroup");30         assertEquals("Hello, Jack", new CommandHelloWorld(groupKey, "Jack").queue().get());31         assertEquals("Hello, Rose", new CommandHelloWorld(groupKey, "Rose").queue().get());32 33         CommandHelloWorld command = new CommandHelloWorld(groupKey, "Cheng");34         Future
future = command.queue();35 String result = future.get();36 assertEquals("Hello, Cheng", result);37 38 // blocking39 Observable
observable = new CommandHelloWorld(groupKey, "Lucy").observe();40 assertEquals("Hello, Lucy", observable.toBlocking().single());41 42 // non-blocking43 Observable
observable2 = new CommandHelloWorld(groupKey, "Jerry").observe();44 observable2.subscribe(new Observer
() {45 @Override46 public void onCompleted() {47 System.out.println("completed");48 }49 50 @Override51 public void onError(Throwable throwable) {52 throwable.printStackTrace();53 }54 55 @Override56 public void onNext(String s) {57 System.out.println("onNext: " + s);58 }59 });60 }61 62 @Test63 public void testFail() throws ExecutionException, InterruptedException {64 HystrixCommandGroupKey groupKey = HystrixCommandGroupKey.Factory.asKey("Group2");65 assertEquals("Failure, Alice", new CommandHelloWorld(groupKey,"Alice").execute());66 assertEquals("Failure, Alice", new CommandHelloWorld(groupKey,"Alice").queue().get());67 }68 69 @Test70 public void testProp() {71 HystrixCommandGroupKey groupKey = HystrixCommandGroupKey.Factory.asKey("Group3");72 73 HystrixThreadPoolProperties.Setter threadPoolProperties = HystrixThreadPoolProperties.Setter()74 .withCoreSize(10)75 .withMaximumSize(10);76 77 HystrixCommandProperties.Setter commandProperties = HystrixCommandProperties.Setter()78 .withCircuitBreakerEnabled(true)79 .withExecutionTimeoutInMilliseconds(100);80 81 HystrixCommand.Setter setter = HystrixCommand.Setter.withGroupKey(groupKey);82 setter.andThreadPoolPropertiesDefaults(threadPoolProperties);83 setter.andCommandPropertiesDefaults(commandProperties);84 85 assertEquals("Hello, Cheng", new CommandHelloWorld(setter, "Cheng").execute());86 87 }88 }

Spring Boot中使用Hystrix

1. Maven依赖

2. 使用@HystrixCommand注解

1 package com.cjs.example; 2  3 import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand; 4 import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty; 5 import org.apache.commons.lang.StringUtils; 6 import org.springframework.web.bind.annotation.RequestMapping; 7 import org.springframework.web.bind.annotation.RestController; 8  9 @RestController10 @RequestMapping("/greet")11 public class GreetController {12 13     @HystrixCommand(fallbackMethod = "onError",14             commandProperties = {15                     @HystrixProperty(name = "execution.isolation.strategy", value = "THREAD"),16                     @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "1000"),17                     @HystrixProperty(name = "circuitBreaker.enabled", value = "true"),18                     @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "2")},19             threadPoolProperties = {20                     @HystrixProperty(name = "coreSize", value = "5"),21                     @HystrixProperty(name = "maximumSize", value = "5"),22                     @HystrixProperty(name = "maxQueueSize", value = "10")23             })24     @RequestMapping("/sayHello")25     public String sayHello(String name) {26         try {27             Thread.sleep(200);28         } catch (InterruptedException e) {29             e.printStackTrace();30         }31         return "Hello, " + name;32     }33 34     @HystrixCommand35     @RequestMapping("/sayHi")36     public String sayHi(String name) {37         if (StringUtils.isBlank(name)) {38             throw new RuntimeException("name不能为空");39         }40         return "Good morning, " + name;41     }42 43     /**44      * 如果fallback方法的参数和原方法参数个数不一致,则会出现FallbackDefinitionException: fallback method wasn't found45      */46     public String onError(String name) {47         return "Error!!!" + name;48     }49 50 }

3. Hystrix配置

1 package com.cjs.example; 2  3 import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand; 4 import com.netflix.hystrix.contrib.javanica.aop.aspectj.HystrixCommandAspect; 5 import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet; 6 import org.springframework.boot.web.servlet.ServletContextInitializer; 7 import org.springframework.boot.web.servlet.ServletRegistrationBean; 8 import org.springframework.context.annotation.Bean; 9 import org.springframework.context.annotation.Configuration;10 11 import javax.servlet.Servlet;12 13 @Configuration14 public class HystrixConfig {15 16     /**17      * A {
@link ServletContextInitializer} to register {
@link Servlet}s in a Servlet 3.0+ container.18 */19 @Bean20 public ServletRegistrationBean hystrixMetricsStreamServlet() {21 return new ServletRegistrationBean(new HystrixMetricsStreamServlet(), "/hystrix.stream");22 }23 24 /**25 * AspectJ aspect to process methods which annotated with {
@link HystrixCommand} annotation.26 *27 * {
@link HystrixCommand} annotation used to specify some methods which should be processes as hystrix commands.28 */29 @Bean30 public HystrixCommandAspect hystrixCommandAspect() {31 return new HystrixCommandAspect();32 }33 34 }

4. hystrix-dashboard

http://localhost:7979/hystrix-dashboard/

 

参考

 

转载地址:http://udaoa.baihongyu.com/

你可能感兴趣的文章
浅谈OS
查看>>
mac下开启docker API远程调用
查看>>
tar 命令的详解
查看>>
Cisco路由器安全配置
查看>>
第十次作业
查看>>
给定一个字符串s,返回去掉子串"mi"后的字符串。
查看>>
Nginx 外的另一选择,轻量级开源 Web 服务器 Tengine 发布新版本
查看>>
Wrod中超链接的一些技巧
查看>>
IP_VFR-4-FRAG_TABLE_OVERFLOW【cisco设备报错】碎片***
查看>>
Codeforces Round #256 (Div. 2) D. Multiplication Table 【二分】
查看>>
ARM汇编指令格式
查看>>
HDU-2044-一只小蜜蜂
查看>>
HDU-1394-Minimum Inversion Number
查看>>
df -h 卡住
查看>>
[转] createObjectURL方法 实现本地图片预览
查看>>
JavaScript—DOM编程核心.
查看>>
JavaScript碎片
查看>>
Bootstrap-下拉菜单
查看>>
soapUi 接口测试
查看>>
【c学习-12】
查看>>