call between openFeign services

1. Definition:

Fegin: A declarative service invocation component based on Ribbon and Hystrix. Feign|OpenFeign integrates Hystrix and has the ability to fuse and downgrade. Feign|OpenFeign integrates Ribbon and has load balancing capabilities

2. Classification:

3.openFeign uses:

import dependencies

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

The startup class needs to add @EnableFeignClients

The @EnableFeignClients annotation tells the framework to scan all feign clients defined with the annotation @FeignClient and register the feign clients with the IOC container.

Create an interface:

// 参数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);

The role of contextId

There are a lot of methods called in a module, we don't want to put them all in one class, we can create multiple classes.

But this time it will report an error because the name of the bean conflicts.

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

This problem can be avoided by specifying a different contextId. The contextId can be defined by the class name of the operation.

Create Dto

TrainplanUserDto, which can be placed in the common module
@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;
    }
    
}

Inter-service call:

caller: special module, callee: lms module

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

Guess you like

Origin blog.csdn.net/Ciel_Y/article/details/122586519