本文介绍了如何在Spring Cloud中通过伪装调整负载均衡规则的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
据我所知,假装包含功能区的功能,我在代码中对此进行了证明.
As I know, feign include ribbon's function, and I prove it in my code.
当我使用伪装时,默认规则是Round Robin Rule.但是,如何更改假冒客户代码中的规则,功能区是唯一的方法吗?
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
ConsumerApplication.java
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
@EnableCircuitBreaker
public class ConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class, args);
}
}
UserFeignClient .java
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
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;
}
}
推荐答案
来自文档:
@RibbonClient(name = "cloud-provider", configuration = CloudProviderConfiguration.class)
public class ConsumerApplication {
/* ... */
}
class CloudProviderConfiguration {
@Bean
public IRule ribbonRule(IClientConfig config) {
return new RandomRule();
}
}
这篇关于如何在Spring Cloud中通过伪装调整负载均衡规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!