5.Feign

文章介绍了如何在SpringCloud中整合并使用Feign进行服务间的调用,包括创建Feign接口、实现、引入依赖,以及在DAO、Service和Controller层的应用。此外,还讨论了Feign接口的继承以复用代码,以及通过更换HTTPClient和启用数据压缩来优化HTTP性能。

1.简介
Feign是SpringCloud集成的HTTP客户端,可以更加便捷、优雅地调用HTTP API。

2.整合Feign(在影厅服务中调用电影服务查询电影信息,在hall服务工程中修改)
(1).引入依赖

<dependency>
	<groupId>io.github.openfeign</groupId>
	<artifactId>feign-httpclient</artifactId>
</dependency>

(2).feign开发
首先在项目目录“/src/main/java/com/steven/movies/hall”下新建“/feign”目录,并在feign目录下新建FilmFeignAPI和FilmFeignAPIImpl类,具体代码如下。

@FeignClient(name = "film.service", path = "/films")
public interface FilmFeignAPI {
    //根据影厅id获取电影信息
    @RequestMapping(value = "/findFilmListByHallId/{hallId}", method = RequestMethod.GET)
    ServerResponse findFilmListByHallId(@PathVariable("hallId") Integer hallId);
}
@Service
public class FilmFeignAPIImpl implements FilmFeignAPI {

    @Override
    public ServerResponse findFilmListByHallId(Integer hallId) {
        return null;
    }
}

(3).dao层开发
在entity目录下新增Film实体类,具体代码如下。

@Data
@NoArgsConstructor
@AllArgsConstructor
@ToString
public class Film extends Model<Film> {

    private static final long serialVersionUID = 1L;

    private Integer id;

    private Integer hallId;

    private String filmName;

    private BigDecimal price;

    private Date showTime;

    private Date createTime;

    private Date updateTime;
}

(4).service层开发
在IHallService接口中新增获取所有影厅放映的电影信息方法,具体代码如下。

public interface IHallService extends IService<Hall> {
    //获取所有影厅放映的电影信息
    List<Film> findFilmListByHallIds();
}

在HallServiceImpl实现类中新增获取所有影厅放映的电影信息方法,具体代码如下。

@Service
public class HallServiceImpl extends ServiceImpl<HallMapper, Hall> implements IHallService {
    @Resource
    private FilmFeignAPI filmFeignAPI;

    @Override
    public List<Film> findFilmListByHallIds() {
        List<Film> filmList = new ArrayList<>();
        //获取所有影厅id
        Integer[] hallIds = {1, 2, 3, 4, 5, 6};

        //根据影厅id获取电影信息
        for (int i = 0; i < hallIds.length; i++) {
            //调用电影服务获取电影信息
            ServerResponse<List<Film>> serverResponse = filmFeignAPI.findFilmListByHallId(hallIds[i]);
            if (Objects.isNull(serverResponse) || !serverResponse.isSuccess() || Objects.isNull(serverResponse.getData())) {
                continue;
            }

            List<Film> tmpFilmList = serverResponse.getData();
            if (!CollectionUtils.isEmpty(tmpFilmList)) {
                filmList.addAll(tmpFilmList);
            }
        }
        return filmList;
    }
}

(5).controller层开发
在HallController类中新增获取所有影厅放映的电影信息方法,具体代码如下。

@Controller
@RequestMapping("/halls")
public class HallController {
    //获取所有影厅放映的电影信息
    @RequestMapping(value = "/findFilmListByHallIds", method = RequestMethod.GET)
    public ServerResponse findFilmListByHallIds() throws CommonServiceException {
        List<Film> filmList = hallService.findFilmListByHallIds();
        if (CollectionUtils.isEmpty(filmList)) {
            return ServerResponse.createBySuccess("暂无电影信息");
        } else {
            return ServerResponse.createBySuccess(filmList);
        }
    }
}

(6).启动项目
在启动类MoviesHallApplication上新增注解@EnableFeignClients,然后启动项目。

@MapperScan(basePackages = {"com.steven.movies.hall.dao"})
@EnableFeignClients
@EnableDiscoveryClient
@SpringBootApplication
public class MoviesHallApplication {

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

}

(7).测试
启动项目,然后在postman中请求“localhost:8401/halls/findFilmListByHallIds”,可以查询到相应的信息,测试结果如下图所示。
在这里插入图片描述

(8).hall服务工程目录结构
在这里插入图片描述

4.继承
(1).简介
Feign支持继承,继承可以将一些公共操作封装到父接口中,从而简化开发。

(2).引入依赖

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

(3).公共接口开发

public interface BaseFeignAPI {

}
@Service
public class BaseFeignAPIImpl implements BaseFeignAPI {

}

(4).继承开发

@FeignClient(name = "film.service", path = "/films")
public interface FilmFeignAPI extends BaseFeignAPI {
    //根据影厅id获取电影信息
    @RequestMapping(value = "/findFilmListByHallId/{hallId}", method = RequestMethod.GET)
    ServerResponse findFilmListByHallId(@PathVariable("hallId") Integer hallId);
}
@Service
public class FilmFeignAPIImpl implements FilmFeignAPI {

    @Override
    public ServerResponse findFilmListByHallId(Integer hallId) {
        return null;
    }
}

5.HTTP性能优化
(1).HTTPClient替换
Feign最大的优化点是将HTTPClient由JDK自带的版本替换为Apache HTTPClient版本

<dependency>
	<groupId>io.github.openfeign</groupId>
	<artifactId>feign-httpclient</artifactId>
</dependency>
feign:
  httpclient:
    enabled: true

(2).数据压缩

feign:
  compression:
    request:
      enabled: true
      mime-types: text/xml,application/xml,application/json
      min-request-size: 2048
    response:
      enabled: true
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值