服务接口调用OpenFeign_入门案列

构建cloud-consumer-feign-order80工程

服务接口调用OpenFeign_入门案列_第1张图片

修改POM文件



  org.springframework.cloud
  spring-cloud-starter-openfeign

编写YML文件

eureka:
  client:
  # 表示是否将自己注册到Eureka Server
   register-with-eureka: true
  # 示是否从Eureka Server获取注册的服务信息
   fetch-registry: true
  # Eureka Server地址
   service-url:
    defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka
  instance:
   instance-id: cloud-openfeign-order-consumer
   prefer-ip-address: true
spring:
  application:
  # 设置应用名词
   name: cloud-openfeign-order-consumer
server:
  port: 80

编写主启动类

/**
 * 主启动类
 */
@Slf4j
@SpringBootApplication
# 
@EnableFeignClients
public class OrderFeignMain80 {
  public static void main(String[] args) {
    SpringApplication.run(OrderFeignMain80.class,args);
    log.info("************** OrderFeignMain80 服务启动成功  **********");
   }
}

编写业务逻辑接口PaymentFeignService

/**
 * 支付远程调用Feign接口
 */
@Component
@FeignClient(value = "cloud-payment-provider")
public interface PaymentFeignService {


   @GetMapping("/payment/index")
   String index();


}

编写控制层Controller

/**
 * 订单控制层
 */
@RestController
@RequestMapping("/order")
public class OrderController {


  @Autowired
  private PaymentFeignService paymentFeignService;


  /**
   * 测试OpenFeign接口调用
   * @return
   */
  @GetMapping("/index")
  public String get(){
    return paymentFeignService.index();
   }
}

测试

  1. 先启动2个Eureka集群7001/7002
  2. 在启动服务提供者payment8001
  3. 在启动服务消费者cloud-consumer-feign-order
  4. 浏览器访问http://localhost/order/index

你可能感兴趣的:(java,开发语言)