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
Hide 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 @@
...
@@ -36,15 +36,15 @@
<groupId>
org.springframework.cloud
</groupId>
<groupId>
org.springframework.cloud
</groupId>
<artifactId>
spring-cloud-starter-openfeign
</artifactId>
<artifactId>
spring-cloud-starter-openfeign
</artifactId>
</dependency>
</dependency>
<!-- <dependency>--
>
<dependency
>
<!-- <groupId>com.netflix.hystrix</groupId>--
>
<groupId>
com.netflix.hystrix
</groupId
>
<!-- <artifactId>hystrix-javanica</artifactId>--
>
<artifactId>
hystrix-javanica
</artifactId
>
<!-- <version>RELEASE</version>--
>
<version>
RELEASE
</version
>
<!-- </dependency>--
>
</dependency
>
<!-- <dependency>--
>
<dependency
>
<!-- <groupId>org.springframework.cloud</groupId>--
>
<groupId>
org.springframework.cloud
</groupId
>
<!-- <artifactId>spring-cloud-netflix-hystrix</artifactId>--
>
<artifactId>
spring-cloud-netflix-hystrix
</artifactId
>
<!-- </dependency>--
>
</dependency
>
<dependency>
<dependency>
<groupId>
org.springframework.boot
</groupId>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-starter-test
</artifactId>
<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;
...
@@ -3,12 +3,12 @@ package com.yingxin.springcloudconsumer;
import
org.springframework.boot.SpringApplication
;
import
org.springframework.boot.SpringApplication
;
import
org.springframework.boot.autoconfigure.SpringBootApplication
;
import
org.springframework.boot.autoconfigure.SpringBootApplication
;
import
org.springframework.cloud.netflix.eureka.EnableEurekaClient
;
import
org.springframework.cloud.netflix.eureka.EnableEurekaClient
;
import
org.springframework.cloud.
openfeign.EnableFeignClients
;
import
org.springframework.cloud.
netflix.hystrix.EnableHystrix
;
@SpringBootApplication
@SpringBootApplication
@EnableEurekaClient
@EnableEurekaClient
@EnableFeignClients
//
@EnableFeignClients
//
@EnableHystrix
@EnableHystrix
public
class
SpringCloudConsumerApplication
{
public
class
SpringCloudConsumerApplication
{
public
static
void
main
(
String
[]
args
)
{
public
static
void
main
(
String
[]
args
)
{
...
...
src/main/java/com/yingxin/springcloudconsumer/controller/RestApi.java
View file @
d88dabb0
package
com
.
yingxin
.
springcloudconsumer
.
controller
;
package
com
.
yingxin
.
springcloudconsumer
.
controller
;
import
com.yingxin.springcloudconsumer.entity.User
;
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.beans.factory.annotation.Autowired
;
import
org.springframework.web.bind.annotation.GetMapping
;
import
org.springframework.web.bind.annotation.GetMapping
;
import
org.springframework.web.bind.annotation.RestController
;
import
org.springframework.web.bind.annotation.RestController
;
...
@@ -11,41 +13,35 @@ import java.util.List;
...
@@ -11,41 +13,35 @@ import java.util.List;
@RestController
@RestController
public
class
RestApi
{
public
class
RestApi
{
//
@Autowired
@Autowired
//
private TestService service;
private
TestService
service
;
//
//
// @GetMapping("/test")
// private final HelloFastService helloFastService;
// public String test(){
// private final HelloSlowService helloSlowService;
// return service.hello1();
// }
//
//
//
@GetMapping("/test2")
//
public RestApi(HelloFastService helloFastService, HelloSlowService helloSlowService) {
//
public String test2(){
//
this.helloFastService = helloFastService;
//
return service.hello2()
;
//
this.helloSlowService = helloSlowService
;
// }
// }
@Autowired
private
HelloService
helloService
;
// @Autowired
// private HelloService2 helloService2;
@GetMapping
(
"/hello1"
)
@GetMapping
(
"/hello1"
)
public
String
testHello1
()
{
public
String
testHello1
()
{
return
helloService
.
hello1
();
return
service
.
hello
();
}
}
// @GetMapping("/hello2")
@GetMapping
(
"/hello2"
)
// public String testHello2() {
public
String
testHello2
()
{
// return helloService2.hello2();
return
service
.
hello2
();
// }
@GetMapping
(
"user"
)
public
User
userConsumer
()
{
return
helloService
.
getUser
();
}
}
@GetMapping
(
"users"
)
// @GetMapping("user")
public
List
<
User
>
usersConsumer
()
{
// public User userConsumer() {
return
helloService
.
getUsers
();
// 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;
...
@@ -6,11 +6,11 @@ import org.springframework.stereotype.Component;
import
java.util.List
;
import
java.util.List
;
@Component
@Component
public
class
HelloFa
llBack
implements
Hello
Service
{
public
class
HelloFa
stFallBack
implements
HelloFast
Service
{
@Override
@Override
public
String
hello
1
()
{
public
String
hello
()
{
System
.
out
.
println
(
"
hello1 线程:"
+
Thread
.
currentThread
().
getName
()
);
System
.
out
.
println
(
"
fast server error"
);
return
"
service1
error"
;
return
"
hello
error"
;
}
}
@Override
@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;
...
@@ -6,11 +6,11 @@ import org.springframework.web.bind.annotation.GetMapping;
import
java.util.List
;
import
java.util.List
;
@FeignClient
(
name
=
"spring-cloud-server-HelloWorld
"
,
fallback
=
Hello
FallBack
.
class
)
@FeignClient
(
name
=
"spring-cloud-server-HelloWorld
Fast"
,
fallback
=
HelloFast
FallBack
.
class
)
public
interface
HelloService
{
public
interface
Hello
Fast
Service
{
@GetMapping
(
"hello
1
"
)
@GetMapping
(
"hello"
)
String
hello
1
();
String
hello
();
@GetMapping
(
"exampleforget"
)
@GetMapping
(
"exampleforget"
)
User
getUser
();
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;
package
com
.
yingxin
.
springcloudconsumer
.
service
;
//
//
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import
com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand
;
//
import org.springframework.stereotype.Service;
import
org.springframework.stereotype.Service
;
//
import org.springframework.web.client.RestTemplate;
import
org.springframework.web.client.RestTemplate
;
//
//
@Service
@Service
//
public class TestService {
public
class
TestService
{
//
//
private final RestTemplate restTemplate;
private
final
RestTemplate
restTemplate
;
//
//
public TestService(RestTemplate restTemplate) {
public
TestService
(
RestTemplate
restTemplate
)
{
//
this.restTemplate = restTemplate;
this
.
restTemplate
=
restTemplate
;
//
}
}
//
// @HystrixCommand(fallbackMethod = "helloFallBack", threadPoolKey = "Te
stService")
@HystrixCommand
(
fallbackMethod
=
"helloFallBack"
,
threadPoolKey
=
"HelloFa
stService"
)
//
public String hello() {
public
String
hello
()
{
//
System.out.println("执行任务线程:" + Thread.currentThread().getName());
System
.
out
.
println
(
"执行任务线程:"
+
Thread
.
currentThread
().
getName
());
// return restTemplate.getForObject("http://spring-cloud-server-HelloWorld/hello1
",String.class);
return
restTemplate
.
getForObject
(
"http://spring-cloud-server-HelloWorldFast/hello
"
,
String
.
class
);
//
}
}
//
// @HystrixCommand(fallbackMethod = "helloFallBack", threadPoolKey = "TestService2
")
@HystrixCommand
(
fallbackMethod
=
"helloFallBack"
,
threadPoolKey
=
"HelloSlowService
"
)
//
public String hello2() {
public
String
hello2
()
{
//
System.out.println("执行任务线程:" + Thread.currentThread().getName());
System
.
out
.
println
(
"执行任务线程:"
+
Thread
.
currentThread
().
getName
());
// return restTemplate.getForObject("http://spring-cloud-server-HelloWorld/hello2
",String.class);
return
restTemplate
.
getForObject
(
"http://spring-cloud-server-HelloWorldSlow/hello
"
,
String
.
class
);
//
}
}
//
//
public String helloFallBack() {
public
String
helloFallBack
()
{
// System.out.println("执行error任务线程
:" + Thread.currentThread().getName());
System
.
out
.
println
(
"error
:"
+
Thread
.
currentThread
().
getName
());
//
return "error";
return
"error"
;
//
}
}
//
}
}
src/main/resources/application.properties
View file @
d88dabb0
...
@@ -2,6 +2,6 @@ server.port=9000
...
@@ -2,6 +2,6 @@ server.port=9000
spring.application.name
=
consumer
spring.application.name
=
consumer
eureka.client.service-url.defaultZone
=
http://localhost:8761/eureka/
eureka.client.service-url.defaultZone
=
http://localhost:8761/eureka/
feign.hystrix.enabled
=
true
feign.hystrix.enabled
=
true
hystrix.threadpool.default.coreSize
=
5
hystrix.threadpool.default.coreSize
=
1
hystrix.threadpool.HelloService.coreSize
=
10
hystrix.threadpool.HelloFastService.coreSize
=
5
#
hystrix.threadpool.HelloService2.coreSize
=
5
hystrix.threadpool.HelloSlowService.coreSize
=
3
\ No newline at end of file
\ 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