Browse Source

feign 测试通过 & hystrix 测试通过

master
yirenyishi 6 years ago
parent
commit
12486dce75
  1. 8
      sc-consumer/src/main/java/com/trgis/sb2sc/Sb2scConsumerApplication.java
  2. 27
      sc-consumer/src/main/java/com/trgis/sb2sc/controller/FeignController.java
  3. 6
      sc-consumer/src/main/java/com/trgis/sb2sc/controller/RibbonController.java
  4. 21
      sc-consumer/src/main/java/com/trgis/sb2sc/feign/MFeignClient.java
  5. 24
      sc-consumer/src/main/java/com/trgis/sb2sc/feign/MFeignClientFallback.java
  6. 13
      sc-consumer/src/main/java/com/trgis/sb2sc/feign/config/MFeignConfig.java
  7. 20
      sc-provider/src/main/java/com/trgis/sb2sc/controller/IndexController.java

8
sc-consumer/src/main/java/com/trgis/sb2sc/Sb2scConsumerApplication.java

@ -4,12 +4,20 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@EnableDiscoveryClient
//@EnableEurekaClient
@EnableFeignClients
@EnableCircuitBreaker
@EnableHystrix
@SpringBootApplication
public class Sb2scConsumerApplication {

27
sc-consumer/src/main/java/com/trgis/sb2sc/controller/FeignController.java

@ -1,25 +1,40 @@
package com.trgis.sb2sc.controller;
import com.trgis.sb2sc.feign.MFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import java.util.List;
@RestController
public class FeignController {
@Autowired
private RestTemplate restTemplate;
@Autowired
private LoadBalancerClient loadBalancerClient;
private MFeignClient feignClient;
@GetMapping("/feign/{wd}")
public Mono<String> sayHelloWorld(@PathVariable("wd") String parm) {
String res = this.restTemplate.getForObject("http://sc-provider/test/" + parm, String.class);
return Mono.just(res);
String result = feignClient.sayHelloWorld(parm);
return Mono.just(result);
}
@GetMapping("/feign/list")
public Flux<Integer> list() {
List<Integer> list = feignClient.list();
Flux<Integer> userFlux = Flux.fromIterable(list);
return userFlux;
}
@GetMapping("/feign/array")
public Flux<Integer> array() {
Integer[] arrays = feignClient.array();
Flux<Integer> userFlux = Flux.fromArray(arrays);
return userFlux;
}
}

6
sc-consumer/src/main/java/com/trgis/sb2sc/controller/RibbonController.java

@ -1,5 +1,6 @@
package com.trgis.sb2sc.controller;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
@ -18,8 +19,13 @@ public class RibbonController {
private LoadBalancerClient loadBalancerClient;
@GetMapping("/ribbon/{wd}")
@HystrixCommand(fallbackMethod="fallbackMethod")
public Mono<String> sayHelloWorld(@PathVariable("wd") String parm) {
String res = this.restTemplate.getForObject("http://sc-provider/test/" + parm, String.class);
return Mono.just(res);
}
public Mono<String> fallbackMethod(@PathVariable("wd") String parm) {
return Mono.just("fallback");
}
}

21
sc-consumer/src/main/java/com/trgis/sb2sc/feign/MFeignClient.java

@ -0,0 +1,21 @@
package com.trgis.sb2sc.feign;
import com.trgis.sb2sc.feign.config.MFeignConfig;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import java.util.List;
@FeignClient(name = "sc-provider",fallback = MFeignClientFallback.class, configuration = MFeignConfig.class)
public interface MFeignClient {
// 这是被请求微服务的地址,也就是provider的地址
@GetMapping(value = "/test/{msg}")
String sayHelloWorld(@PathVariable("msg") String msg);
@GetMapping(value = "/test/list")
List<Integer> list();
@GetMapping(value = "/test/list")
Integer[] array();
}

24
sc-consumer/src/main/java/com/trgis/sb2sc/feign/MFeignClientFallback.java

@ -0,0 +1,24 @@
package com.trgis.sb2sc.feign;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.List;
@Component
public class MFeignClientFallback implements MFeignClient{
@Override
public String sayHelloWorld(String msg) {
return "fallback";
}
@Override
public List<Integer> list() {
return new ArrayList<>();
}
@Override
public Integer[] array() {
return new Integer[0];
}
}

13
sc-consumer/src/main/java/com/trgis/sb2sc/feign/config/MFeignConfig.java

@ -0,0 +1,13 @@
package com.trgis.sb2sc.feign.config;
import feign.Logger;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MFeignConfig {
@Bean
Logger.Level feignLoggerLevel() {
return Logger.Level.FULL;
}
}

20
sc-provider/src/main/java/com/trgis/sb2sc/controller/IndexController.java

@ -2,14 +2,32 @@ package com.trgis.sb2sc.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import java.util.ArrayList;
import java.util.List;
@RestController
@RequestMapping("test")
public class IndexController {
@GetMapping("/test/{msg}")
@GetMapping("{msg}")
public Mono<String> sayHelloWorld(@PathVariable("msg") String msg) {
System.out.println("come on " + msg);
return Mono.just("sc-provider receive : " +msg);
}
@GetMapping("list")
public Flux<Integer> list() {
List<Integer> list = new ArrayList<>();
list.add(8);
list.add(22);
list.add(75);
list.add(93);
Flux<Integer> userFlux = Flux.fromIterable(list);
return userFlux;
}
}

Loading…
Cancel
Save