Skip to content

Commit 7bd58a1

Browse files
committed
同步
1 parent 1042949 commit 7bd58a1

File tree

20 files changed

+563
-1
lines changed

20 files changed

+563
-1
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
spring.application.name=eureka-consumer-ribbon-hystrix
22
server.port=2101
33

4-
eureka.client.serviceUrl.defaultZone=http://localhost:11001/eureka/
4+
eureka.client.serviceUrl.defaultZone=http://localhost:1001/eureka/
55

66
logging.file=${spring.application.name}.log
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>com.didispace</groupId>
7+
<artifactId>hystrix-collapser-consumer</artifactId>
8+
<version>1.0.0</version>
9+
<packaging>jar</packaging>
10+
11+
<name>hystrix-collapser-consumer</name>
12+
<description>Spring Cloud In Action</description>
13+
14+
<parent>
15+
<groupId>org.springframework.boot</groupId>
16+
<artifactId>spring-boot-starter-parent</artifactId>
17+
<version>1.5.4.RELEASE</version>
18+
<relativePath/>
19+
</parent>
20+
21+
<properties>
22+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
23+
<java.version>1.8</java.version>
24+
</properties>
25+
26+
<dependencies>
27+
<dependency>
28+
<groupId>org.springframework.cloud</groupId>
29+
<artifactId>spring-cloud-starter-eureka</artifactId>
30+
</dependency>
31+
32+
<dependency>
33+
<groupId>org.springframework.boot</groupId>
34+
<artifactId>spring-boot-starter-web</artifactId>
35+
</dependency>
36+
37+
<!--<dependency>-->
38+
<!--<groupId>org.springframework.boot</groupId>-->
39+
<!--<artifactId>spring-boot-starter-actuator</artifactId>-->
40+
<!--</dependency>-->
41+
<dependency>
42+
<groupId>org.springframework.cloud</groupId>
43+
<artifactId>spring-cloud-starter-hystrix</artifactId>
44+
</dependency>
45+
<dependency>
46+
<groupId>org.springframework.boot</groupId>
47+
<artifactId>spring-boot-starter-test</artifactId>
48+
<scope>test</scope>
49+
</dependency>
50+
<dependency>
51+
<groupId>org.projectlombok</groupId>
52+
<artifactId>lombok</artifactId>
53+
</dependency>
54+
</dependencies>
55+
56+
<dependencyManagement>
57+
<dependencies>
58+
<dependency>
59+
<groupId>org.springframework.cloud</groupId>
60+
<artifactId>spring-cloud-dependencies</artifactId>
61+
<version>Dalston.SR1</version>
62+
<type>pom</type>
63+
<scope>import</scope>
64+
</dependency>
65+
</dependencies>
66+
</dependencyManagement>
67+
68+
<build>
69+
<plugins>
70+
<plugin>
71+
<groupId>org.springframework.boot</groupId>
72+
<artifactId>spring-boot-maven-plugin</artifactId>
73+
</plugin>
74+
</plugins>
75+
</build>
76+
77+
</project>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.didispace;
2+
3+
import org.springframework.boot.autoconfigure.SpringBootApplication;
4+
import org.springframework.boot.builder.SpringApplicationBuilder;
5+
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
6+
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
7+
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
8+
import org.springframework.context.annotation.Bean;
9+
import org.springframework.web.client.RestTemplate;
10+
11+
@EnableHystrix
12+
@EnableDiscoveryClient
13+
@SpringBootApplication
14+
public class Application {
15+
16+
@Bean
17+
@LoadBalanced
18+
public RestTemplate restTemplate() {
19+
return new RestTemplate();
20+
}
21+
22+
public static void main(String[] args) {
23+
new SpringApplicationBuilder(Application.class).web(true).run(args);
24+
}
25+
26+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.didispace;
2+
3+
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCollapser;
4+
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
5+
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
6+
import lombok.extern.slf4j.Slf4j;
7+
import org.apache.commons.lang.StringUtils;
8+
import org.springframework.beans.factory.annotation.Autowired;
9+
import org.springframework.stereotype.Service;
10+
import org.springframework.web.client.RestTemplate;
11+
12+
import java.util.ArrayList;
13+
import java.util.List;
14+
import java.util.concurrent.Future;
15+
16+
/**
17+
* @author 翟永超
18+
* @create 2017/7/25.
19+
* @blog http://blog.didispace.com
20+
*/
21+
@Slf4j
22+
@Service
23+
public class UserService {
24+
25+
@Autowired
26+
RestTemplate restTemplate;
27+
28+
@HystrixCollapser(
29+
scope = com.netflix.hystrix.HystrixCollapser.Scope.GLOBAL,
30+
batchMethod = "findByIds",
31+
collapserProperties = {
32+
@HystrixProperty(name="timerDelayInMilliseconds", value = "100")
33+
}
34+
)
35+
public Future<String> findById(Long id) {
36+
log.info("findById : " + id);
37+
return null;
38+
}
39+
40+
@HystrixCommand(commandProperties = @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "5000"))
41+
public List<String> findByIds(List<Long> ids) {
42+
log.info("findByIds : " + ids);
43+
List<String> result = restTemplate.getForObject("http://hystrix-collapser-provider/users?ids={1}",
44+
List.class, StringUtils.join(ids, ","));
45+
log.info(result.toString());
46+
return result;
47+
}
48+
49+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
spring.application.name=hystrix-collapser-consumer
2+
server.port=2002
3+
4+
eureka.client.serviceUrl.defaultZone=http://localhost:1001/eureka/
5+
6+
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import com.didispace.Application;
2+
import com.didispace.UserService;
3+
import lombok.extern.slf4j.Slf4j;
4+
import org.junit.Assert;
5+
import org.junit.Test;
6+
import org.junit.runner.RunWith;
7+
import org.springframework.beans.factory.annotation.Autowired;
8+
import org.springframework.boot.test.context.SpringBootTest;
9+
import org.springframework.test.context.junit4.SpringRunner;
10+
11+
import java.util.concurrent.Future;
12+
13+
/**
14+
* @author 翟永超
15+
* @create 2017/7/25.
16+
* @blog http://blog.didispace.com
17+
*/
18+
@Slf4j
19+
@RunWith(SpringRunner.class)
20+
@SpringBootTest(classes = Application.class)
21+
public class CollapserTest {
22+
23+
@Autowired
24+
UserService userService;
25+
26+
@Test
27+
public void test() throws Exception {
28+
Future<String> u1 = userService.findById(1L);
29+
Future<String> u2 = userService.findById(2L);
30+
Future<String> u3 = userService.findById(3L);
31+
Future<String> u4 = userService.findById(4L);
32+
33+
34+
log.info(u1.get().toString());
35+
log.info(u2.get().toString());
36+
log.info(u3.get().toString());
37+
log.info(u4.get().toString());
38+
39+
40+
Assert.assertEquals("aaa",u1.get());
41+
Assert.assertEquals("bbb",u2.get());
42+
Assert.assertEquals("ccc",u3.get());
43+
Assert.assertEquals("ddd",u4.get());
44+
}
45+
46+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>com.didispace</groupId>
7+
<artifactId>hystrix-collapser-provider</artifactId>
8+
<version>1.0.0</version>
9+
<packaging>jar</packaging>
10+
11+
<name>hystrix-collapser-provider</name>
12+
<description>Spring Cloud In Action</description>
13+
14+
<parent>
15+
<groupId>org.springframework.boot</groupId>
16+
<artifactId>spring-boot-starter-parent</artifactId>
17+
<version>1.5.4.RELEASE</version>
18+
<relativePath/>
19+
</parent>
20+
21+
<properties>
22+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
23+
<java.version>1.8</java.version>
24+
</properties>
25+
26+
<dependencies>
27+
<dependency>
28+
<groupId>org.springframework.cloud</groupId>
29+
<artifactId>spring-cloud-starter-eureka</artifactId>
30+
</dependency>
31+
32+
<dependency>
33+
<groupId>org.springframework.boot</groupId>
34+
<artifactId>spring-boot-starter-web</artifactId>
35+
</dependency>
36+
37+
<!--<dependency>-->
38+
<!--<groupId>org.springframework.boot</groupId>-->
39+
<!--<artifactId>spring-boot-starter-actuator</artifactId>-->
40+
<!--</dependency>-->
41+
<dependency>
42+
<groupId>org.projectlombok</groupId>
43+
<artifactId>lombok</artifactId>
44+
</dependency>
45+
</dependencies>
46+
47+
<dependencyManagement>
48+
<dependencies>
49+
<dependency>
50+
<groupId>org.springframework.cloud</groupId>
51+
<artifactId>spring-cloud-dependencies</artifactId>
52+
<version>Dalston.SR1</version>
53+
<type>pom</type>
54+
<scope>import</scope>
55+
</dependency>
56+
</dependencies>
57+
</dependencyManagement>
58+
59+
<build>
60+
<plugins>
61+
<plugin>
62+
<groupId>org.springframework.boot</groupId>
63+
<artifactId>spring-boot-maven-plugin</artifactId>
64+
</plugin>
65+
</plugins>
66+
</build>
67+
68+
</project>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.didispace;
2+
3+
import org.springframework.boot.autoconfigure.SpringBootApplication;
4+
import org.springframework.boot.builder.SpringApplicationBuilder;
5+
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
6+
7+
@EnableDiscoveryClient
8+
@SpringBootApplication
9+
public class Application {
10+
11+
public static void main(String[] args) {
12+
new SpringApplicationBuilder(Application.class).web(true).run(args);
13+
}
14+
15+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.didispace;
2+
3+
import lombok.extern.slf4j.Slf4j;
4+
import org.springframework.web.bind.annotation.*;
5+
6+
import java.util.ArrayList;
7+
import java.util.HashMap;
8+
import java.util.List;
9+
import java.util.Map;
10+
11+
/**
12+
* @author 翟永超
13+
* @create 2017/4/15.
14+
* @blog http://blog.didispace.com
15+
*/
16+
@Slf4j
17+
@RestController
18+
public class UserController {
19+
20+
private static Map<Long, String> users = new HashMap<>();
21+
22+
static {
23+
users.put(1L, "aaa");
24+
users.put(2L, "bbb");
25+
users.put(3L, "ccc");
26+
users.put(4L, "ddd");
27+
users.put(5L, "eee");
28+
}
29+
30+
@RequestMapping(value = "/users/{id}", method = RequestMethod.GET)
31+
public String findById(@PathVariable Long id) {
32+
log.info("findById : " + id);
33+
return users.get(id);
34+
}
35+
36+
@RequestMapping(value = "/users", method = RequestMethod.GET)
37+
public List<String> findByIds(@RequestParam String ids) {
38+
log.info("findByIds : " + ids);
39+
List<String> result = new ArrayList<>();
40+
for(String id : ids.split(",")) {
41+
if(users.get(Long.valueOf(id)) != null)
42+
result.add(users.get(Long.valueOf(id)));
43+
}
44+
45+
log.info("findByIds : " + result);
46+
return result;
47+
}
48+
49+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
spring.application.name=hystrix-collapser-provider
2+
server.port=2001
3+
4+
eureka.client.serviceUrl.defaultZone=http://localhost:1001/eureka/
5+
6+

0 commit comments

Comments
 (0)