Commit d88dabb0 authored by XCQi's avatar XCQi

服务调用线程隔离demo

parent 67355d64
......@@ -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>
......
......@@ -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) {
......
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();
// }
}
//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;
import java.util.List;
@Component
public class HelloFallBack implements HelloService {
public class HelloFastFallBack implements HelloFastService {
@Override
public String hello1() {
System.out.println("hello1 线程:" + Thread.currentThread().getName());
return "service1 error";
public String hello() {
System.out.println("fast server error");
return "hello error";
}
@Override
......
......@@ -6,11 +6,11 @@ import org.springframework.web.bind.annotation.GetMapping;
import java.util.List;
@FeignClient(name = "spring-cloud-server-HelloWorld", fallback = HelloFallBack.class)
public interface HelloService {
@FeignClient(name = "spring-cloud-server-HelloWorldFast", fallback = HelloFastFallBack.class)
public interface HelloFastService {
@GetMapping("hello1")
String hello1();
@GetMapping("hello")
String hello();
@GetMapping("exampleforget")
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;
//
//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 = "TestService")
// 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 = "HelloFastService")
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";
}
}
......@@ -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
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