OpenFeign远程调用实现

1.导入依赖

            org.springframework.cloud
            spring-cloud-starter-openfeign

2.建立远程调用类
@FeignClient("userservice")
@Component
public interface UserClient {
    @GetMapping("/user/{id}")
    public User queryById(@PathVariable("id") Long id);

}

3.启动类注解
@EnableFeignClients
4.远程调用
@Service
public class OrderService {

  @Autowired private OrderMapper orderMapper;
  /*@Autowired private RestTemplate restTemplate;*/
  @Autowired private UserClient userClient;

  public Order queryOrderById(Long orderId) {
    // 1.查询订单
    Order order = orderMapper.findById(orderId);
    // 2.利用RestTemplate发送请求
    /*String url = "http://userservice/user/" + order.getUserId();*/
    //使用Fiegn发送远程请求
    //3.远程调用
   /* User user = restTemplate.getForObject(url, User.class);*/
    User user = userClient.queryById(order.getUserId());
    order.setUser(user);
    return order;
  }
}
===============================
远程controller层
@Slf4j
@RestController
@RequestMapping("/user")
@RefreshScope/*热更新*/
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 路径: /user/110
     *
     * @param id 用户id
     * @return 用户
     */
    @GetMapping("/{id}")
    public User queryById(@PathVariable("id") Long id) {
        return userService.queryById(id);
    }
}

==================================
连接池优化orderservice
 
            io.github.openfeign
            feign-httpclient
       

feign:
  httpclient:
    max-connections: 200
    max-connections-per-route: 50
    enabled: true

你可能感兴趣的:(springCloud,java,开发语言)