5

据我所知,feign 包含了功能区的功能,我在我的代码中证明了这一点。

当我使用 feign 时,默认规则是 Round Robin Rule。但是如何更改我的假客户端代码中的规则,功能区是唯一的方法吗?

下面是我的代码,请帮忙。

ConsumerApplication.java

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

用户FeignClient .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

1 回答 1

6

文档中:

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

class CloudProviderConfiguration {
    @Bean
    public IRule ribbonRule(IClientConfig config) {
        return new RandomRule();
    }
}
于 2017-02-28T16:29:50.533 回答