Commit d88dabb0 authored by XCQi's avatar XCQi

服务调用线程隔离demo

parent 67355d64
...@@ -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>
......
...@@ -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) {
......
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();
// }
} }
//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;
// }
//}
...@@ -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 HelloFallBack implements HelloService { public class HelloFastFallBack implements HelloFastService {
@Override @Override
public String hello1() { public String hello() {
System.out.println("hello1 线程:" + Thread.currentThread().getName()); System.out.println("fast server error");
return "service1 error"; return "hello error";
} }
@Override @Override
......
...@@ -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 = HelloFallBack.class) @FeignClient(name = "spring-cloud-server-HelloWorldFast", fallback = HelloFastFallBack.class)
public interface HelloService { public interface HelloFastService {
@GetMapping("hello1") @GetMapping("hello")
String hello1(); String hello();
@GetMapping("exampleforget") @GetMapping("exampleforget")
User getUser(); User getUser();
......
//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();
//}
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;
}
}
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();
}
//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 = "TestService") @HystrixCommand(fallbackMethod = "helloFallBack", threadPoolKey = "HelloFastService")
// 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";
// } }
//} }
...@@ -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
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment