服务的拆分和远程调用

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

服务拆分原则

微服务拆分时的几个原则:

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

远程调用

案例需求:

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

因此,我们需要在order-service中 向user-service发起一个http的请求,调用http://localhost:8081/user/{userId}这个接口。

大概的步骤是这样的:

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

注册RestTemplate(Java发HTTP请求)

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.web.client.RestTemplate;

@Configuration
public class RestTemplateConfig {
          
   

	@Bean
	@Primary
	public RestTemplate restTemplate() {
          
   
		RestTemplate restTemplate =new RestTemplate();
		return  restTemplate;
	}
}

服务调用(根据Order对象中的userId查询User)

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;

    public Order queryOrderById(Long orderId) {
          
   
        // 1.查询订单
        Order order = orderMapper.findById(orderId);
        // 2.利用RestTemplate发起http请求,查询用户
        // 2.1.url路径
        String url = "http://userservice/user/" + order.getUserId();
        // 2.2.发送http请求,实现远程调用
        User user = restTemplate.getForObject(url, User.class);
        // 3.封装user到Order
        order.setUser(user);
        // 4.返回
        return order;
    }
}

常用的远程调用方法。

    getForEntity() 发送一个HTTP GET请求,返回的ResponseEntity包含了响应体所映射成的对象 getForObject() 发送一个HTTP GET请求,返回的请求体将映射为一个对象 postForEntity() ,POST 数据到一个URL,返回包含一个对象的ResponseEntity,这个对象是从响应体中映射得到的 postForObject() POST 数据到一个URL,返回根据响应体匹配形成的对象

微服务调用方式

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

提供者与消费者

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

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

服务消费者:一次业务中,调用其它微服务的服务。(调用其它微服务提供的接口) 但是,服务提供者与服务消费者的角色并不是绝对的,而是相对于业务而言。

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

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

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

经验分享 程序员 微信小程序 职场和发展