Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I adjust load balancing rule by feign in spring cloud

As I know, feign include ribbon's function, and I prove it in my code.

When I use feign, the default rule is Round Robin Rule. But how can I change the rule in my feign client code, is ribbon the only way?

Here is my code below, so please help.

ConsumerApplication.java

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

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;
    }
}
like image 963
Gabriel.ge Avatar asked Feb 28 '17 09:02

Gabriel.ge


People also ask

Does feign client do load balancing?

Feign is a Java HTTP client that is implemented in Java to simplify RESTful API calls. Use @EnableFeignClients and @FeignClient to initiate a load balancing request.

How load balancing is implemented in spring cloud?

Loadbalance Across Server Instances This means that, whenever a service named say-hello is contacted, instead of running with the default setup, Spring Cloud LoadBalancer uses the configuration provided in SayHelloConfiguration.

Does spring cloud gateway do load balancing?

Using Spring Cloud Load Balancer, we can easily create applications that use various load balancing techniques to distribute requests to different service instances.

How do you handle load balancing in microservices?

Two types of load balancing microservices architecture are used: server side load balancing and client side load balancing. Server side load balancing involves the classical load balancing approach wherein the traffic is distributed using a load distributor that is placed in front of the servers.


1 Answers

From the documentation:

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

class CloudProviderConfiguration {
    @Bean
    public IRule ribbonRule(IClientConfig config) {
        return new RandomRule();
    }
}
like image 150
spencergibb Avatar answered Sep 28 '22 11:09

spencergibb