单体架构
将业务的所有功能都集中在一个项目中进行开发,打成一个包部署.
根据业务功能对系统进行拆分,每个业务模块作为独立项目开发,称为一个服务
服务治理
分布式架构要考虑的问题:
微服务
微服务是一种经过良好架构设计的分布式架构方案,微服务架构特征:
优点:拆分粒度更小、服务更独立、耦合度更低
缺点:架构非常复杂,运维、监控、部署难度提高
微服务结构
微服务需要技术框架来实现,比较出名的是SpringCloud和alibaba的Dubbo
微服务技术对比

企业需求:

SpringCloud集成了各种微服务功能组件,并基于SpringBoot实现了这些组件的自动装配,从而提供了了良好的开箱即用:

服务拆分注意事项:
项目cloud-demo1
git地址 https://gitee.com/xn2001/cloudcode/tree/master/01-cloud-demo

根据订单id查询订单功能
需求:根据订单id查询订单的同时,把订单所属的用户信息一起返回
远程调用方式分析:

在order-service的OrderApplication中添加RestTemplate配置类
/**
* 创建RestTemplate并注入spring容器
* @return
*/
@Bean
public RestTemplate restTemplate(){
return new RestTemplate();
}
package com.xn2001.order.service;
import com.xn2001.order.mapper.OrderMapper;
import com.xn2001.order.pojo.Order;
import com.xn2001.order.pojo.User;
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;
public Order queryOrderById(Long orderId) {
// 1.查询订单
Order order = orderMapper.findById(orderId);
//2 利用RestTemplate发起Http请求,查询用户
//2.1url路径
String url = "http://localhost:8081/user/"+order.getUserId();
//2.2发送http请求,实现远程调用,getForObject get请求
User user = restTemplate.getForObject(url, User.class);
//3.封装user到Order
order.setUser(user);
// 4.返回
return order;
}
}
打开浏览器测试:localhost:8080/order/101
测试结果:

总结:
想要远程调用,实际上就是一个基于http的请求