其他调用示例

提供YAR协议服务

YAR协议是php的一个rpc扩展,motan框架可以提供yar协议的RPC服务1、引入motan-protocol-yar.jar

  1. <dependency>
  2. <groupId>com.weibo</groupId>
  3. <artifactId>motan-protocol-yar</artifactId>
  4. <version>RELEASE</version>
  5. </dependency>

2、在服务接口类上增加注解@YarConfig,声明服务的uri

  1. @YarConfig(path = "/openapi/yarserver/test")
  2. public interface YarService {
  3. public String hello(String name);

3、配置protocol的name="yar"

  1. <motan:protocol id="demoYar" name="yar" .../>

4、配置service的export,使用yar协议提供服务

  1. <motan:service interface="com.weibo.motan.demo.service.YarService"
  2. export="demoYar:8003" .../>

具体配置见motan-demo模块YAR协议使用yar-java进行解析,java作为YAR client时可以直接使用

使用注解方式配置motan

server端配置

1、声明Annotation用来指定需要解析的包名

  1. @Bean
  2. public AnnotationBean motanAnnotationBean() {
  3. AnnotationBean motanAnnotationBean = new AnnotationBean();
  4. motanAnnotationBean.setPackage("com.weibo.motan.demo.server");
  5. return motanAnnotationBean;
  6. }

2、配置ProtocolConfig、RegistryConfig、BasicServiceConfig的bean对象,功能与xml配置中的protocol、registry、basicService标签一致。

  1. @Bean(name = "demoMotan")
  2. public ProtocolConfigBean protocolConfig1() {
  3. ProtocolConfigBean config = new ProtocolConfigBean();
  4. config.setDefault(true);
  5. config.setName("motan");
  6. config.setMaxContentLength(1048576);
  7. return config;
  8. }
  9.  
  10. @Bean(name = "registryConfig1")
  11. public RegistryConfigBean registryConfig() {
  12. RegistryConfigBean config = new RegistryConfigBean();
  13. config.setRegProtocol("local");
  14. return config;
  15. }
  16.  
  17. @Bean
  18. public BasicServiceConfigBean baseServiceConfig() {
  19. BasicServiceConfigBean config = new BasicServiceConfigBean();
  20. config.setExport("demoMotan:8002");
  21. config.setGroup("testgroup");
  22. config.setAccessLog(false);
  23. config.setShareChannel(true);
  24. config.setModule("motan-demo-rpc");
  25. config.setApplication("myMotanDemo");
  26. config.setRegistry("registryConfig1");
  27. return config;
  28. }

3、service的实现类上添加@MotanService注解,注解的配置参数与xml配置方式的service标签一致。

  1. @MotanService(export = "demoMotan:8002")
  2. public class MotanDemoServiceImpl implements MotanDemoService {
  3.  
  4. public String hello(String name) {
  5. System.out.println(name);
  6. return "Hello " + name + "!";
  7. }
  8. }

4、使用spring-boot启动服务

  1. @EnableAutoConfiguration
  2. @SpringBootApplication
  3. public class SpringBootRpcServerDemo {
  4.  
  5. public static void main(String[] args) {
  6. System.setProperty("server.port", "8081");
  7. ConfigurableApplicationContext context = SpringApplication.run(SpringBootRpcServerDemo.class, args);
  8.  
  9. MotanSwitcherUtil.setSwitcherValue(MotanConstants.REGISTRY_HEARTBEAT_SWITCHER, true);
  10. System.out.println("server start...");
  11. }
  12. }

server端详细配置请参考motan-demo模块

client端配置

1、声明Annotation、protocolConfig、RegistryConfig的配置bean。方式与server端配置类似。

2、配置basicRefererConfig bean

  1. @Bean(name = "motantestClientBasicConfig")
  2. public BasicRefererConfigBean baseRefererConfig() {
  3. BasicRefererConfigBean config = new BasicRefererConfigBean();
  4. config.setProtocol("demoMotan");
  5. config.setGroup("motan-demo-rpc");
  6. config.setModule("motan-demo-rpc");
  7. config.setApplication("myMotanDemo");
  8. config.setRegistry("registry");
  9. config.setCheck(false);
  10. config.setAccessLog(true);
  11. config.setRetries(2);
  12. config.setThrowException(true);
  13. return config;
  14. }

3、在使用motan service 的对象上添加@MotanReferer注解,注册配置与xml方式的referer标签一致

  1. @RestController
  2. public class HelloController {
  3.  
  4. @MotanReferer(basicReferer = "motantestClientBasicConfig", group = "testgroup", directUrl = "127.0.0.1:8002")
  5. MotanDemoService service;
  6.  
  7. @RequestMapping("/")
  8. @ResponseBody
  9. public String home() {
  10. String result = service.hello("test");
  11. return result;
  12. }
  13. }

4、使用spring-boot启动client

  1. @EnableAutoConfiguration
  2. @SpringBootApplication
  3. public class SpringBootRpcClientDemo {
  4.  
  5. public static void main(String[] args) {
  6. SpringApplication.run(SpringBootRpcClientDemo.class, args);
  7. }
  8. }

client端详细配置请参考motan-demo模块

使用restful协议

功能支持

  • 支持rpc单独进程和部署到servlet容器中
  • 完全支持原有服务治理功能
  • 支持rpc request/response的attachment机制
  • 完全支持rpc filter机制
  • rest服务编程完全按照JAX-RS代码方式编写

前置条件

引入motan-protocol-restful包

  1. <dependency>
  2. <groupId>com.weibo</groupId>
  3. <artifactId>motan-protocol-restful</artifactId>
  4. <version>RELEASE</version>
  5. </dependency>

接口声明与实现

服务接口

  1. @Path("/rest")
  2. public interface RestfulService {
  3. @GET
  4. @Produces(MediaType.APPLICATION_JSON)
  5. List<User> getUsers(@QueryParam("uid") int uid);
  6.  
  7. @GET
  8. @Path("/primitive")
  9. @Produces(MediaType.TEXT_PLAIN)
  10. String testPrimitiveType();
  11.  
  12. @POST
  13. @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
  14. @Produces(MediaType.APPLICATION_JSON)
  15. Response add(@FormParam("id") int id, @FormParam("name") String name);
  16.  
  17. @GET
  18. @Path("/exception")
  19. @Produces(MediaType.APPLICATION_JSON)
  20. void testException();
  21. }

服务实现

  1. public class RestfulServerDemo implements RestfulService {
  2.  
  3. @Override
  4. public List<User> getUsers(@CookieParam("uid") int uid) {
  5. return Arrays.asList(new User(uid, "name" + uid));
  6. }
  7.  
  8. @Override
  9. public String testPrimitiveType() {
  10. return "helloworld!";
  11. }
  12.  
  13. @Override
  14. public Response add(@FormParam("id") int id, @FormParam("name") String name) {
  15. return Response.ok().cookie(new NewCookie("ck", String.valueOf(id))).entity(new User(id, name)).build();
  16. }
  17.  
  18. @Override
  19. public void testException() {
  20. throw new UnsupportedOperationException("unsupport");
  21. }
  22. }

配置restserver

独立rpc进程方式

server端配置:

  1. <bean id="motanDemoServiceImpl" class="com.weibo.motan.demo.server.RestfulServerDemo"/>
  2.  
  3. <motan:registry regProtocol="local" name="registry" />
  4.  
  5. <motan:protocol id="demoRest" name="restful" endpointFactory="netty"/>
  6.  
  7. <motan:basicService export="demoRest:8004" group="motan-demo-rpc" module="motan-demo-rpc"
  8. application="myMotanDemo" registry="registry" id="serviceBasicConfig"/>
  9.  
  10. <motan:service interface="com.weibo.motan.demo.service.RestfulService"
  11. ref="motanDemoServiceImpl" basicService="serviceBasicConfig"/>

client端配置:

  1. <motan:registry regProtocol="direct" name="registry" address="127.0.0.1:8004"/>
  2.  
  3. <!-- restful 协议 -->
  4. <motan:protocol id="restfulProtocol" name="restful" endpointFactory="netty"/>
  5.  
  6. <!-- 通用referer基础配置 -->
  7. <motan:basicReferer requestTimeout="1000" group="motan-demo-rpc" module="motan-demo-rpc"
  8. application="myMotanDemo" protocol="restfulProtocol" registry="registry"
  9. id="motanTestClientBasicConfig" />
  10.  
  11. <!-- 使用 restful 协议-->
  12. <motan:referer id="restfulReferer" interface="com.weibo.motan.demo.service.RestfulService"
  13. basicReferer="motanTestClientBasicConfig"/>
集成到java应用服务器中(如部署到tomcat中)

此时需要注意contextpath问题

<motan:protocol name="restful" endpointFactory="servlet" />

服务端还需配置web.xml如下:

 <!-- 此filter必须在spring ContextLoaderFilter之前 -->
 <listener>
     <listener-class>com.weibo.api.motan.protocol.restful.support.servlet.RestfulServletContainerListener</listener-class>
 </listener>

 <servlet>
     <servlet-name>dispatcher</servlet-name>
     <servlet-class>org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher</servlet-class>
     <load-on-startup>1</load-on-startup>
     <init-param>
         <param-name>resteasy.servlet.mapping.prefix</param-name>
         <param-value>/servlet</param-value>  <!-- 此处实际为servlet-mapping的url-pattern,具体配置见resteasy文档-->
     </init-param>
 </servlet>

 <servlet-mapping>
     <servlet-name>dispatcher</servlet-name>
     <url-pattern>/servlet/*</url-pattern>
 </servlet-mapping>

集成到java应用服务器的方式(servlet方式)适合不同语言直接http调用,需要注意url中contextpath的问题。推荐使用rpc进程方式

java作为client端调用时,推荐server端同时导出restful和motan两种协议,java使用motan协议进行调用,其他语言使用标准http协议调用。

详细请参考motan-demo模块中的RestfulServerDemo、RestfulClient