[back end] call between Spring Cloud services

Preface

Recently, the editor needs to call an interface of the external service, so the api layer of the external service is needed to open the port. In this service, the returned value of the result encapsulation of IntegralResult is processed through Json transformation.

Two service code implementations

I. external service open port

1. Code of API layer

package com.dmsdbj.integral.kernel.api.facade;  // Packet path

import com.dmsdbj.cloud.tool.business.IntegralResult;
import org.springframework.cloud.openfeign.FeignClient; // Notes for registering service discovery
....


@FeignClient(value = "INTEGRAL-KERNEL-PROVIDER", path = "/kernel-web",fallback = IntegralClientException.class)
public interface IntegralDetailClientService {

...

 /**
     * Query bonus record of full attendance reward
     * @author Feng Hao Yue
     * @param userId
     * @param date
     * @return
     * @since  2019 March 12, 2016 16:12:56
     */
    @GetMapping(value = {"/api/integralDetail/findByUserIdDate/{userId}/{date}"})
    IntegralResult findByUserIdDate(@PathVariable(value="userId") String userId,
                                                @PathVariable(value="date") String date);

}

Provider layer implements the interface of api layer

package com.dmsdbj.integral.kernel.provider.controller.api;/**


@Controller
@Api(tags = {"External integration details interface "})
@RestController
@Slf4j
@RequestMapping(value = {"/api/integralDetail"})

public class ApiIntegralDetailController implements IntegralDetailClientService {

...

   *
     * @param userId
     * @param date
     * @return
     * @author Feng Hao Yue
     * @since 2019 March 12, 2016 16:12:56
     */
    @ApiOperation(value = "Query bonus record of full attendance reward")
    @GetMapping(value = {"/findByUserIdDate/{userId}/{date}"})
    @Override
    public IntegralResult<List<IntegralDetailModel>> findByUserIdDate(@PathVariable(value = "userId") String userId, @PathVariable(value = "date") String date) {

        if (integralDetailService.findByUserIdDate(userId, date).size() == 0) {
            return IntegralResult.build(IntegralResult.SUCCESS, "no data", null);
        } else {
            return IntegralResult.build(IntegralResult.SUCCESS, "query was successful", integralDetailService.findByUserIdDate(userId, date));
        }
    }

....
}

 

Two. Call in this service

// Out of reference service api
import ...api.facade.IntegralDetailClientService;
import ...api.model.IntegralDetailModel; 

public class DetailRecordServiceImpl extends.. implements ..{

...

// Injection service
@Resource
private IntegralDetailClientService integralDetailClientService;

// Method
public void plusIntegral() {
...

String userJiFenId = detailRecordEntity.getUserJiFenId();
 
// Call the findByUserIdDate method of the external service, and the return value is the encapsulated result body, for example:
IntegralResult{code:"0000",message:"query was successful",data:null}

 IntegralResult<List<IntegralDetailModel>> findResult = 
 integralDetailClientService.findByUserIdDate(userJiFenId, today);

 if (IntegralResult.SUCCESS.equals(findResult.getCode())) {

  // Convert the returned data to String type, using JsonUtil.beanToJson();
    String integralDetailModelList = JsonUtil.beanToJson(findResult.getData());


   if (integralDetailModelList == null) { // The user didn't give a score
    List<Integer> fullTimeList = 
    this.queryFullTimeByUserId(detailRecordEntity.getUserJiFenId()); // According to the user, query whether the day is full-time
     fullTimeDays = fullTimeList.get(1);
   }    

 } else {
  log.error(IntegralResult.FAIL, "External service
integralDetailClientService.findByUserIdDate(userJiFenId, today)Call failed--{}", userJiFenId);
 }
...
}

 

Summary

The implementation idea of the call between services is basically the embodiment of the inheritance and implementation relationship between classes and interfaces, and the use of necessary annotations. At the same time, the maven package needs to be updated and the service needs to be built. These two details cannot be ignored, otherwise, the code problem will be found in vain. At the same time, the use of IntegralResult package, the receiving and processing of return value.

Thank you for your visit!

Tags: JSON Maven

Posted on Sat, 30 Nov 2019 08:53:06 -0800 by vinodkalpaka