声明式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调用服务方法,使得客户端像调用本地方法那样直接调用方法

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值