【spring Cloud】微服务通信的三种方式RestTemplate、Feign远程调用与Dubbo的使用

目录

一、通过RestTemplate调用微服务

二、通过Feign远程调用

三、Dubbo 


分布式中的远程调用大概分为两种

RESTful接口 

REST,即Representational State Transfer的缩写,如果一个架构符合REST原则,就称它为RESTful架构。

  • 每一个URI代表一种资源;
  • 客户端和服务器之间,传递这种资源的某种表现层;
  • 客户端通过四个HTTP动词,对服务器端资源进行操作,实现"表现层状态转化"。

RPC协议

RPC( Remote Procedure Call )一种进程间通信方式。允许像调用本地服务一样调用远程服务。 RPC框架的主要目标就是让远程服务调用更简单、透明。 RPC框架负责屏蔽底层的传输方式(TCP或者UDP)、序列化方式(XML/JSON/二进制)和通信细节。开发人员在使用的时候只需要了解谁在什么位置提供了什么样的远程服务接口即可,并不需要关心底层通信细节和调用过程。

区别与联系 

比较项

RESTful

RPC

通讯协议

HTTP

一般使用TCP

性能

略低

较高

灵活度

应用

微服务架构

SOA架构

首先说下我这个案例的基本模型,通过订单微服务与商品微服务,查询商品实现下订单。

前两种方法都是用RESTful接口最后一个用的是RPC协议

一、通过RestTemplate调用微服务

配置RestTemplate交给spring管理

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

写配置类或者启动类中都行

通过restTemplate调用商品微服务

订单微服务中控制器层自动注入RestTemplate 

通过 restTemplate.getForObject 方法获取商品对象

    @Autowired
    private RestTemplate restTemplate;

    ......

    Product pr = restTemplate.getForObject("http://localhost:8081/product/" + pid, Product.class);

缺点

  • 代码可读性差,编程体验不统一
  • 参数复杂URL难以维护 

二、通过Feign远程调用

首先加入导入坐标

        org.springframework.cloud

        spring-cloud-starter-openfeign

在启动类上添加Fegin的注解

@EnableFeignClients//开启Fegin

在订单微服务中创建一个service,并使用Fegin实现微服务调用

@FeignClient("service-product")//声明调用的提供者的name 
public interface ProductService {
    //指定调用提供者的哪个方法 
    //@FeignClient+@GetMapping 就是一个完整的请求路径 http://service- product/product/{pid} 
    @GetMapping(value = "/product/{pid}")
    Product findById(@PathVariable("pid") int pid);
}

订单微服务中控制器层改写方法 获取商品对象

    @Autowired
    private ProductService productService;
    
    ......

    Product pr = productService.findById(pid);

配置文件中

feign:  
  client:
    config: 
      service-product: # 针对某个微服务的配置
      # default: # 这里用default就是全局配置,如果是写服务名称,则是针对某个微服务的配置
         loggerLevel: FULL #  日志级别 
#需要把日志级别设置
logging:
  level:
    com.apesource: debug

而日志的级别分为四种:

  1. NONE:不记录任何日志信息,这是默认值。
  2. BASIC:仅记录请求的方法, URL以及响应状态码和执行时间
  3. HEADERS:在BASIC的基础上,额外记录了请求和响应的头信息
  4. FULL:记录所有请求和响应的明细,包括头信息、请求体、元数据。

Feign使用优化 

Feign底层发起http请求,依赖于其它的框架。其底层客户端实现包括:

  • URLConnection:默认实现,不支持连接池
  • Apache HttpClient :支持连接池
  • OKHttp:支持连接池

因此提高Feign的性能主要手段就是使用连接池代替默认的URLConnection
这里我们用Apache的HttpClient来演示

导入依赖

        io.github.openfeign

        feign-httpclient

配置文件

feign:
  client:
    config:
      default: # default全局的配置
        loggerLevel: BASIC # 日志级别,BASIC就是基本的请求和响应信息
  httpclient:
    enabled: true # 开启feign对HttpClient的支持
    max-connections: 200 # 最大的连接数
    max-connections-per-route: 50 # 每个路径的最大连接数

总结:

  1. 日志级别尽量用basic
  2. 使用HttpClient或OKHttp代替URLConnection

三、Dubbo 

Dubbo是阿里巴巴开源的基于Java的高性能RPC一种远程调用) 分布式服务框架,致力于提供高性能和透明化的RPC远程服务调用方案,以及SOA服务治理方案。

首先在common服务中添加一个IProductService接口

public interface IProductService {
    Product findById(int pid);
}

在商品微服务中(服务提供者

添加Dobbo依赖

        com.alibaba.cloud

        spring-cloud-starter-dubbo

添加dubbo配置

dubbo: 
    scan: 
        base-packages: com.cc.service.impl # 开启包扫描
    protocols: 
        dubbo: 
            name: dubbo # 服务协议 
            port: -1 # 服务端口 使用随机端口
    registry: 
        address: spring-cloud://localhost # 注册中心

创建IProductServiceImpl实现类

//暴露服务:注意这里使用的是dubbo提供的注解@Service,而不是Spring的
@Service
public class IProductServiceImpl implements IProductService {
    @Autowired
    private ProductMapper productMapper;

    @Override
    public Product findById(int pid) {
        return productMapper.selectById(pid);
    }
}

在订单微服务中(服务消费者

导入Dobbo依赖

        com.alibaba.cloud

        spring-cloud-starter-dubbo

添加dobbo配置

dubbo: 
    registry: 
        address: spring-cloud://localhost # 注册中心
    cloud: 
        subscribed-services: service-product # 订阅的提供者名称

 在订单微服务中控制器层改写方法 获取商品对象

    @Reference
    private IProductService productService;

    ......

    Product pr = productService.findById(pid);

你可能感兴趣的:(spring,cloud,微服务,dubbo)