SpringCloud之服务调用(feign)

前言

前一篇介绍了使用Ribbon的RestTemplate进行服务调用的使用方式。除了这种方式进行服务调用以外还可以通过Feign进行调用,本篇文章就是简单介绍一下如何使用Feign进行服务调用。根据前一篇文章所用项目进行修改。

Feign使用流程

1.pom文件引入依赖

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

备注:根据feign版本的不同,名称可能不一样,可以到官方进行查看feign的maven。
2.入口添加@EnableFeignClients

package com.ckmike.order_service;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
@EnableFeignClients
public class OrderServiceApplication {

    public static void main(String[] args) {
        SpringApplication.run(OrderServiceApplication.class, args);
    }

    @Bean
    @LoadBalanced
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }
}

3.编写商品服务客户端接口

package com.ckmike.order_service.service;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * @ClassName ProductClient商品服务客户端
 * @Description TODO:描述该接口职责
 * @Author ckmike
 * @Date 18-11-22 下午4:10
 * @Version 1.0
 * @Copyright ckmike
 **/
@FeignClient(name="product-service")
public interface ProductClient {

    @GetMapping("/api/v1/product/find")
    String findById(@RequestParam(value = "id") int id);
}

备注:商品服务客户端接口的路由必须与商品服务对应路由保持一致。

订单服务接口实现

package com.ckmike.order_service.service.impl;

import com.ckmike.order_service.domain.ProductOrder;
import com.ckmike.order_service.service.OrderService;
import com.ckmike.order_service.service.ProductClient;
import com.ckmike.order_service.utils.JsonUtil;
import com.fasterxml.jackson.databind.JsonNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.Date;
import java.util.Map;
import java.util.UUID;

/**
 * OrderServiceImpl 简要描述
 * <p> TODO:描述该类职责 </p>
 *
 * @author ckmike
 * @version 1.0
 * @date 18-11-7 下午11:55
 * @copyright ckmike
 **/
@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private ProductClient productClient;

    @Override
    public ProductOrder saveForRibbon(int userId, int productId) {
        // 获取商品信息
        Map<String,Object> obj = restTemplate.getForObject("http://product-service/api/v1/product/find?id="+productId,Map.class);

        ProductOrder productOrder = new ProductOrder();
        productOrder.setCreateTime(new Date());
        productOrder.setUserId(userId);
        productOrder.setTradeNo(UUID.randomUUID().toString());

        productOrder.setPrice(Double.parseDouble(obj.get("price").toString()));

        productOrder.setProductName(obj.get("name").toString());
        return productOrder;
    }

    @Override
    public ProductOrder saveForFeign(int userId, int productId) {
        String response = this.productClient.findById(productId);
        JsonNode obj = JsonUtil.str2JsonNode(response);

        ProductOrder productOrder = new ProductOrder();
        productOrder.setCreateTime(new Date());
        productOrder.setUserId(userId);
        productOrder.setTradeNo(UUID.randomUUID().toString());

        productOrder.setPrice(Double.parseDouble(obj.get("price").toString()));

        productOrder.setProductName(obj.get("name").toString());
        return productOrder;
    }
}

截图

启动EurekaServer、ProductService、OrderService.
SpringCloud之服务调用(feign)

访问接口:
http://ckmikepc.lan:8781/api/v1/order/saveforfeign?user_id=1&product_id=1
SpringCloud之服务调用(feign)

总结一下:
Feign是通过Ribbon实现的,具体可以查看相应的代码。更多详细信息请查看Feign相应文档,会对你理解Feign的使用和实现有很大帮助。在学会如何使用feign后,建议阅读一下Feign的源码,看看别人是怎么写出这种东西的。

猜你喜欢

转载自blog.51cto.com/4837471/2320677