其他调用示例
提供YAR协议服务
YAR协议是php的一个rpc扩展,motan框架可以提供yar协议的RPC服务1、引入motan-protocol-yar.jar
- <dependency>
- <groupId>com.weibo</groupId>
- <artifactId>motan-protocol-yar</artifactId>
- <version>RELEASE</version>
- </dependency>
2、在服务接口类上增加注解@YarConfig,声明服务的uri
- @YarConfig(path = "/openapi/yarserver/test")
- public interface YarService {
- public String hello(String name);
- }
3、配置protocol的name="yar"
- <motan:protocol id="demoYar" name="yar" .../>
4、配置service的export,使用yar协议提供服务
- <motan:service interface="com.weibo.motan.demo.service.YarService"
- export="demoYar:8003" .../>
具体配置见motan-demo模块YAR协议使用yar-java进行解析,java作为YAR client时可以直接使用
使用注解方式配置motan
server端配置
1、声明Annotation用来指定需要解析的包名
- @Bean
- public AnnotationBean motanAnnotationBean() {
- AnnotationBean motanAnnotationBean = new AnnotationBean();
- motanAnnotationBean.setPackage("com.weibo.motan.demo.server");
- return motanAnnotationBean;
- }
2、配置ProtocolConfig、RegistryConfig、BasicServiceConfig的bean对象,功能与xml配置中的protocol、registry、basicService标签一致。
- @Bean(name = "demoMotan")
- public ProtocolConfigBean protocolConfig1() {
- ProtocolConfigBean config = new ProtocolConfigBean();
- config.setDefault(true);
- config.setName("motan");
- config.setMaxContentLength(1048576);
- return config;
- }
- @Bean(name = "registryConfig1")
- public RegistryConfigBean registryConfig() {
- RegistryConfigBean config = new RegistryConfigBean();
- config.setRegProtocol("local");
- return config;
- }
- @Bean
- public BasicServiceConfigBean baseServiceConfig() {
- BasicServiceConfigBean config = new BasicServiceConfigBean();
- config.setExport("demoMotan:8002");
- config.setGroup("testgroup");
- config.setAccessLog(false);
- config.setShareChannel(true);
- config.setModule("motan-demo-rpc");
- config.setApplication("myMotanDemo");
- config.setRegistry("registryConfig1");
- return config;
- }
3、service的实现类上添加@MotanService注解,注解的配置参数与xml配置方式的service标签一致。
- @MotanService(export = "demoMotan:8002")
- public class MotanDemoServiceImpl implements MotanDemoService {
- public String hello(String name) {
- System.out.println(name);
- return "Hello " + name + "!";
- }
- }
4、使用spring-boot启动服务
- @EnableAutoConfiguration
- @SpringBootApplication
- public class SpringBootRpcServerDemo {
- public static void main(String[] args) {
- System.setProperty("server.port", "8081");
- ConfigurableApplicationContext context = SpringApplication.run(SpringBootRpcServerDemo.class, args);
- MotanSwitcherUtil.setSwitcherValue(MotanConstants.REGISTRY_HEARTBEAT_SWITCHER, true);
- System.out.println("server start...");
- }
- }
server端详细配置请参考motan-demo模块
client端配置
1、声明Annotation、protocolConfig、RegistryConfig的配置bean。方式与server端配置类似。
2、配置basicRefererConfig bean
- @Bean(name = "motantestClientBasicConfig")
- public BasicRefererConfigBean baseRefererConfig() {
- BasicRefererConfigBean config = new BasicRefererConfigBean();
- config.setProtocol("demoMotan");
- config.setGroup("motan-demo-rpc");
- config.setModule("motan-demo-rpc");
- config.setApplication("myMotanDemo");
- config.setRegistry("registry");
- config.setCheck(false);
- config.setAccessLog(true);
- config.setRetries(2);
- config.setThrowException(true);
- return config;
- }
3、在使用motan service 的对象上添加@MotanReferer注解,注册配置与xml方式的referer标签一致
- @RestController
- public class HelloController {
- @MotanReferer(basicReferer = "motantestClientBasicConfig", group = "testgroup", directUrl = "127.0.0.1:8002")
- MotanDemoService service;
- @RequestMapping("/")
- @ResponseBody
- public String home() {
- String result = service.hello("test");
- return result;
- }
- }
4、使用spring-boot启动client
- @EnableAutoConfiguration
- @SpringBootApplication
- public class SpringBootRpcClientDemo {
- public static void main(String[] args) {
- SpringApplication.run(SpringBootRpcClientDemo.class, args);
- }
- }
client端详细配置请参考motan-demo模块
使用restful协议
功能支持
- 支持rpc单独进程和部署到servlet容器中
- 完全支持原有服务治理功能
- 支持rpc request/response的attachment机制
- 完全支持rpc filter机制
- rest服务编程完全按照JAX-RS代码方式编写
前置条件
引入motan-protocol-restful包
- <dependency>
- <groupId>com.weibo</groupId>
- <artifactId>motan-protocol-restful</artifactId>
- <version>RELEASE</version>
- </dependency>
接口声明与实现
服务接口
- @Path("/rest")
- public interface RestfulService {
- @GET
- @Produces(MediaType.APPLICATION_JSON)
- List<User> getUsers(@QueryParam("uid") int uid);
- @GET
- @Path("/primitive")
- @Produces(MediaType.TEXT_PLAIN)
- String testPrimitiveType();
- @POST
- @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
- @Produces(MediaType.APPLICATION_JSON)
- Response add(@FormParam("id") int id, @FormParam("name") String name);
- @GET
- @Path("/exception")
- @Produces(MediaType.APPLICATION_JSON)
- void testException();
- }
服务实现
- public class RestfulServerDemo implements RestfulService {
- @Override
- public List<User> getUsers(@CookieParam("uid") int uid) {
- return Arrays.asList(new User(uid, "name" + uid));
- }
- @Override
- public String testPrimitiveType() {
- return "helloworld!";
- }
- @Override
- public Response add(@FormParam("id") int id, @FormParam("name") String name) {
- return Response.ok().cookie(new NewCookie("ck", String.valueOf(id))).entity(new User(id, name)).build();
- }
- @Override
- public void testException() {
- throw new UnsupportedOperationException("unsupport");
- }
- }
配置restserver
独立rpc进程方式
server端配置:
- <bean id="motanDemoServiceImpl" class="com.weibo.motan.demo.server.RestfulServerDemo"/>
- <motan:registry regProtocol="local" name="registry" />
- <motan:protocol id="demoRest" name="restful" endpointFactory="netty"/>
- <motan:basicService export="demoRest:8004" group="motan-demo-rpc" module="motan-demo-rpc"
- application="myMotanDemo" registry="registry" id="serviceBasicConfig"/>
- <motan:service interface="com.weibo.motan.demo.service.RestfulService"
- ref="motanDemoServiceImpl" basicService="serviceBasicConfig"/>
client端配置:
- <motan:registry regProtocol="direct" name="registry" address="127.0.0.1:8004"/>
- <!-- restful 协议 -->
- <motan:protocol id="restfulProtocol" name="restful" endpointFactory="netty"/>
- <!-- 通用referer基础配置 -->
- <motan:basicReferer requestTimeout="1000" group="motan-demo-rpc" module="motan-demo-rpc"
- application="myMotanDemo" protocol="restfulProtocol" registry="registry"
- id="motanTestClientBasicConfig" />
- <!-- 使用 restful 协议-->
- <motan:referer id="restfulReferer" interface="com.weibo.motan.demo.service.RestfulService"
- 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