yirenyishi
6 years ago
7 changed files with 112 additions and 7 deletions
@ -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; |
|||
} |
|||
} |
|||
|
@ -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(); |
|||
} |
@ -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]; |
|||
} |
|||
} |
@ -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; |
|||
} |
|||
} |
Loading…
Reference in new issue