Spring Cloud Service Call (Ribbon)

Posted by zgkhoo on Sat, 11 May 2019 12:22:34 +0200

Business scenario

Order service calls commodity service cluster, develops pseudo-order function, and uses Ribbon to implement order call commodity service.

Train of thought:
1. Create order service
2. Write pseudo-single interface
a. Calling Commodity services to obtain commodity information (Ribbon calls services)
b. According to the commodity information, the order interface returns the order details information.

The call logic diagram is as follows:

Implementing Order Service Items

The order service project builds the project through Spring Initializr, choosing Web and Eureka Discovery. It's the same Product-Service as the previous blog post. We also get the Product-Service instance port and put it into the product information to verify Ribbon's client load balancing.

order-service entry:

Analysis:
The call to take is implemented through RestTemplate, which is implemented through Ribbon. @ LoadBalanced is a client load balancing annotation.

Service invocation:

package com.ckmike.order_service.service.impl;

import com.ckmike.order_service.domain.ProductOrder;
import com.ckmike.order_service.service.OrderService;
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;

    @Override
    public ProductOrder save(int userId, int productId) {
        // Access to commodity 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;
    }
}

We started eureka Server, Product-Service (3 examples) and Order-Service projects, the eureka console, as follows:

By calling save interface of order-service:


Analysis:
With the invocation of the order interface, we can see that the interface of the commodity service instance of ProductName is different every time. This is the embodiment of the client load balancing strategy.

Conclusion:
By default, RestTemplate client debt balancing uses round robin strategy for client service invocation. We can modify the load balancing strategy by configuring it. As follows:

This strategy adopts random call strategy. More strategies can refer to the source code under com. netflix. loadbalance or look at the document query.

Topics: Java Spring