likes
comments
collection
share

玩转Motan RPC-基于Spirng boot 配置

作者站长头像
站长
· 阅读数 30

概述:

写作原由:

公司项目目前使用的Motan,基于Motan技术栈需要自我学习且公司使用的单机版本,故此需要重新学会搭建如何使用Motan,从而入门。

Motan 是一个由微博开源的远程过程调用(RPC)框架,它旨在提供简单、高性能和易扩展的RPC解决方案。它主要用于构建大规模的分布式系统,支持服务治理、负载均衡、故障转移等特性。

Motan 的原理:

  1. 服务提供者:服务提供者将其提供的服务注册到注册中心,并监听来自服务消费者的调用请求。

  2. 服务消费者:服务消费者从注册中心查询可用的服务提供者,并根据负载均衡策略选择一个服务提供者进行远程调用。

  3. 注册中心:注册中心负责维护服务提供者的信息,服务消费者通过注册中心来发现服务。

  4. 通信协议:Motan 支持多种通信协议,如 Motan 自己的协议、HTTP、gRPC 等,以实现服务之间的通信。

  5. 序列化/反序列化:Motan 支持多种序列化/反序列化机制,如 Protobuf、JSON、Hessian 等,以高效地传输数据。

  6. 负载均衡:Motan 提供多种负载均衡策略,如随机、轮询、一致性哈希等。

  7. 服务治理:Motan 提供服务治理功能,如服务配置的动态下发、服务降级、服务容错等。

  8. 过滤链:Motan 允许用户通过过滤链来插入自定义的过滤器,实现特定的中间件逻辑,如日志记录、权限校验等。

Motan 的优点:

  1. 简单易用:Motan 设计简洁,提供了易于理解的配置和API,降低了学习成本。

  2. 性能高效:Motan 在设计时考虑了高性能的需求,通过优化通信协议和序列化方式来提高效率。

  3. 服务治理:提供了服务治理的完整解决方案,包括服务发现、配置管理、负载均衡、故障转移等。

  4. 支持多协议:支持多种通信协议和序列化方式,可以根据需要灵活选择。

  5. 扩展性强:Motan 的架构支持扩展,用户可以根据自己的需求增加新的组件。

  6. 社区支持:作为一个开源项目,Motan 有着活跃的社区和不断的更新维护。

Motan 的缺点:

  1. 社区相对较小:与一些大型的RPC框架(如gRPC、Dubbo)相比,Motan 的社区规模较小,可能在遇到问题时获取帮助的速度较慢。

  2. 生态系统:虽然 Motan 提供了丰富的功能,但其生态系统不如一些主流的RPC框架那样完善。

  3. 文档和资源:相对于其他流行的RPC框架,Motan 的文档和学习资源可能不那么丰富。

  4. 企业使用案例:虽然 Motan 在微博内部有大规模的使用,但在其他企业中的使用案例相对较少,可能会影响新用户的信心。

流程图:

Motan RPC框架的流程图。此流程图展示了服务提供者、服务消费者、注册中心和监控系统之间的交互:

Monitor System
Registry Center
Service Provider
Service Consumer
Discover Services
Return Providers List
Register Service
Notify Consumers
Send Request
Process Request
Send Response
Return Response
Report to Monitor
Report to Monitor
Aggregate Data
Monitor System
Registry Center
Service Provider
Service Consumer

在这个流程中,可以看到以下步骤:

  1. 服务提供者 (Service Provider) 向 注册中心 (Registry Center) 注册其提供的服务。
  2. 服务消费者 (Service Consumer) 从 注册中心 (Registry Center) 发现可用的服务,并获取服务提供者列表。
  3. 注册中心将服务提供者的信息通知给服务消费者。
  4. 服务消费者向服务提供者发送请求。
  5. 服务提供者处理请求并返回响应给服务消费者。
  6. 服务提供者和服务消费者都向 监控系统 (Monitor System) 报告调用数据。
  7. 监控系统聚合数据并进行分析。

时序图

该图展示了服务消费者、服务提供者和注册中心之间的交互过程:

ConsumerRegistryProvider    C->>R: Query for servicesReturn providers listSend RPC requestRegister serviceAcknowledge registrationProcess requestSend RPC responseReport invocation to registryReport status to registryConsumerRegistryProvider    C->>R: Query for services

在这个时序图中,可以看到以下步骤:

1. 服务消费者 (Consumer) 向 注册中心 (Registry) 查询可用的服务。 2. 注册中心返回服务提供者列表给服务消费者。 3. 服务消费者向服务提供者发送RPC请求。 4. 服务提供者 (Provider) 向注册中心注册服务。 5. 注册中心确认服务注册。 6. 服务提供者处理RPC请求。 7. 服务提供者将RPC响应发送回服务消费者。 8. 服务消费者向注册中心报告调用情况。 9. 服务提供者向注册中心报告服务状态。

使用步骤:

简化版:

在Spring环境中使用Motan时,可以通过Spring的自动配置和注解支持来简化服务的提供和消费。 以下是一个简化的示例,展示了如何在Spring项目中封装Motan的工具类,以便更方便地使用。 首先,需要在项目的pom.xml中添加Motan和Spring相关的依赖:

<dependencies>
    
    <dependency>
        <groupId>com.weibo</groupId>
        <artifactId>motan-core</artifactId>
        <version>1.1.10</version>
    </dependency>
    <dependency>
        <groupId>com.weibo</groupId>
        <artifactId>motan-spring</artifactId>
       <version>1.1.10</version>
    </dependency>
	
	 <dependency>
        <groupId>com.weibo</groupId>
        <artifactId>motan-springsupport</artifactId>
        <version>1.1.10</version>
    </dependency>
    
</dependencies>

接着,创建一个配置类来配置Motan,包括注册中心、协议和注解扫描:

@Configuration
@ImportResource(locations = "classpath:motan.xml")
public class MotanConfig {
    // 可以在这里定义一些Motan的配置Bean,或者直接在XML文件中配置
}

resources目录下创建motan.xml配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:motan="http://motan.io/schema/motan"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                           http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://motan.io/schema/motan
                           http://motan.io/schema/motan.xsd">

    <!-- 注册中心配置 -->
    <motan:registry regProtocol="zookeeper" address="127.0.0.1:2181" />

	<!-- 协议配置 -->
    <motan:protocol name="motan" default="true" maxContentLength="1048576" 
                    haStrategy="failover" loadbalance="roundrobin" />
					
					
	  <!-- 服务监控 -->
    <motan:basicService requestTimeout="200" accessLog="false" 
                        shareChannel="true" registry="registry" 
                        group="motan-demo-rpc" module="motan-demo-rpc" 
                        application="myMotanDemo" version="1.0" 
                        throwException="true" filter="statistic"/>
    
    <!-- 服务端配置 -->
    <motan:basicService export="motan:8002" />

    <!-- 客户端配置 -->
    <motan:basicReferer requestTimeout="200" accessLog="false" 
                        throwException="true" check="false" 
                        registry="registry" group="motan-demo-rpc" 
                        module="motan-demo-rpc" application="myMotanDemo" 
                        version="1.0" filter="statistic"/>
    
    <!-- 扫描注解的包路径 -->
    <motan:annotation package="com.derek.mall" />
</beans>				

接下来,定义服务接口和实现:

public interface HelloService {
    String hello(String name);
}

@Service
@MotanService(export = "motan:8002") // 指定服务协议和端口
public class HelloServiceImpl implements HelloService {
    @Override
    public String hello(String name) {
        return "Hello, " + name;
    }
}

在服务消费端,使用@MotanReferer注解来注入远程服务:

@Component
public class ServiceConsumer {
    @MotanReferer
    private HelloService helloService;

    public void execute() {
        String result = helloService.hello("World");
        System.out.println(result);
    }
}

最后,在Spring Boot的主类或其他配置类中启用Motan的自动配置:

@SpringBootApplication
@EnableMotan
public class MotanSpringApplication {
    public static void main(String[] args) {
        SpringApplication.run(MotanSpringApplication.class, args);
    }
}

以上就是一个基本的Motan与Spring集成的示例。这个配置通过Spring的自动装配和Motan的注解支持,使得定义和消费服务变得非常简单。当然,实际项目中的配置可能会更复杂,包括安全性、事务管理、服务监控等方面的考虑, 但上述示例提供了一个基本的起点

优化版:

当然也可以继续优化:

在服务提供方,可以使用@MotanService注解来暴露服务,并配置一些高级特性,如服务降级:

@Service
@MotanService(export = "motan:8002", basicService = "serviceBasicConfig")
public class HelloServiceImpl implements HelloService {
    @Override
    public String hello(String name) {
        // 实现服务逻辑
        return "Hello, " + name;
    }
}

在服务消费方,可以通过@MotanReferer注解来引用远程服务,并配置一些高级特性,如请求限流:

@Component
public class ServiceConsumer {
    @MotanReferer(basicReferer = "refererBasicConfig")
    private HelloService helloService;

    public void execute() {
        // 调用远程服务
        String result = helloService.hello("World");
        System.out.println(result);
    }
}

最终版:

为了进一步优化上面的Motan示例,提供一个更为详细的配置,包括服务端和客户端的高级特性设置。 这些配置可以帮助更好地管理服务治理、监控、负载均衡、安全性和性能。这里,将使用Spring Boot进行配置,而不是使用XML文件。 首先,确保pom.xml包含Motan和Spring Boot的相关依赖:

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
        <version>YOUR_SPRING_BOOT_VERSION</version>
    </dependency>
    
    
    <dependency>
        <groupId>com.weibo</groupId>
        <artifactId>motan-springsupport</artifactId>
        <version>YOUR_MOTAN_VERSION</version>
    </dependency>

    <!-- 其他依赖... -->
</dependencies>

然后在Spring Boot的配置类中声明Motan的配置:

@Configuration
public class MotanConfig {

    @Bean
    public BasicServiceConfigBean serviceConfig() {
        BasicServiceConfigBean serviceConfig = new BasicServiceConfigBean();
        serviceConfig.setExport("motan:8002"); // 设置服务端口
        serviceConfig.setGroup("motan-demo-rpc"); // 设置服务组
        serviceConfig.setAccessLog(false); // 设置是否记录访问日志
        serviceConfig.setShareChannel(true); // 设置是否共享通道
        serviceConfig.setModule("motan-demo-rpc"); // 设置模块名称
        serviceConfig.setApplication("myMotanDemo"); // 设置应用名称
        serviceConfig.setRegistry("registry"); // 设置注册中心
        serviceConfig.setVersion("1.0"); // 设置服务版本
        serviceConfig.setFilter("statistic"); // 设置服务过滤器,例如统计信息
        // 其他需要的配置...
        return serviceConfig;
    }

    @Bean
    public BasicRefererConfigBean refererConfig() {
        BasicRefererConfigBean refererConfig = new BasicRefererConfigBean();
        refererConfig.setProtocol("motan"); // 设置协议
        refererConfig.setGroup("motan-demo-rpc"); // 设置服务组
        refererConfig.setCheck(false); // 设置启动时不检查服务提供者是否存在
        refererConfig.setModule("motan-demo-rpc"); // 设置模块名称
        refererConfig.setApplication("myMotanDemo"); // 设置应用名称
        refererConfig.setRegistry("registry"); // 设置注册中心
        refererConfig.setVersion("1.0"); // 设置服务版本
        refererConfig.setFilter("statistic"); // 设置服务过滤器,例如统计信息
        refererConfig.setRequestTimeout(200); // 设置请求超时时间
        // 其他需要的配置...
        return refererConfig;
    }

    @Bean
    public RegistryConfigBean registryConfig() {
        RegistryConfigBean registryConfig = new RegistryConfigBean();
        registryConfig.setRegProtocol("zookeeper");
        registryConfig.setAddress("127.0.0.1:2181");
        // 其他需要的配置...
        return registryConfig;
    }

    @Bean
    public ProtocolConfigBean protocolConfig() {
        ProtocolConfigBean protocolConfig = new ProtocolConfigBean();
        protocolConfig.setName("motan");
        protocolConfig.setMaxContentLength(1048576);
        protocolConfig.setHaStrategy("failover");
        protocolConfig.setLoadbalance("roundrobin");
        // 其他需要的配置...
        return protocolConfig;
    }
}

现在,已经使用Java配置替代了XML配置。接下来,定义服务提供者和消费者:

@Service
@MotanService(basicService = "serviceConfig")
public class HelloServiceImpl implements HelloService {
    @Override
    public String hello(String name) {
        return "Hello, " + name;
    }
}

@Component
public class ServiceConsumer {
    @MotanReferer(basicReferer = "refererConfig")
    private HelloService helloService;

    public void execute() {
        String result = helloService.hello("World");
        System.out.println(result);
    }
}

最后,更新Spring Boot主类:

@SpringBootApplication
@EnableMotan
public class MotanSpringBootApplication {
    public static void main(String[] args) {
        SpringApplication.run(MotanSpringBootApplication.class, args);
    }
}

这个配置示例展示了如何使用Java配置来代替XML配置,这样可以更好地集成到Spring Boot应用中,并且更易于版本控制和维护。通过这种方式,可以轻松地管理Motan的配置,并且可以利用Spring Boot的自动配置功能。此外,还可以根据需要添加更多的配置项,例如安全性、请求限流、服务监控等。

总结:

总的来说,Motan 是一个功能强大且易于使用的RPC框架,适合构建大规模的分布式服务。它在性能和服务治理方面表现出色,但在社区支持和生态系统上使用者较少。

转载自:https://juejin.cn/post/7368758932153679924
评论
请登录