Motan服务接入

此篇文介绍如何将 Motan 服务接入到 Apache ShenYu 网关,Apache ShenYu 网关使用 motan 插件来接入Motan服务。

接入前,请正确启动 shenyu-admin,并开启motan插件,在网关端和motan服务端引入相关依赖。可以参考前面的 Motan快速开始

应用客户端接入的相关配置请参考:客户端接入配置

数据同步的相关配置请参考:数据同步配置

在网关中引入 motan 插件

引入网关对Motan的代理插件,在网关的 pom.xml 文件中增加如下依赖:

  1. <!-- apache shenyu motan plugin -->
  2. <dependency>
  3. <groupId>org.apache.shenyu</groupId>
  4. <artifactId>shenyu-spring-boot-starter-plugin-motan</artifactId>
  5. <version>${project.version}</version>
  6. </dependency>
  7. <dependency>
  8. <groupId>com.weibo</groupId>
  9. <artifactId>motan-core</artifactId>
  10. <version>1.1.9</version>
  11. </dependency>
  12. <dependency>
  13. <groupId>com.weibo</groupId>
  14. <artifactId>motan-registry-zookeeper</artifactId>
  15. <version>1.1.9</version>
  16. </dependency>
  17. <dependency>
  18. <groupId>com.weibo</groupId>
  19. <artifactId>motan-transport-netty4</artifactId>
  20. <version>1.1.9</version>
  21. </dependency>
  22. <dependency>
  23. <groupId>com.weibo</groupId>
  24. <artifactId>motan-springsupport</artifactId>
  25. <version>1.1.9</version>
  26. </dependency>
  • 重启你的网关服务。

Motan服务接入网关

可以参考: shenyu-examples-motan

  1. 在由Motan构建的微服务中,引入如下依赖:
  1. <dependency>
  2. <groupId>org.apache.shenyu</groupId>
  3. <artifactId>shenyu-spring-boot-starter-client-motan</artifactId>
  4. <version>${shenyu.version}</version>
  5. </dependency>
  1. application.yaml 配置文件增加如下配置:
  1. shenyu:
  2. register:
  3. registerType: http #zookeeper #etcd #nacos #consul
  4. serverLists: http://localhost:9095 #localhost:2181 #http://localhost:2379 #localhost:8848
  5. props:
  6. username: admin
  7. password: 123456
  8. client:
  9. motan:
  10. props:
  11. contextPath: /motan
  12. ipAndPort: motan
  13. appName: motan
  14. port: 8081
  15. motan:
  16. registry:
  17. protocol: zookeeper
  18. address: 127.0.0.1:2181
  1. Motan服务接口实现类的方法上加上注解@ShenyuMotanClient,启动你的服务提供者,成功注册后,在后台管理系统进入插件列表 -> rpc proxy -> motan,会看到自动注册的选择器和规则信息。

示例:

  1. @MotanService(export = "demoMotan:8002")
  2. public class MotanDemoServiceImpl implements MotanDemoService {
  3. @Override
  4. @ShenyuMotanClient(path = "/hello")
  5. public String hello(String name) {
  6. return "hello " + name;
  7. }
  8. }

用户请求

可以通过 http 的方式来请求你的motan服务。Apache ShenYu网关需要有一个路由前缀,这个路由前缀就是接入网关配置的 contextPath。比如: http://localhost:9195/motan/hello