Declarative REST client: Feign

Create a maven project and add feign dependencies

 <!-- feign -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
        <!--bas-cloud所需依赖-->
        <dependency>
            <groupId>com.ekingwin</groupId>
            <artifactId>bas-cloud-cache-redis</artifactId>
            <version>${
    
    bas.version}</version>
        </dependency>
        <dependency>
            <groupId>com.ekingwin</groupId>
            <artifactId>bas-cloud-portal</artifactId>
            <version>${
    
    bas.version}</version>
        </dependency>
        <dependency>
            <groupId>com.ekingwin</groupId>
            <artifactId>bas-cloud-audit</artifactId>
            <version>${
    
    bas.version}</version>
        </dependency>

        <dependency>
            <groupId>io.github.swagger2markup</groupId>
            <artifactId>swagger2markup</artifactId>
            <version>1.3.1</version>
        </dependency>
        <dependency>
            <groupId>io.vavr</groupId>
            <artifactId>vavr</artifactId>
            <version>0.9.2</version>
        </dependency>
        <dependency>
            <groupId>ch.netzwerg</groupId>
            <artifactId>paleo-core</artifactId>
            <version>0.10.2</version>
        </dependency>
        <dependency>
            <groupId>javax.validation</groupId>
            <artifactId>validation-api</artifactId>
            <version>2.0.1.Final</version>
        </dependency>
        <dependency>
            <groupId>com.ekingwin</groupId>
            <artifactId>bas-cloud-infrastructure-repository</artifactId>
            <version>${
    
    bas.version}</version>
        </dependency>
        <!-- api 导入api接口包>
        <dependency>
            <groupId>com.ekingwin</groupId>
            <artifactId>jc-mall-goods-interface</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>com.ekingwin</groupId>
            <artifactId>jc-mall-system-interface</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        

@EnableFeignClients annotation enables Feign function OrderApplication.java

@RestController
@SpringBootApplication
@ComponentScan({
    
    "com.lcw.bas.cloud","com.lcw.jc.mall"})
@MapperScan({
    
    "com.lcw.bas.cloud.**.dao","com.lcw.jc.mall.**.dao"})
@EnableTransactionManagement
@EnableCaching
@EnableFeignClients
@ServletComponentScan(basePackages = "com.lcw.bas.cloud")
@EnableDiscoveryClient

public class OrderApplication extends SpringBootServletInitializer implements WebApplicationInitializer {
    
    
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
    
    
        return application.sources(OrderApplication.class);
    }

    public static void main(String[] args) {
    
    
        SpringApplication.run(OrderApplication.class, args);
    }
}

Define service interface class

import com.ekingwin.jc.mall.system.api.UserAddressApi;
import org.springframework.cloud.openfeign.FeignClient;

/**
 * Created by craywen on 2019/12/28 16:35
 *
 * @description
 */

@FeignClient("jc-mall-system")
public interface UserAddressClient extends UserAddressApi {
    
    
}

jc-mall-system is the registered project name of the registry

service injects the Fegin interface

@Service
public class DemandServiceImpl extends BaseService implements IDemandService {
    
    

    @Resource
    IDemandDao demandDao;

    @Resource
    IMaterialDao iMaterialDao;
	//fegin api
    @Autowired
    UserAddressApi userAddressApi;

    @Autowired
    UserInfoApi userInfoApi;

 BaseResult<EmployeeAddressDto> addressById = 	userAddressApi.getAddressById(demand.getReceiver_id());

In fact, the HTTP call service method is encapsulated through Feign, so that the client directly calls the method like calling a local method

Guess you like

Origin blog.csdn.net/qq_38893133/article/details/103812958