Commit 67355d64 authored by XCQi's avatar XCQi

直接使用hystrix可以实现线程隔离

改为尝试使用feign实现线程隔离
parent 5ba23bfe
...@@ -16,7 +16,7 @@ ...@@ -16,7 +16,7 @@
<properties> <properties>
<java.version>1.8</java.version> <java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR1</spring-cloud.version> <spring-cloud.version>Greenwich.SR2</spring-cloud.version>
</properties> </properties>
<dependencies> <dependencies>
...@@ -37,6 +37,11 @@ ...@@ -37,6 +37,11 @@
<artifactId>spring-cloud-starter-openfeign</artifactId> <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency> </dependency>
<!-- <dependency>--> <!-- <dependency>-->
<!-- <groupId>com.netflix.hystrix</groupId>-->
<!-- <artifactId>hystrix-javanica</artifactId>-->
<!-- <version>RELEASE</version>-->
<!-- </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>-->
......
...@@ -8,6 +8,7 @@ import org.springframework.cloud.openfeign.EnableFeignClients; ...@@ -8,6 +8,7 @@ import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication @SpringBootApplication
@EnableEurekaClient @EnableEurekaClient
@EnableFeignClients @EnableFeignClients
//@EnableHystrix
public class SpringCloudConsumerApplication { public class SpringCloudConsumerApplication {
public static void main(String[] args) { public static void main(String[] args) {
......
package com.yingxin.springcloudconsumer.config;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
@Configuration
public class RestTemplateConfig {
@Bean
@LoadBalanced
RestTemplate restTemplate() {
return new RestTemplate();
}
}
...@@ -2,6 +2,7 @@ package com.yingxin.springcloudconsumer.controller; ...@@ -2,6 +2,7 @@ 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.HelloService;
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;
...@@ -10,16 +11,32 @@ import java.util.List; ...@@ -10,16 +11,32 @@ import java.util.List;
@RestController @RestController
public class RestApi { public class RestApi {
private final HelloService helloService; // @Autowired
// private TestService service;
public RestApi(HelloService helloService) { //
this.helloService = helloService; // @GetMapping("/test")
// public String test(){
// return service.hello1();
// }
//
// @GetMapping("/test2")
// public String test2(){
// return service.hello2();
// }
@Autowired
private HelloService helloService;
// @Autowired
// private HelloService2 helloService2;
@GetMapping("/hello1")
public String testHello1() {
return helloService.hello1();
} }
@GetMapping("/consumer") // @GetMapping("/hello2")
public String helloConsumer() { // public String testHello2() {
return helloService.hello(); // return helloService2.hello2();
} // }
@GetMapping("user") @GetMapping("user")
public User userConsumer() { public User userConsumer() {
......
...@@ -8,8 +8,9 @@ import java.util.List; ...@@ -8,8 +8,9 @@ import java.util.List;
@Component @Component
public class HelloFallBack implements HelloService { public class HelloFallBack implements HelloService {
@Override @Override
public String hello() { public String hello1() {
return "service error"; System.out.println("hello1 线程:" + Thread.currentThread().getName());
return "service1 error";
} }
@Override @Override
......
//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;
// }
//}
...@@ -9,8 +9,8 @@ import java.util.List; ...@@ -9,8 +9,8 @@ import java.util.List;
@FeignClient(name = "spring-cloud-server-HelloWorld", fallback = HelloFallBack.class) @FeignClient(name = "spring-cloud-server-HelloWorld", fallback = HelloFallBack.class)
public interface HelloService { public interface HelloService {
@GetMapping("hello") @GetMapping("hello1")
String hello(); String hello1();
@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.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";
// }
//}
...@@ -2,3 +2,6 @@ server.port=9000 ...@@ -2,3 +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.HelloService.coreSize=10
#hystrix.threadpool.HelloService2.coreSize=5
\ 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