Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
S
SpringCloudServerConsumer
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
xiachenqi
SpringCloudServerConsumer
Commits
d88dabb0
Commit
d88dabb0
authored
Jul 12, 2019
by
XCQi
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
服务调用线程隔离demo
parent
67355d64
Show whitespace changes
Inline
Side-by-side
Showing
11 changed files
with
125 additions
and
129 deletions
+125
-129
pom.xml
pom.xml
+9
-9
SpringCloudConsumerApplication.java
...n/springcloudconsumer/SpringCloudConsumerApplication.java
+3
-3
RestApi.java
...a/com/yingxin/springcloudconsumer/controller/RestApi.java
+23
-27
HelloFallBack2.java
...m/yingxin/springcloudconsumer/service/HelloFallBack2.java
+0
-26
HelloFastFallBack.java
...ingxin/springcloudconsumer/service/HelloFastFallBack.java
+4
-4
HelloFastService.java
...yingxin/springcloudconsumer/service/HelloFastService.java
+4
-4
HelloService2.java
...om/yingxin/springcloudconsumer/service/HelloService2.java
+0
-20
HelloSlowFallBack.java
...ingxin/springcloudconsumer/service/HelloSlowFallBack.java
+26
-0
HelloSlowService.java
...yingxin/springcloudconsumer/service/HelloSlowService.java
+20
-0
TestService.java
.../com/yingxin/springcloudconsumer/service/TestService.java
+32
-32
application.properties
src/main/resources/application.properties
+4
-4
No files found.
pom.xml
View file @
d88dabb0
...
...
@@ -36,15 +36,15 @@
<groupId>
org.springframework.cloud
</groupId>
<artifactId>
spring-cloud-starter-openfeign
</artifactId>
</dependency>
<!-- <dependency>--
>
<!-- <groupId>com.netflix.hystrix</groupId>--
>
<!-- <artifactId>hystrix-javanica</artifactId>--
>
<!-- <version>RELEASE</version>--
>
<!-- </dependency>--
>
<!-- <dependency>--
>
<!-- <groupId>org.springframework.cloud</groupId>--
>
<!-- <artifactId>spring-cloud-netflix-hystrix</artifactId>--
>
<!-- </dependency>--
>
<dependency
>
<groupId>
com.netflix.hystrix
</groupId
>
<artifactId>
hystrix-javanica
</artifactId
>
<version>
RELEASE
</version
>
</dependency
>
<dependency
>
<groupId>
org.springframework.cloud
</groupId
>
<artifactId>
spring-cloud-netflix-hystrix
</artifactId
>
</dependency
>
<dependency>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-starter-test
</artifactId>
...
...
src/main/java/com/yingxin/springcloudconsumer/SpringCloudConsumerApplication.java
View file @
d88dabb0
...
...
@@ -3,12 +3,12 @@ package com.yingxin.springcloudconsumer;
import
org.springframework.boot.SpringApplication
;
import
org.springframework.boot.autoconfigure.SpringBootApplication
;
import
org.springframework.cloud.netflix.eureka.EnableEurekaClient
;
import
org.springframework.cloud.
openfeign.EnableFeignClients
;
import
org.springframework.cloud.
netflix.hystrix.EnableHystrix
;
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
//
@EnableHystrix
//
@EnableFeignClients
@EnableHystrix
public
class
SpringCloudConsumerApplication
{
public
static
void
main
(
String
[]
args
)
{
...
...
src/main/java/com/yingxin/springcloudconsumer/controller/RestApi.java
View file @
d88dabb0
package
com
.
yingxin
.
springcloudconsumer
.
controller
;
import
com.yingxin.springcloudconsumer.entity.User
;
import
com.yingxin.springcloudconsumer.service.HelloService
;
import
com.yingxin.springcloudconsumer.service.HelloFastService
;
import
com.yingxin.springcloudconsumer.service.HelloSlowService
;
import
com.yingxin.springcloudconsumer.service.TestService
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.web.bind.annotation.GetMapping
;
import
org.springframework.web.bind.annotation.RestController
;
...
...
@@ -11,41 +13,35 @@ import java.util.List;
@RestController
public
class
RestApi
{
//
@Autowired
//
private TestService service;
@Autowired
private
TestService
service
;
//
// @GetMapping("/test")
// public String test(){
// return service.hello1();
// }
// private final HelloFastService helloFastService;
// private final HelloSlowService helloSlowService;
//
//
@GetMapping("/test2")
//
public String test2(){
//
return service.hello2()
;
//
public RestApi(HelloFastService helloFastService, HelloSlowService helloSlowService) {
//
this.helloFastService = helloFastService;
//
this.helloSlowService = helloSlowService
;
// }
@Autowired
private
HelloService
helloService
;
// @Autowired
// private HelloService2 helloService2;
@GetMapping
(
"/hello1"
)
public
String
testHello1
()
{
return
helloService
.
hello1
();
return
service
.
hello
();
}
// @GetMapping("/hello2")
// public String testHello2() {
// return helloService2.hello2();
// }
@GetMapping
(
"user"
)
public
User
userConsumer
()
{
return
helloService
.
getUser
();
@GetMapping
(
"/hello2"
)
public
String
testHello2
()
{
return
service
.
hello2
();
}
@GetMapping
(
"users"
)
public
List
<
User
>
usersConsumer
()
{
return
helloService
.
getUsers
();
}
// @GetMapping("user")
// public User userConsumer() {
// return helloFastService.getUser();
// }
//
// @GetMapping("users")
// public List<User> usersConsumer() {
// return helloFastService.getUsers();
// }
}
src/main/java/com/yingxin/springcloudconsumer/service/HelloFallBack2.java
deleted
100644 → 0
View file @
67355d64
//package com.yingxin.springcloudconsumer.service;
//
//import com.yingxin.springcloudconsumer.entity.User;
//import org.springframework.stereotype.Component;
//
//import java.util.List;
//
//@Component
//public class HelloFallBack2 implements HelloService2 {
//
// @Override
// public String hello2() {
// System.out.println("hello2 线程:" + Thread.currentThread().getName());
// return "service2 error";
// }
//
// @Override
// public User getUser() {
// return new User();
// }
//
// @Override
// public List<User> getUsers() {
// return null;
// }
//}
src/main/java/com/yingxin/springcloudconsumer/service/HelloFallBack.java
→
src/main/java/com/yingxin/springcloudconsumer/service/HelloFa
stFa
llBack.java
View file @
d88dabb0
...
...
@@ -6,11 +6,11 @@ import org.springframework.stereotype.Component;
import
java.util.List
;
@Component
public
class
HelloFa
llBack
implements
Hello
Service
{
public
class
HelloFa
stFallBack
implements
HelloFast
Service
{
@Override
public
String
hello
1
()
{
System
.
out
.
println
(
"
hello1 线程:"
+
Thread
.
currentThread
().
getName
()
);
return
"
service1
error"
;
public
String
hello
()
{
System
.
out
.
println
(
"
fast server error"
);
return
"
hello
error"
;
}
@Override
...
...
src/main/java/com/yingxin/springcloudconsumer/service/HelloService.java
→
src/main/java/com/yingxin/springcloudconsumer/service/Hello
Fast
Service.java
View file @
d88dabb0
...
...
@@ -6,11 +6,11 @@ import org.springframework.web.bind.annotation.GetMapping;
import
java.util.List
;
@FeignClient
(
name
=
"spring-cloud-server-HelloWorld
"
,
fallback
=
Hello
FallBack
.
class
)
public
interface
HelloService
{
@FeignClient
(
name
=
"spring-cloud-server-HelloWorld
Fast"
,
fallback
=
HelloFast
FallBack
.
class
)
public
interface
Hello
Fast
Service
{
@GetMapping
(
"hello
1
"
)
String
hello
1
();
@GetMapping
(
"hello"
)
String
hello
();
@GetMapping
(
"exampleforget"
)
User
getUser
();
...
...
src/main/java/com/yingxin/springcloudconsumer/service/HelloService2.java
deleted
100644 → 0
View file @
67355d64
//package com.yingxin.springcloudconsumer.service;
//
//import com.yingxin.springcloudconsumer.entity.User;
//import org.springframework.cloud.openfeign.FeignClient;
//import org.springframework.web.bind.annotation.GetMapping;
//
//import java.util.List;
//
//@FeignClient(name = "spring-cloud-server-HelloWorld", fallback = HelloFallBack2.class)
//public interface HelloService2 {
//
// @GetMapping("hello2")
// String hello2();
//
// @GetMapping("exampleforget")
// User getUser();
//
// @GetMapping("queryUserList")
// List<User> getUsers();
//}
src/main/java/com/yingxin/springcloudconsumer/service/HelloSlowFallBack.java
0 → 100644
View file @
d88dabb0
package
com
.
yingxin
.
springcloudconsumer
.
service
;
import
com.yingxin.springcloudconsumer.entity.User
;
import
org.springframework.stereotype.Component
;
import
java.util.List
;
@Component
public
class
HelloSlowFallBack
implements
HelloSlowService
{
@Override
public
String
hello
()
{
System
.
out
.
println
(
"slow server error."
);
return
"service error"
;
}
@Override
public
User
getUser
()
{
return
new
User
();
}
@Override
public
List
<
User
>
getUsers
()
{
return
null
;
}
}
src/main/java/com/yingxin/springcloudconsumer/service/HelloSlowService.java
0 → 100644
View file @
d88dabb0
package
com
.
yingxin
.
springcloudconsumer
.
service
;
import
com.yingxin.springcloudconsumer.entity.User
;
import
org.springframework.cloud.openfeign.FeignClient
;
import
org.springframework.web.bind.annotation.GetMapping
;
import
java.util.List
;
@FeignClient
(
name
=
"spring-cloud-server-HelloWorldSlow"
,
fallback
=
HelloSlowFallBack
.
class
)
public
interface
HelloSlowService
{
@GetMapping
(
"hello"
)
String
hello
();
@GetMapping
(
"exampleforget"
)
User
getUser
();
@GetMapping
(
"queryUserList"
)
List
<
User
>
getUsers
();
}
src/main/java/com/yingxin/springcloudconsumer/service/TestService.java
View file @
d88dabb0
//
package com.yingxin.springcloudconsumer.service;
//
//
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
//
import org.springframework.stereotype.Service;
//
import org.springframework.web.client.RestTemplate;
//
//
@Service
//
public class TestService {
//
//
private final RestTemplate restTemplate;
//
//
public TestService(RestTemplate restTemplate) {
//
this.restTemplate = restTemplate;
//
}
//
// @HystrixCommand(fallbackMethod = "helloFallBack", threadPoolKey = "Te
stService")
//
public String hello() {
//
System.out.println("执行任务线程:" + Thread.currentThread().getName());
// return restTemplate.getForObject("http://spring-cloud-server-HelloWorld/hello1
",String.class);
//
}
//
// @HystrixCommand(fallbackMethod = "helloFallBack", threadPoolKey = "TestService2
")
//
public String hello2() {
//
System.out.println("执行任务线程:" + Thread.currentThread().getName());
// return restTemplate.getForObject("http://spring-cloud-server-HelloWorld/hello2
",String.class);
//
}
//
//
public String helloFallBack() {
// System.out.println("执行error任务线程
:" + Thread.currentThread().getName());
//
return "error";
//
}
//
}
package
com
.
yingxin
.
springcloudconsumer
.
service
;
import
com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand
;
import
org.springframework.stereotype.Service
;
import
org.springframework.web.client.RestTemplate
;
@Service
public
class
TestService
{
private
final
RestTemplate
restTemplate
;
public
TestService
(
RestTemplate
restTemplate
)
{
this
.
restTemplate
=
restTemplate
;
}
@HystrixCommand
(
fallbackMethod
=
"helloFallBack"
,
threadPoolKey
=
"HelloFa
stService"
)
public
String
hello
()
{
System
.
out
.
println
(
"执行任务线程:"
+
Thread
.
currentThread
().
getName
());
return
restTemplate
.
getForObject
(
"http://spring-cloud-server-HelloWorldFast/hello
"
,
String
.
class
);
}
@HystrixCommand
(
fallbackMethod
=
"helloFallBack"
,
threadPoolKey
=
"HelloSlowService
"
)
public
String
hello2
()
{
System
.
out
.
println
(
"执行任务线程:"
+
Thread
.
currentThread
().
getName
());
return
restTemplate
.
getForObject
(
"http://spring-cloud-server-HelloWorldSlow/hello
"
,
String
.
class
);
}
public
String
helloFallBack
()
{
System
.
out
.
println
(
"error
:"
+
Thread
.
currentThread
().
getName
());
return
"error"
;
}
}
src/main/resources/application.properties
View file @
d88dabb0
...
...
@@ -2,6 +2,6 @@ server.port=9000
spring.application.name
=
consumer
eureka.client.service-url.defaultZone
=
http://localhost:8761/eureka/
feign.hystrix.enabled
=
true
hystrix.threadpool.default.coreSize
=
5
hystrix.threadpool.HelloService.coreSize
=
10
#
hystrix.threadpool.HelloService2.coreSize
=
5
\ No newline at end of file
hystrix.threadpool.default.coreSize
=
1
hystrix.threadpool.HelloFastService.coreSize
=
5
hystrix.threadpool.HelloSlowService.coreSize
=
3
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment