Skip to content

Commit 440e247

Browse files
authored
Merge pull request eugenp#586 from sivabalachandran/master
Hystrix Spring integration article 2
2 parents 2b7f03e + 6401669 commit 440e247

12 files changed

Lines changed: 257 additions & 25 deletions

‎hystrix/pom.xml‎

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,15 @@
66
<groupId>com.baeldung</groupId>
77
<artifactId>hystrix</artifactId>
88
<version>1.0</version>
9-
109
<name>hystrix</name>
1110

11+
<parent>
12+
<groupId>org.springframework.boot</groupId>
13+
<artifactId>spring-boot-starter-parent</artifactId>
14+
<version>1.4.0.RELEASE</version>
15+
<relativePath></relativePath>
16+
</parent>
17+
1218
<properties>
1319

1420
<!-- General -->
@@ -32,12 +38,35 @@
3238

3339
<dependencies>
3440

41+
<dependency>
42+
<groupId>org.springframework.boot</groupId>
43+
<artifactId>spring-boot-starter-web</artifactId>
44+
</dependency>
45+
46+
<dependency>
47+
<groupId>org.springframework.boot</groupId>
48+
<artifactId>spring-boot-starter-aop</artifactId>
49+
</dependency>
50+
3551
<dependency>
3652
<groupId>com.netflix.hystrix</groupId>
3753
<artifactId>hystrix-core</artifactId>
3854
<version>${hystrix-core.version}</version>
3955
</dependency>
4056

57+
<dependency>
58+
<groupId>com.netflix.hystrix</groupId>
59+
<artifactId>hystrix-metrics-event-stream</artifactId>
60+
<version>1.3.16</version>
61+
</dependency>
62+
63+
<!-- https://mvnrepository.com/artifact/com.netflix.hystrix/hystrix-dashboard -->
64+
<!--<dependency>
65+
<groupId>com.netflix.hystrix</groupId>
66+
<artifactId>hystrix-dashboard</artifactId>
67+
<version>1.4.3</version>
68+
</dependency>-->
69+
4170
<dependency>
4271
<groupId>com.netflix.rxjava</groupId>
4372
<artifactId>rxjava-core</artifactId>
@@ -62,6 +91,10 @@
6291

6392
<build>
6493
<plugins>
94+
<plugin>
95+
<groupId>org.springframework.boot</groupId>
96+
<artifactId>spring-boot-maven-plugin</artifactId>
97+
</plugin>
6598
<plugin>
6699
<groupId>org.apache.maven.plugins</groupId>
67100
<artifactId>maven-compiler-plugin</artifactId>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.baeldung.hystrix;
2+
3+
import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet;
4+
import org.springframework.boot.SpringApplication;
5+
import org.springframework.boot.autoconfigure.SpringBootApplication;
6+
import org.springframework.boot.web.servlet.ServletRegistrationBean;
7+
import org.springframework.context.annotation.Bean;
8+
9+
@SpringBootApplication
10+
public class AppConfig {
11+
12+
public static void main(String[] args) {
13+
SpringApplication.run(AppConfig.class, args);
14+
}
15+
16+
@Bean
17+
public ServletRegistrationBean adminServletRegistrationBean() {
18+
return new ServletRegistrationBean(new HystrixMetricsStreamServlet(), "/hystrix.stream");
19+
}
20+
}

hystrix/src/test/java/com/baeldung/hystrix/CommandHelloWorld.java renamed to hystrix/src/main/java/com/baeldung/hystrix/CommandHelloWorld.java

File renamed without changes.
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package com.baeldung.hystrix;
2+
3+
import com.netflix.hystrix.*;
4+
import org.aspectj.lang.ProceedingJoinPoint;
5+
import org.aspectj.lang.annotation.Around;
6+
import org.aspectj.lang.annotation.Aspect;
7+
import org.springframework.beans.factory.annotation.Value;
8+
import org.springframework.stereotype.Component;
9+
10+
import javax.annotation.PostConstruct;
11+
12+
@Component
13+
@Aspect
14+
public class HystrixAspect {
15+
16+
private HystrixCommand.Setter config;
17+
private HystrixCommandProperties.Setter commandProperties;
18+
private HystrixThreadPoolProperties.Setter threadPoolProperties;
19+
20+
@Around("@annotation(com.baeldung.hystrix.HystrixCircuitBreaker)")
21+
public Object circuitBreakerAround(final ProceedingJoinPoint aJoinPoint) {
22+
return new RemoteServiceCommand(config, aJoinPoint).execute();
23+
}
24+
25+
@PostConstruct
26+
private void setup() {
27+
this.config = HystrixCommand.Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey(groupKey));
28+
this.config = config.andCommandKey(HystrixCommandKey.Factory.asKey(key));
29+
30+
this.commandProperties = HystrixCommandProperties.Setter();
31+
this.commandProperties.withExecutionTimeoutInMilliseconds(executionTimeout);
32+
this.commandProperties.withCircuitBreakerSleepWindowInMilliseconds(sleepWindow);
33+
34+
this.threadPoolProperties= HystrixThreadPoolProperties.Setter();
35+
this.threadPoolProperties.withMaxQueueSize(maxThreadCount).withCoreSize(coreThreadCount).withMaxQueueSize(queueCount);
36+
37+
this.config.andCommandPropertiesDefaults(commandProperties);
38+
this.config.andThreadPoolPropertiesDefaults(threadPoolProperties);
39+
}
40+
41+
private static class RemoteServiceCommand extends HystrixCommand<String> {
42+
43+
private final ProceedingJoinPoint joinPoint;
44+
45+
RemoteServiceCommand(final Setter config, final ProceedingJoinPoint joinPoint) {
46+
super(config);
47+
this.joinPoint = joinPoint;
48+
}
49+
50+
@Override
51+
protected String run() throws Exception {
52+
try {
53+
return (String) joinPoint.proceed();
54+
} catch (final Throwable th) {
55+
throw new Exception(th);
56+
}
57+
58+
}
59+
}
60+
61+
@Value("${remoteservice.command.execution.timeout}")
62+
private int executionTimeout;
63+
64+
@Value("${remoteservice.command.sleepwindow}")
65+
private int sleepWindow;
66+
67+
@Value("${remoteservice.command.threadpool.maxsize}")
68+
private int maxThreadCount;
69+
70+
@Value("${remoteservice.command.threadpool.coresize}")
71+
private int coreThreadCount;
72+
73+
@Value("${remoteservice.command.task.queue.size}")
74+
private int queueCount;
75+
76+
@Value("${remoteservice.command.group.key}")
77+
private String groupKey;
78+
79+
@Value("${remoteservice.command.key}")
80+
private String key;
81+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.baeldung.hystrix;
2+
3+
import java.lang.annotation.ElementType;
4+
import java.lang.annotation.Retention;
5+
import java.lang.annotation.RetentionPolicy;
6+
import java.lang.annotation.Target;
7+
8+
@Retention(RetentionPolicy.RUNTIME)
9+
@Target(ElementType.METHOD)
10+
public @interface HystrixCircuitBreaker {
11+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.baeldung.hystrix;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.web.bind.annotation.RequestMapping;
5+
import org.springframework.web.bind.annotation.RestController;
6+
7+
@RestController
8+
public class HystrixController {
9+
10+
@Autowired
11+
private SpringExistingClient client;
12+
13+
@RequestMapping("/")
14+
public String index() throws InterruptedException{
15+
return client.invokeRemoteService();
16+
}
17+
}

‎hystrix/src/main/java/com/baeldung/hystrix/RemoteServiceSimulator.java‎

Lines changed: 0 additions & 15 deletions
This file was deleted.

hystrix/src/test/java/com/baeldung/hystrix/RemoteServiceTestCommand.java renamed to hystrix/src/main/java/com/baeldung/hystrix/RemoteServiceTestCommand.java

File renamed without changes.

hystrix/src/test/java/com/baeldung/hystrix/RemoteServiceTestSimulator.java renamed to hystrix/src/main/java/com/baeldung/hystrix/RemoteServiceTestSimulator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.baeldung.hystrix;
22

33

4-
class RemoteServiceTestSimulator {
4+
public class RemoteServiceTestSimulator {
55

66
private long wait;
77

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.baeldung.hystrix;
2+
3+
import org.springframework.beans.factory.annotation.Value;
4+
import org.springframework.stereotype.Component;
5+
6+
@Component("springClient")
7+
public class SpringExistingClient {
8+
9+
@Value("${remoteservice.timeout}")
10+
private int remoteServiceDelay;
11+
12+
@HystrixCircuitBreaker
13+
public String invokeRemoteService() throws InterruptedException{
14+
return new RemoteServiceTestSimulator(remoteServiceDelay).execute();
15+
}
16+
17+
}

0 commit comments

Comments
 (0)