Skip to content

Commit a3e70bc

Browse files
committed
add hystrix-service
1 parent ccad61a commit a3e70bc

File tree

12 files changed

+495
-0
lines changed

12 files changed

+495
-0
lines changed

hystrix-service/.gitignore

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
HELP.md
2+
target/
3+
!.mvn/wrapper/maven-wrapper.jar
4+
!**/src/main/**
5+
!**/src/test/**
6+
7+
### STS ###
8+
.apt_generated
9+
.classpath
10+
.factorypath
11+
.project
12+
.settings
13+
.springBeans
14+
.sts4-cache
15+
16+
### IntelliJ IDEA ###
17+
.idea
18+
*.iws
19+
*.iml
20+
*.ipr
21+
22+
### NetBeans ###
23+
/nbproject/private/
24+
/nbbuild/
25+
/dist/
26+
/nbdist/
27+
/.nb-gradle/
28+
build/
29+
30+
### VS Code ###
31+
.vscode/

hystrix-service/pom.xml

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<parent>
6+
<groupId>org.springframework.boot</groupId>
7+
<artifactId>spring-boot-starter-parent</artifactId>
8+
<version>2.1.7.RELEASE</version>
9+
<relativePath/> <!-- lookup parent from repository -->
10+
</parent>
11+
<groupId>com.macro.cloud</groupId>
12+
<artifactId>hystrix-service</artifactId>
13+
<version>0.0.1-SNAPSHOT</version>
14+
<name>hystrix-service</name>
15+
<description>Demo project for Spring Boot</description>
16+
17+
<properties>
18+
<java.version>1.8</java.version>
19+
<spring-cloud.version>Greenwich.SR2</spring-cloud.version>
20+
</properties>
21+
22+
<dependencies>
23+
<dependency>
24+
<groupId>org.springframework.boot</groupId>
25+
<artifactId>spring-boot-starter-test</artifactId>
26+
<scope>test</scope>
27+
</dependency>
28+
<dependency>
29+
<groupId>org.springframework.cloud</groupId>
30+
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
31+
</dependency>
32+
<dependency>
33+
<groupId>org.springframework.cloud</groupId>
34+
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
35+
</dependency>
36+
<dependency>
37+
<groupId>org.springframework.boot</groupId>
38+
<artifactId>spring-boot-starter-web</artifactId>
39+
</dependency>
40+
<dependency>
41+
<groupId>org.springframework.boot</groupId>
42+
<artifactId>spring-boot-starter-actuator</artifactId>
43+
</dependency>
44+
<dependency>
45+
<groupId>cn.hutool</groupId>
46+
<artifactId>hutool-all</artifactId>
47+
<version>4.6.3</version>
48+
</dependency>
49+
</dependencies>
50+
51+
<dependencyManagement>
52+
<dependencies>
53+
<dependency>
54+
<groupId>org.springframework.cloud</groupId>
55+
<artifactId>spring-cloud-dependencies</artifactId>
56+
<version>${spring-cloud.version}</version>
57+
<type>pom</type>
58+
<scope>import</scope>
59+
</dependency>
60+
</dependencies>
61+
</dependencyManagement>
62+
63+
<build>
64+
<plugins>
65+
<plugin>
66+
<groupId>org.springframework.boot</groupId>
67+
<artifactId>spring-boot-maven-plugin</artifactId>
68+
</plugin>
69+
</plugins>
70+
</build>
71+
72+
</project>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.macro.cloud;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
6+
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
7+
8+
@EnableCircuitBreaker
9+
@EnableDiscoveryClient
10+
@SpringBootApplication
11+
public class HystrixServiceApplication {
12+
13+
public static void main(String[] args) {
14+
SpringApplication.run(HystrixServiceApplication.class, args);
15+
}
16+
17+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.macro.cloud.config;
2+
3+
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
4+
import org.springframework.context.annotation.Bean;
5+
import org.springframework.context.annotation.Configuration;
6+
import org.springframework.web.client.RestTemplate;
7+
8+
/**
9+
* Created by macro on 2019/8/29.
10+
*/
11+
@Configuration
12+
public class RibbonConfig {
13+
14+
@Bean
15+
@LoadBalanced
16+
public RestTemplate restTemplate(){
17+
return new RestTemplate();
18+
}
19+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package com.macro.cloud.controller;
2+
3+
import cn.hutool.core.thread.ThreadUtil;
4+
import com.macro.cloud.domain.CommonResult;
5+
import com.macro.cloud.domain.User;
6+
import com.macro.cloud.service.UserService;
7+
import org.springframework.beans.factory.annotation.Autowired;
8+
import org.springframework.web.bind.annotation.GetMapping;
9+
import org.springframework.web.bind.annotation.PathVariable;
10+
import org.springframework.web.bind.annotation.RequestMapping;
11+
import org.springframework.web.bind.annotation.RestController;
12+
13+
import java.util.concurrent.ExecutionException;
14+
import java.util.concurrent.Future;
15+
16+
/**
17+
* Created by macro on 2019/9/3.
18+
*/
19+
@RestController
20+
@RequestMapping("/user")
21+
public class UserHystrixController {
22+
@Autowired
23+
private UserService userService;
24+
25+
@GetMapping("/testFallback/{id}")
26+
public CommonResult testFallback(@PathVariable Long id) {
27+
return userService.getUser(id);
28+
}
29+
30+
@GetMapping("/testCommand/{id}")
31+
public CommonResult testCommand(@PathVariable Long id) {
32+
return userService.getUserCommand(id);
33+
}
34+
35+
@GetMapping("/testException/{id}")
36+
public CommonResult testException(@PathVariable Long id) {
37+
return userService.getUserException(id);
38+
}
39+
40+
@GetMapping("/testCache/{id}")
41+
public CommonResult testCache(@PathVariable Long id) {
42+
userService.getUserCache(id);
43+
userService.getUserCache(id);
44+
userService.getUserCache(id);
45+
return new CommonResult("操作成功", 200);
46+
}
47+
48+
@GetMapping("/testRemoveCache/{id}")
49+
public CommonResult testRemoveCache(@PathVariable Long id) {
50+
userService.getUserCache(id);
51+
userService.removeCache(id);
52+
userService.getUserCache(id);
53+
return new CommonResult("操作成功", 200);
54+
}
55+
56+
@GetMapping("/testCollapser")
57+
public CommonResult testCollapser() throws ExecutionException, InterruptedException {
58+
Future<User> future1 = userService.getUserFuture(1L);
59+
Future<User> future2 = userService.getUserFuture(2L);
60+
future1.get();
61+
future2.get();
62+
ThreadUtil.safeSleep(200);
63+
Future<User> future3 = userService.getUserFuture(3L);
64+
future3.get();
65+
return new CommonResult("操作成功", 200);
66+
}
67+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.macro.cloud.domain;
2+
3+
/**
4+
* Created by macro on 2019/8/29.
5+
*/
6+
public class CommonResult<T> {
7+
private T data;
8+
private String message;
9+
private Integer code;
10+
11+
public CommonResult() {
12+
}
13+
14+
public CommonResult(T data, String message, Integer code) {
15+
this.data = data;
16+
this.message = message;
17+
this.code = code;
18+
}
19+
20+
public CommonResult(String message, Integer code) {
21+
this(null, message, code);
22+
}
23+
24+
public CommonResult(T data) {
25+
this(data, "操作成功", 200);
26+
}
27+
28+
public T getData() {
29+
return data;
30+
}
31+
32+
public void setData(T data) {
33+
this.data = data;
34+
}
35+
36+
public String getMessage() {
37+
return message;
38+
}
39+
40+
public void setMessage(String message) {
41+
this.message = message;
42+
}
43+
44+
public Integer getCode() {
45+
return code;
46+
}
47+
48+
public void setCode(Integer code) {
49+
this.code = code;
50+
}
51+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.macro.cloud.domain;
2+
3+
/**
4+
* Created by macro on 2019/8/29.
5+
*/
6+
public class User {
7+
8+
private Long id;
9+
private String username;
10+
private String password;
11+
12+
public User() {
13+
}
14+
15+
public User(Long id, String username, String password) {
16+
this.id = id;
17+
this.username = username;
18+
this.password = password;
19+
}
20+
21+
public Long getId() {
22+
return id;
23+
}
24+
25+
public void setId(Long id) {
26+
this.id = id;
27+
}
28+
29+
public String getUsername() {
30+
return username;
31+
}
32+
33+
public void setUsername(String username) {
34+
this.username = username;
35+
}
36+
37+
public String getPassword() {
38+
return password;
39+
}
40+
41+
public void setPassword(String password) {
42+
this.password = password;
43+
}
44+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.macro.cloud.filter;
2+
3+
import com.netflix.hystrix.strategy.concurrency.HystrixRequestContext;
4+
import org.springframework.stereotype.Component;
5+
6+
import javax.servlet.*;
7+
import javax.servlet.annotation.WebFilter;
8+
import java.io.IOException;
9+
10+
/**
11+
* Created by macro on 2019/9/4.
12+
*/
13+
@Component
14+
@WebFilter(urlPatterns = "/*",asyncSupported = true)
15+
public class HystrixRequestContextFilter implements Filter {
16+
@Override
17+
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
18+
HystrixRequestContext context = HystrixRequestContext.initializeContext();
19+
try {
20+
filterChain.doFilter(servletRequest, servletResponse);
21+
} finally {
22+
context.close();
23+
}
24+
}
25+
}

0 commit comments

Comments
 (0)