Use openFeign to achieve load balancing

1. Introduce dependencies

  <!--openfeign-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>

2. Write and call other microservice interfaces

@Component
@FeignClient(value = "CLOUD-PAYMENT-SERVICE") //这是注册到eureka微服务的名称
public interface PaymentFeignService {
    
    
    @GetMapping(value = "/get")//这是调用的微服务的地址
    public CommonResult<Payment> getById(@RequestParam("id") int id);
}

3. Add this annotation to the main startup class

@EnableFeignClients

4. Call in the control layer


    @Resource
    private PaymentFeignService paymentFeignService;
    
    @GetMapping(value = "/consumer/payment/get")
    public CommonResult<Payment> getById(@RequestParam("id") int id){
    
    
        return paymentFeignService.getById(id);
    }

Guess you like

Origin blog.csdn.net/qq_45432665/article/details/113893046