Service invocation of spring cloud (feign)

Keywords: Java Spring Maven

Preface

The previous article described how to use the RestTemplate of Ribbon for service calls. In addition to this way of service invocation, it can also be called through Feign. This article is a brief introduction to how to use Feign for service invocation. Make changes based on the items used in the previous article.

Feign use process

1.pom file import dependency

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

Note: according to the different versions of feign, the names may be different. You can go to the official to view feign's maven.
2. Add @ EnableFeignClients to the entry

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. Write commodity service client interface

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 Commodity service client
 * @Description TODO:Describe the responsibilities of the interface
 * @Author ckmike
 * @Date 18-11-22 4:10 p.m.
 * @Version 1.0
 * @Copyright ckmike
 **/
@FeignClient(name="product-service")
public interface ProductClient {

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

Note: the route of commodity service client interface must be consistent with that of commodity service.

Order service interface implementation

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 Brief description
 * <p> TODO:Describe such responsibilities</p>
 *
 * @author ckmike
 * @version 1.0
 * @date 18-11-7 11:55 p.m.
 * @copyright ckmike
 **/
@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private ProductClient productClient;

    @Override
    public ProductOrder saveForRibbon(int userId, int productId) {
        // Get product information
        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;
    }
}

screenshot

Start EurekaServer, ProductService and OrderService

Provider:
http://ckmikepc.lan:8781/api/v1/order/saveforfeign?user_id=1&product_id=1

To summarize:
Feign is implemented through the Ribbon. You can check the corresponding code. For more details, please check the corresponding documentation of feign, which will help you understand the use and implementation of feign. After learning how to use feign, it is recommended to read the source code of feign to see how others write such things.

Posted by pessi on Fri, 06 Dec 2019 18:41:39 -0800