SpringCloud入门_认识微服务

1 什么是微服务

image

2 什么是微服务总结

image

3 微服务远程调用

1)注册RestTemplate

在order-service的OrderApplication中注册RestTemplate

@MapperScan("cn.itcast.order.mapper")  
@SpringBootApplication  
public class OrderApplication {  
  
    public static void main(String[] args) {  
        SpringApplication.run(OrderApplication.class, args);  
    }  
  
    @Bean  
    public RestTemplate restTemplate(){  
        return new RestTemplate();  
    }  
}

2)服务远程调用RestTemplate

修改order-service中的OrderService的queryOrderById方法:

@Service  
public class OrderService {  
  
    @Autowired  
    private OrderMapper orderMapper;  
  
    @Autowired  
    private RestTemplate restTemplate;  
  
    public Order queryOrderById(Long orderId) {  
        // 1.查询订单  
        Order order = orderMapper.findById(orderId);  
        // 2.生成url  
        String url = "http://localhost:8081/user/"+order.getUserId();  
        // 3.发请求到用户模块查询用户信息  
        User user = restTemplate.getForObject(url, User.class);  
        // 4.将查询到的用户数据添加到订单数据中  
        order.setUser(user);  
        // 5.返回  
        return order;  
    }  
}

3)微服务远程调用总结

微服务调用方式

》基于RestTemplate发起的http请求实现远程调用
》http请求做远程调用是与语言无关的调用,只要知道对方的ip、端口、接口路径、请求参数即可。

相关文章

本篇文章为大家展示了如何解决Spring Cloud 服务冲突问题,内...
本篇内容主要讲解“spring cloud服务的注册与发现怎么实现”...
本篇内容介绍了“Dubbo怎么实现Spring Cloud服务治理 ”的有...
本篇内容主要讲解“SpringCloud相关面试题有哪些”,感兴趣的...
如何分析Spring Cloud Ribbon、Spring Cloud Feign以及断路器...
这篇文章主要讲解了“springcloud微服务的组成部分有哪些”,...