修改cloud-consumer-order调用cloud-provider-payment接口

修改pom.xml

  1. <dependency>
  2. <groupId>org.springframework.cloud</groupId>
  3. <artifactId>spring-cloud-starter-openfeign</artifactId>
  4. </dependency>

修改OrderMain

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

业务类

新建PaymentFeignService接口并新增注解@FeignClient

@Service
@FeignClient(value = "${service-url.nacos-user-service}")
public interface PaymentFeignService
{
    @GetMapping(value = "/payment/get/{id}")
    public CommonResult<Payment> getPaymentById(@PathVariable("id") Long id);

}

修改OrderController

@Slf4j
@RestController
@RefreshScope //支持Nacos的动态刷新功能。
public class OrderController {
    @Value("${config.info}")
    private String configInfo;

    @Resource
    private PaymentFeignService paymentFeignService;

    @GetMapping(value = "/consumer/payment/get/{id}")
    public CommonResult<Payment> getPaymentById(@PathVariable("id") Long id)
    {
        return paymentFeignService.getPaymentById(id);
    }

    @GetMapping("/config/info")
    @ResponseBody
    public String getConfigInfo() {
        return configInfo;
    }
}

测试

访问:http://localhost:8002/consumer/payment/get/1

4.OpenFeign服务调用 - 图1