openFeignサービス間の呼び出し

1.定義:

Fegin:リボンとHystrixに基づく宣言型サービス呼び出しコンポーネント。Feign| OpenFeignはHystrixを統合し、融合およびダウングレードする機能を備えています。Feign| OpenFeignはリボンを統合し、負荷分散機能を備えています。

2.分類:

3.openFeignは以下を使用します:

依存関係をインポートする

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
            <version>2.2.3.RELEASE</version>
        </dependency>

スタートアップクラスは@EnableFeignClientsを追加する必要があります

@EnableFeignClientsアノテーションは、アノテーション@FeignClientで定義されたすべての偽のクライアントをスキャンし、偽のクライアントをIOCコンテナに登録するようにフレームワークに指示します。

インターフェイスを作成します。

// 参数1 value = 模块名  
// 参数2 详解在下面 
@FeignClient(value = "gp6-lms",contextId = "TrainplanUser")
public interface TrainplanUserClient {
    // url含义如下图:
    @GetMapping("/gp6/lms/serviceApi/trainplanUser/getByUserIdPlanId")
    TrainplanUserDto getByUserIdPlanId(@RequestParam("userId") String userId,
                                       @RequestParam("planId") String planId,
                                       @RequestParam("platformId") String platformId);
    // 需要被调用的方法
    @PostMapping("/gp6/lms/serviceApi/trainplanUser/addActivateTrainplanForOrderNo")
    String addActivateTrainplanForOrderNo(@RequestParam("platformId") String platformId,
                                          @RequestBody TrainplanDto trainplanDto,
                                          @RequestParam("userId") String userId,
                                          @RequestParam("userName") String userName,
                                          @RequestParam("resourceId") String resourceId);
}

infoToDto() :
       PharmacistArticleDto res = new PharmacistArticleDto();
        BeanUtils.copyProperties(data, res);

contextIdの役割

モジュールには多くのメソッドが呼び出されます。それらすべてを1つのクラスに入れたくはありません。複数のクラスを作成できます。

ただし、今回はBeanの名前が競合するため、エラーが報告されます。

The bean 'gp6-lms.FeignClientSpecification' could not be registered. A bean with that name has already been defined and overriding is disabled.

この問題は、別のcontextIdを指定することで回避できます。contextIdは、操作のクラス名で定義できます。

DTOを作成する

共通モジュールに配置できるTrainplanUserDto
@SuppressWarnings("serial")
public class TrainplanUserDto implements Serializable
{

	private String id;
	private String trainplanId;
    public TrainplanUserDto(){}
   
    
    /**
     * 培训计划所选学员信息编号(f_planuser_id)
     */ 	
	public String getId(){
        return id;
    }
    
     /**
     * 培训计划所选学员信息编号(f_planuser_id)
     */ 	
    public void setId(String id){
        this.id=id;
    }
    
    /**
     * 培训计划编号(f_plan_id)
     */ 	
	public String getTrainplanId(){
        return trainplanId;
    }
    
     /**
     * 培训计划编号(f_plan_id)
     */ 	
    public void setTrainplanId(String trainplanId){
        this.trainplanId=trainplanId;
    }
    
}

サービス間通話:

発信者:特別モジュール、着信者:lmsモジュール

trainplanUserClient.getByUserIdPlanId(studentVO.getApplyId(),studentVO.getTrainplanId(),platformId);

おすすめ

転載: blog.csdn.net/Ciel_Y/article/details/122586519