微服务服务拆分原则 与 RestTemplate远程调用

后端 创建于:2023-05-28

(目录)


服务拆分和远程调用

任何分布式架构都离不开服务的拆分,微服务也是一样。


服务拆分原则

这里总结了微服务拆分时的几个原则:

  • 不同微服务,不要重复开发相同业务
  • 微服务数据独立,不要访问其它微服务的数据库
  • 微服务可以将自己的业务暴露为接口,供其它微服务调用

image-20210713210800950


服务拆分示例

服务拆分注意事项:

  • 单一职责:不同微服务,不要重复开发相同业务
  • 数据独立:不要访问其它微服务的数据库
  • 面向服务:将自己的业务暴露为接口,供其它微服务调用

image-20210713211009593

cloud-demo:父工程,管理依赖

  • order-service:订单微服务,负责订单相关业务
  • user-service:用户微服务,负责用户相关业务

要求:

  • 订单微服务 和 用户微服务都必须有各自的数据库,相互独立
  • 订单服务 和 用户服务都对外暴露Restful的接口
  • 订单服务如果需要查询用户信息,只能调用用户服务的Restful接口,不能查询用户数据库

实现远程调用案例

在order-service服务中,有一个根据id查询订单的接口:

image-20210713212749575

微服务项目下,打开 idea 中的 Service,可以很方便的启动。

image-20220713203335591

根据id查询订单,返回值是Order对象,如图:

启动完成后,访问 http://localhost:8080/order/101

image-20220713203315103

user 为 null

正如上面的服务拆分要求中所提到

订单服务如果需要查询用户信息,只能调用用户服务的 Restful 接口,不能查询用户数据库


在user-service中有一个根据id查询用户的接口:

image-20210713213146089

查询的结果如图:

image-20210713213213075


1.需求:

修改order-service中的根据id查询订单业务,要求在查询订单的同时,根据订单中包含的userId查询出用户信息,一起返回

image-20210713213312278

因此,我们需要在order-service中 向user-service发起一个http的请求

调用 http://localhost:8081/user/{userId} 这个接口。


大概的步骤是这样的:

  • 注册一个RestTemplate实例到Spring容器
  • 修改order-service服务中的OrderService类中的queryOrderById方法,根据Order对象中的userId查询User
  • 将查询的User填充到Order对象,一起返回

2.注册RestTemplate

首先,我们在order-service服务中的OrderApplication启动类中,注册RestTemplate实例:

Spring 提供了一个 RestTemplate 工具,只需要把它创建出来即可。(即注入 Bean)

image-20220713203953130

package cn.itcast.order;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.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();
    }
}

3.实现远程调用

修改order-service服务中的cn.itcast.order.service包下的OrderService类中的queryOrderById方法

发送请求,自动序列化为 Java 对象。

@Service
public class OrderService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private RestTemplate restTemplate;

    public Order queryOrderById(Long orderId) {
        // 1.查询订单
        Order order = orderMapper.findById(orderId);
        // 2.远程查询 user
        String url = "http://localhost:8081/user/"+order.getUserId();
        // 使用 restTemplate 发送 http 请求
        User user = restTemplate.getForObject(url,User.class);
        //3. 封装 user 信息
        order.setUser(user);
        // 4.返回
        return order;
    }
}

image-20221107175519960

启动完成后,访问:http://localhost:8080/order/101

image-20220713204914078

在上面代码的 url 中,我们可以发现调用服务的地址采用硬编码,这在后续的开发中肯定是不理想的,这就需要服务注册中心(Eureka)来帮我们解决这个事情。


提供者与消费者

在服务调用关系中,会有两个不同的角色:

  • 服务提供者:一次业务中,被其它微服务调用的服务。(提供接口给其它微服务)

  • 服务消费者:一次业务中,调用其它微服务的服务。(调用其它微服务提供的接口)

image-20210713214404481

但是,服务提供者与服务消费者的角色并不是绝对的,而是相对于业务而言。

如果服务A调用了服务B,而服务B又调用了服务C,服务B的角色是什么?

  • 对于A调用B的业务而言:A是服务消费者,B是服务提供者
  • 对于B调用C的业务而言:B是服务消费者,C是服务提供者

因此,服务B既可以是服务提供者,也可以是服务消费者。


原文地址:https://blog.51cto.com/panyujie/5830421

免责声明:本文来源于互联网,版权归合法拥有者所有,如有侵权请公众号联系管理员

* 本站提供的一些文章、资料是供学习研究之用,如用于商业用途,请购买正版。

Perceus