2017-02-28 16 views
1

私が知っているように、feignにはリボンの機能が含まれています。 私がfeignを使うとき、デフォルトルールはRound Robin Ruleです。 しかし、私のクライアントコードでルールを変更するには、リボンは唯一の方法ですか? 以下は私のコードです。 誰かが助けてください!spring cloudのfeignで負荷分散ルールを調整するにはどうすればいいですか

ConsumerApplication.java

@SpringBootApplication 
@EnableDiscoveryClient 
@EnableFeignClients 
@EnableCircuitBreaker 
public class ConsumerApplication { 
    public static void main(String[] args) { 
     SpringApplication.run(ConsumerApplication.class, args); 
    } 
} 

documentationからUserFeignClientの.javaファイル

@FeignClient(name = "cloud-provider", fallback = UserFeignClient.HystrixClientFallback.class) 
public interface UserFeignClient { 
    @RequestMapping("/{id}") 
    BaseResponse findByIdFeign(@RequestParam("id") Long id); 

    @RequestMapping("/add") 
    BaseResponse addUserFeign(UserVo userVo); 

    @Component 
    class HystrixClientFallback implements UserFeignClient { 
     private static final Logger LOGGER = LoggerFactory.getLogger(HystrixClientFallback.class); 

     @Override 
     public BaseResponse findByIdFeign(@RequestParam("id") Long id) { 
      BaseResponse response = new BaseResponse(); 
      response.setMessage("disable"); 
      return response; 
     } 

     @Override 
     public BaseResponse addUserFeign(UserVo userVo) { 
      BaseResponse response = new BaseResponse(); 
      response.setMessage("disable"); 
      return response; 
     } 
    } 
} 

FeignController.java

@RestController 
public class FeignController { 

    @Autowired 
    private UserFeignClient userFeignClient; 

    @GetMapping("feign/{id}") 
    public BaseResponse<Date> findByIdFeign(@PathVariable Long id) { 
     BaseResponse response = this.userFeignClient.findByIdFeign(id); 
     return response; 
    } 

    @GetMapping("feign/user/add") 
    public BaseResponse<Date> addUser() { 
     UserVo userVo = new UserVo(); 
     userVo.setAge(19); 
     userVo.setId(12345L); 
     userVo.setUsername("nick name"); 
     BaseResponse response = this.userFeignClient.addUserFeign(userVo); 
     return response; 
    } 
} 

答えて

4

@RibbonClient(name = "cloud-provider", configuration = CloudProviderConfiguration.class) 
public class ConsumerApplication { 
    /* ... */ 
} 

class CloudProviderConfiguration { 
    @Bean 
    public IRule ribbonRule(IClientConfig config) { 
     return new RandomRule(); 
    } 
} 
+0

それは機能します!ありがとうございました ! @spencergibb –

+0

複数のプロバイダがある場合、どのようにRibbonClientを設定できますか? –

+2

私はそれを得た。 @RibbonClientsを使用してください。 –

関連する問題