微服务调用:通过@FeignClient注解调用外部微服务接口

 

通过@FeignClient注解可以调用外部微服务接口

总共分为5步:

 

第一步:加载Jar包

第二步:新建一个Feign接口类( ClearBatchFeign.java),并且注明接口异常类,定义接口返回异常后的处理

第三步:创建一个Feign接口接口异常类(ClearBatchFeignFallback.java),实现Feign接口,

需要加上注解@Component自动注入

第四步,定义Bean类来接受数据(接收的数据要和调用的接口一致)

第五步:调用接口,实现微服务调用

 

 

第一步:加载Jar包



   org.springframework.cloud
   spring-cloud-starter-feign

 

第二步:新建一个Feign接口类( ClearBatchFeign.java),

并且注明接口异常类fallback = ClearBatchFeignFallback.class(第二步创建)

import io.hcbm.common.constant.HcbmService;

import io.hcbm.fulfill.api.dto.ContractDTO;

import io.hcbm.fulfill.api.dto.ContractLineDTO;

import io.hcbm.fulfill.infra.feign.fallback.ClearBatchFeignFallback;

import org.springframework.cloud.netflix.feign.FeignClient;

import org.springframework.http.MediaType;

import org.springframework.http.ResponseEntity;

import org.springframework.web.bind.annotation.GetMapping;

import org.springframework.web.bind.annotation.PathVariable;

 

import java.util.List;

 

@FeignClient(value = HcbmService.Contract.NAME, fallback = ClearBatchFeignFallback.class)

public interface ClearBatchFeign {

    @GetMapping(value = "/v1/{organizationId}/contracts/{contractId}", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)

    ResponseEntity getContractByContractId(@PathVariable("contractId") Long contractId,

                                                  @PathVariable("organizationId") Long tenantId);

 

    @GetMapping(value = "/v1/{organizationId}/contracts/{contractId}/lines", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)

     ResponseEntity> getContractLineByContractId(@PathVariable("contractId") Long contractId,

                                                                   @PathVariable("organizationId") Long tenantId);

}

 

 

HcbmService.JAVA文件定义的是微服务的基本信息:服务名,端口号和Code

 

 

第三步:创建一个Feign接口异常实现类(ClearBatchFeignFallback.java),实现Feign接口( ClearBatchFeign),

需要加上注解@Component自动注入

 

import io.hcbm.fulfill.api.dto.ContractDTO;

import io.hcbm.fulfill.api.dto.ContractLineDTO;

import io.hcbm.fulfill.infra.feign.ClearBatchFeign;

import org.springframework.http.HttpStatus;

import org.springframework.http.ResponseEntity;

import org.springframework.stereotype.Component;

 

import java.util.List;

 

@Component

public class ClearBatchFeignFallback implements ClearBatchFeign {

 

    @Override

    public ResponseEntity getContractByContractId(Long contractId, Long tenantId) {

        return new ResponseEntity<>(HttpStatus.BAD_REQUEST);

    }

 

    @Override

    public ResponseEntity> getContractLineByContractId(Long contractId, Long tenantId) {

        return new ResponseEntity<>(HttpStatus.BAD_REQUEST);

    }

}

 

第四步,定义Bean类来接受数据(接收的数据要和调用的接口一致)

 

import com.alibaba.fastjson.annotation.JSONField;

import com.fasterxml.jackson.annotation.JsonFormat;

import lombok.*;

import org.hzero.editpmsn.EditObject;

import org.hzero.editpmsn.constant.EditObjectCode;

import org.springframework.format.annotation.DateTimeFormat;

 

import java.math.BigDecimal;

import java.time.LocalDate;

import java.util.Date;

 

@Getter

@Setter

@Builder

@AllArgsConstructor

@NoArgsConstructor

@ToString

@EditObject(referenceObject = EditObjectCode.CONTRACT)

public class ContractDTO {

    private Long id;

    private String contractNumber;

    private String contractName;

    private Long companyId;

    private String companyName;

    private Long categoryId;

    private String categoryName;

    private String inOutTypeCode;

    private String propertyCode;

    private String departmentName;

    private Long departmentId;

    private String belongingDepartmentName;

    private Long belongingDepartmentId;

    @DateTimeFormat(pattern = "yyyy-MM-dd")

    @JsonFormat(pattern = "yyyy-MM-dd")

    @JSONField(format = "yyyy-MM-dd")

    private LocalDate signatureDate;

    @DateTimeFormat(pattern = "yyyy-MM-dd")

    @JsonFormat(pattern = "yyyy-MM-dd")

    @JSONField(format = "yyyy-MM-dd")

    private LocalDate startDate;

    @DateTimeFormat(pattern = "yyyy-MM-dd")

    @JsonFormat(pattern = "yyyy-MM-dd")

    @JSONField(format = "yyyy-MM-dd")

    private LocalDate endDate;

    private BigDecimal amount;

    private String paymentModeCode;

    private String signatureTypeCode;

    private String currency;

    private String principalName;

    private Long principalId;

    private String statusCode;

    private String executeStatusCode;

    private String lastStatusCode;

    private String description;

    private String creationMethodCode;

    private Long sourceContractId;

    private String sourceContractNumber;

    private Long sourceTemplateId;

    private String sourceTemplateNumber;

    private Long contentId;

    private Long lineFieldId;

    private String creator;

    @DateTimeFormat(pattern = "yyyy-MM-dd")

    @JsonFormat(pattern = "yyyy-MM-dd")

    @JSONField(format = "yyyy-MM-dd")

    private Date creationDate;

    private Long version;

    private String editMode;

    private Long tenantId;

    private Long objectVersionNumber;

 

    private Long createdBy;

    // 权限校验用

    private Boolean canEdit;

    /**

     * 动态字段编码

     */

    private String ruleNumber;

 

}

第五步定义接口,引用方法得到数据

 

 

你可能感兴趣的:(微服务调用:通过@FeignClient注解调用外部微服务接口)