Feign远程调用
注意看这里--->举例:假设当前order-service服务需要远程调用user服务
1、引入依赖
在order-service服务的pom文件中引入feign的依赖:
org.springframework.cloud
spring-cloud-starter-openfeign
2、添加注解
在order-service的启动类添加注解开启Feign的功能:
3、编写Feign的客户端
在order-service中新建一个接口,内容如下:
package cn.itcast.order.client;
@FeignClient("userservice")
public interface UserClient {
@GetMapping("/user/{id}")
User findById(@PathVariable("id") Long id);
}
4、测试
修改order-service中的OrderService类中的queryOrderById方法,使用Feign客户端代替RestTemplate:
package cn.itcast.order.service;
import cn.itcast.feign.client.UserClient;
import cn.itcast.feign.pojo.User;
import cn.itcast.order.mapper.OrderMapper;
import cn.itcast.order.pojo.Order;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
@Service
public class OrderService {
@Autowired
private OrderMapper orderMapper;
@Autowired
private RestTemplate restTemplate;
@Autowired
private UserClient userClient;
public Order queryOrderById(Long orderId) {
// 订单查询
Order order = orderMapper.findById(orderId);
// 1.配置url
// String url = "http://localhost:8081/user/" + order.getUserId();
// String url = "http://userservice:8081/user/" + order.getUserId();
// 2.发起调用
// User user = restTemplate.getForObject(url, User.class);
// 使用Feign发起http请求,查询用户
User user = userClient.findById(order.getUserId());
// 封装user到order
order.setUser(user);
// 返回
return order;
}
}
日志文件配置
针对单个服务
feign:
client:
config:
userservice: # 针对某个微服务的配置
loggerLevel: FULL # 日志级别
针对所有服务
feign:
client:
config:
default: # 这里用default就是全局配置,如果是写服务名称,则是针对某个微服务的配置
loggerLevel: FULL # 日志级别
日志的级别分为四种:
Feign最佳实践方式
注意看这里--->举例:假设当前order-service服务需要远程调用user服务
这种是将Feign单独抽取到一个模块中
1、抽取
首先创建一个module,命名为feign-api
2、引入依赖
org.springframework.cloud
spring-cloud-starter-openfeign
org.springframework.cloud
spring-cloud-openfeign-core
x
3、写入UserClient、User、DefaultFeignConfiguration
创建cn.itcast.feign包
UserClient
package cn.itcast.feign.client;
@FeignClient("userservice")
public interface UserClient {
@GetMapping("/user/{id}")
User findById(@PathVariable("id") Long id);
}
User
package cn.itcast.feign.pojo;
import lombok.Data;
@Data
public class User {
private Long id;
private String username;
private String address;
}
DefaultFeignConfiguration
package cn.itcast.feign.config;
@Configuration
public class DefaultFeignConfiguration {
@Bean
public Logger.Level feignLogLevel(){
return Logger.Level.BASIC; // 日志级别为BASIC
}
}
4、在order-service的pom文件中引入feign-api依赖
com.itcast
feign-api
1.0
5、添加扫描包
这个要加载启动类上,谁用到这个feign-api,就在谁的启动类上加
方式一:
指定Feign应该扫描的包:
@EnableFeignClients(basePackages = "cn.itcast.feign.clients")
方式二:
指定需要加载的Client接口(注意引包是:feign-api下的别引错了):
@EnableFeignClients(clients = {UserClient.class})
6、order-server使用Open Feign调用User
package cn.itcast.order.service;
import cn.itcast.feign.client.UserClient;
import cn.itcast.feign.pojo.User;
import cn.itcast.order.mapper.OrderMapper;
import cn.itcast.order.pojo.Order;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
@Service
public class OrderService {
@Autowired
private OrderMapper orderMapper;
@Autowired
private UserClient userClient;
public Order queryOrderById(Long orderId){
// 订单查询
Order order = orderMapper.findById(orderId);
// 利用Feign发起http请求,查询用户
User user = userClient.findById(order.getUserId());
// 封装user到order
order.setUser(user);
// 返回
return order;
}
}