5.Feign
阿里云国内75折 回扣 微信号:monov8 |
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6 |
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