声明式REST客户端:Feign

创建maven工程 加入feign 依赖

 <!-- 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注解开启Feign功能 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);
    }
}

定义服务接口类

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为注册中心的注册项目名

service注入fegin 接口

@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());

其实通过Feign封装了HTTP调用服务方法,使得客户端像调用本地方法那样直接调用方法

猜你喜欢

转载自blog.csdn.net/qq_38893133/article/details/103812958