From b68fb1734e1b5ad3c2edd3292fd775cfbbcacb44 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B9=9B=E7=84=B6?= Date: Wed, 26 Apr 2017 11:22:56 +0800 Subject: [PATCH 1/2] modify quickstart --- docs/wiki/en_quickstart.md | 268 +++++++++++++++++++++++++++++++++---- docs/wiki/en_userguide.md | 0 docs/wiki/zh_quickstart.md | 140 ++++++++++--------- 3 files changed, 320 insertions(+), 88 deletions(-) create mode 100644 docs/wiki/en_userguide.md diff --git a/docs/wiki/en_quickstart.md b/docs/wiki/en_quickstart.md index e648a9fc1..a5a6d823d 100644 --- a/docs/wiki/en_quickstart.md +++ b/docs/wiki/en_quickstart.md @@ -7,8 +7,8 @@ The quick start gives very basic example of running server and client on the same machine. For more details about using and developing Motan, please jump to [Documents](en_userguide). > The minimum requirements to run the quick start are: -> * JDK 1.7 or above. -> * A java-based project management software like [Maven][maven] or [Gradle][gradle]. +> * JDK 1.7 or above. +> * A java-based project management software like [Maven][maven] or [Gradle][gradle]. ## Using Motan in single machine environment @@ -19,19 +19,19 @@ The quick start gives very basic example of running server and client on the sam com.weibo motan-core - 0.1.1 + 0.3.0 com.weibo motan-transport-netty - 0.1.1 + 0.3.0 com.weibo motan-springsupport - 0.1.1 + 0.3.0 org.springframework @@ -53,9 +53,9 @@ The quick start gives very basic example of running server and client on the sam ``` 3. Write an implementation, create and start RPC Server. - + `src/main/java/quickstart/FooServiceImpl.java` - + ```java package quickstart; @@ -69,7 +69,7 @@ The quick start gives very basic example of running server and client on the sam ``` `src/main/resources/motan_server.xml` - + ```xml ``` - + `src/main/java/quickstart/Server.java` - + ```java package quickstart; - + import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; - + public class Server { - + public static void main(String[] args) throws InterruptedException { ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:motan_server.xml"); System.out.println("server start..."); } } ``` - + Execute main function in Server will start a Motan server listening on port 8002. 4. Create and start RPC Client. @@ -128,20 +128,21 @@ The quick start gives very basic example of running server and client on the sam import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; + ``` public class Client { - + public static void main(String[] args) throws InterruptedException { ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:motan_client.xml"); FooService service = (FooService) ctx.getBean("remoteService"); System.out.println(service.hello("motan")); } } - ``` + ​``` Execute main function in Client will invoke the remote service and print response. - + ## Using Motan in cluster environment In cluster environment, the external service discovery components such as Consul or ZooKeeper is needed to support the use of Motan. @@ -152,17 +153,17 @@ In cluster environment, the external service discovery components such as Consul #### Install and Start Consul ##### Install([Official Document](https://www.consul.io/intro/getting-started/install.html)) - + # Taking Linux as an example wget https://releases.hashicorp.com/consul/0.6.4/consul_0.6.4_linux_amd64.zip unzip consul_0.6.4_linux_amd64.zip sudo mv consul /bin - + ##### Start([Official Document](https://www.consul.io/intro/getting-started/agent.html)) Starting the test environment: consul agent -dev - + UI backend [http://localhost:8500/ui](http://localhost:8500/ui) #### Motan-Consul configuration @@ -181,7 +182,7 @@ UI backend [http://localhost:8500/ui](http://localhost:8500/ui) ```xml - ``` + ``` 3. Change the way of service discovery to registry in the configuration of server and client. @@ -190,17 +191,19 @@ UI backend [http://localhost:8500/ui](http://localhost:8500/ui) ```xml ``` - + client: ```xml ``` -4. After the server starts, you SHOULD call heartbeat switcher explicitly in order to start heartbeat for Consul. +4. After the server starts, you SHOULD call heartbeat switcher explicitly in order to start heartbeat for Consul in server.java. ```java - MotanSwitcherUtil.setSwitcherValue(MotanConstants.REGISTRY_HEARTBEAT_SWITCHER, true) + ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:motan_server.xml"); + //if you want to use zookeeper or consul + MotanSwitcherUtil.setSwitcherValue(MotanConstants.REGISTRY_HEARTBEAT_SWITCHER, true); ``` 5. Go to [UI backend](http://localhost:8500/ui). Verify whether the service is normal. @@ -237,17 +240,17 @@ Install and start ZooKeeper: 2. Add the definition of ZooKeeper registry in the configuration of server and client. single node ZooKeeper: - + ```xml ``` - + multi-nodes ZooKeeper: ```xml ``` - + 3. Change the way of service discovery to registry in the configuration of server and client. server: @@ -255,7 +258,7 @@ Install and start ZooKeeper: ```xml ``` - + client: ```xml @@ -274,3 +277,212 @@ Install and start ZooKeeper: [maven]:https://maven.apache.org [gradle]:http://gradle.org +[maven]: https://maven.apache.org +[gradle]: http://gradle.org + +## Other call examples + +### Provide YAR protocol services + +[YAR](https://github.com/laruence/yar) protocol is a RPC extension of php, the motan framework can provide yar protocol for RPC services +1、include motan-protocol-yar.jar + +```xml + + com.weibo + motan-protocol-yar + 0.3.0 + +``` + +2、Add annotations @YarConfig to the service interface class to declare the uri of the service + +```java + @YarConfig(path = "/openapi/yarserver/test") + public interface YarService { + public String hello(String name); + } +``` + +3、Config protocol name="yar" + +```xml + +``` + +4、Configure the export of service, using yar protocol to provide services + +```xml + +``` + +To get specific configuration,check motan-demo module. +We use [yar-java](https://github.com/weibocom/yar-java) to parse YAR protocol,java can be used directly as YAR client + +### Use the annotations to configure the motan + +#### Server configuration + +1、Declare Annotation to specify the packages to be parsed + +```java + @Bean + public AnnotationBean motanAnnotationBean() { + AnnotationBean motanAnnotationBean = new AnnotationBean(); + motanAnnotationBean.setPackage("com.weibo.motan.demo.server"); + return motanAnnotationBean; + } +``` + +2、Configure ProtocolConfig、RegistryConfig、BasicServiceConfig's bean Object,which function is consistent with the protocol, registry, and basicService tags in the xml configuration. + +```java + @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、Add the @MotanService annotation to the implementation class of the service. The configuration parameters of the annotation are consistent with the service tag of the xml configuration。 + +```java + @MotanService(export = "demoMotan:8002") + public class MotanDemoServiceImpl implements MotanDemoService { + + public String hello(String name) { + System.out.println(name); + return "Hello " + name + "!"; + } + } +``` + +4、Start service using [spring-boot](https://github.com/spring-projects/spring-boot) + +```java + @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..."); + } + } +``` + +we need to add dependencies in pom + +```xml + + org.springframework.boot + spring-boot-tools + 1.5.1.RELEASE + pom + +``` + +Please refer to the motan-demo module for detailed configuration of the server + +#### Client configuration + +1、Declare the configuration bean for Annotation, protocolConfig, and RegistryConfig. The server is configured similarly to the server。 + +2、Configure basicRefererConfig bean + +```java + @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、Add the @MotanReferer annotation to the object using the motan service, and the registration configuration is consistent with the xml way of the referer tag + +```java + @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、Using spring-boot to start service + +```java + @EnableAutoConfiguration + @SpringBootApplication + public class SpringBootRpcClientDemo { + + public static void main(String[] args) { + SpringApplication.run(SpringBootRpcClientDemo.class, args); + } + } +``` + +Please refer to the motan-demo module for detailed client configuration + +## Using OpenTracing + +Motan supports OpenTracing](http://opentracing.io) through the filter's SPI extension mechanism.Motan can support any trace implementation that implements the OpenTracing standard. The following steps are required to use OpenTracing. + +1、Introduced filter-opentracing extension + +```xml + + com.weibo + motan-protocol-yar + 0.3.0 + +``` + +2、If the third-party trace tool declares the io.opentracing.Tracer's SPI extension, you can directly introduce a third-party trace to the jar package. If the third party does not make a statement, turn to the third step. + +3、Customize a TracerFactory to implement TracerFactory interface, through getTracer () to get different tracer implementation. Set the tracerFactory of the OpenTracingContext to a custom TracerFactory。 + +You can refer to the filter-opentracing module src/test/java/com.weibo.api.motan.filter.opentracing.zipkin.demo to achieve server and client. \ No newline at end of file diff --git a/docs/wiki/en_userguide.md b/docs/wiki/en_userguide.md new file mode 100644 index 000000000..e69de29bb diff --git a/docs/wiki/zh_quickstart.md b/docs/wiki/zh_quickstart.md index 57264a17b..e526505b2 100644 --- a/docs/wiki/zh_quickstart.md +++ b/docs/wiki/zh_quickstart.md @@ -13,8 +13,8 @@ 快速入门中会给出一些基本使用场景下的配置方式,更详细的使用文档请参考[用户指南](zh_userguide). > 如果要执行快速入门介绍中的例子,你需要: -> * JDK 1.7或更高版本。 -> * java依赖管理工具,如[Maven][maven]或[Gradle][gradle]。 +> * JDK 1.7或更高版本。 +> * java依赖管理工具,如[Maven][maven]或[Gradle][gradle]。 ## 简单调用示例 @@ -26,19 +26,19 @@ com.weibo motan-core - 0.2.2 + 0.3.0 com.weibo motan-transport-netty - 0.2.2 + 0.3.0 com.weibo motan-springsupport - 0.2.2 + 0.3.0 org.springframework @@ -60,14 +60,14 @@ ``` 3. 编写业务接口逻辑、创建并启动RPC Server。 - + `src/main/java/quickstart/FooServiceImpl.java` ```java package quickstart; public class FooServiceImpl implements FooService { - + public String hello(String name) { System.out.println(name + " invoked rpc service"); return "hello " + name; @@ -76,7 +76,7 @@ ``` `src/main/resources/motan_server.xml` - + ```xml 异步调用 异步调用与同步调用基本配置完全一样,只需要在接口类中加上@MotanAsync注解,然后client端稍作修改。server端不需要做任何修改。具体步骤如下: @@ -157,7 +158,7 @@ ```java package quickstart; - + @MotanAsync public interface FooService { public String hello(String name); @@ -165,29 +166,33 @@ ``` 2. 编译时,Motan自动生成异步service类,生成路径为target/generated-sources/annotations/,生成的类名为service名加上Async,例如service类名为FooService.java,则自动生成的类名为FooServiceAsync.java。 -另外,需要将motan自动生产类文件的路径配置为项目source path,可以使用maven plugin或手动配置。pom.xml配置如下: - - - ```xml - - org.codehaus.mojo - build-helper-maven-plugin - 1.10 - - - generate-sources - - add-source - - - - ${project.build.directory}/generated-sources/annotations - - - - - - ``` + 另外,需要将motan自动生产类文件的路径配置为项目source path,可以使用maven plugin或手动配置。pom.xml配置如下: + + + ​```xml + + + + org.codehaus.mojo + build-helper-maven-plugin + 1.10 + + + generate-sources + + add-source + + + + ${project.build.directory}/generated-sources/annotations + + + + + + + + ​``` 3. 在client端配置motan_client.xml时,在同步调用配置的基础上,只需要修改referer的interface为Motan自动生成的接口类即可。 @@ -242,17 +247,17 @@ #### Consul安装与启动 ##### 安装([官方文档](https://www.consul.io/intro/getting-started/install.html)) - + # 这里以linux为例 wget https://releases.hashicorp.com/consul/0.6.4/consul_0.6.4_linux_amd64.zip unzip consul_0.6.4_linux_amd64.zip sudo mv consul /bin - + ##### 启动([官方文档](https://www.consul.io/intro/getting-started/agent.html)) 测试环境启动: consul agent -dev - + ui后台 [http://localhost:8500/ui](http://localhost:8500/ui) #### Motan-Consul配置 @@ -271,7 +276,7 @@ ui后台 [http://localhost:8500/ui](http://localhost:8500/ui) ```xml - ``` + ``` 3. 在Motan client及server配置改为通过registry服务发现。 @@ -327,17 +332,17 @@ ui后台 [http://localhost:8500/ui](http://localhost:8500/ui) 2. 在server和client的配置文件中分别增加zookeeper registry定义。 zookeeper为单节点 - + ```xml ``` - + zookeeper多节点集群 ```xml ``` - + 3. 在Motan client及server配置改为通过registry服务发现。 client @@ -352,9 +357,11 @@ ui后台 [http://localhost:8500/ui](http://localhost:8500/ui) ``` -4. server程序启动后,需要显式调用心跳开关,注册到zookeeper。 +4. server程序启动后,需要在server.java中显式调用心跳开关,注册到zookeeper。 ```java + ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:motan_server.xml"); + //if you want to use zookeeper or consul MotanSwitcherUtil.setSwitcherValue(MotanConstants.REGISTRY_HEARTBEAT_SWITCHER, true) ``` @@ -367,7 +374,7 @@ ui后台 [http://localhost:8500/ui](http://localhost:8500/ui) ## 其他调用示例 ###提供YAR协议服务 - + [YAR](https://github.com/laruence/yar)协议是php的一个rpc扩展,motan框架可以提供yar协议的RPC服务 1、引入motan-protocol-yar.jar @@ -378,7 +385,7 @@ ui后台 [http://localhost:8500/ui](http://localhost:8500/ui) 0.2.1 ``` - + 2、在服务接口类上增加注解@YarConfig,声明服务的uri ```java @@ -387,20 +394,20 @@ ui后台 [http://localhost:8500/ui](http://localhost:8500/ui) public String hello(String name); } ``` - + 3、配置protocol的name="yar" ```xml ``` - + 4、配置service的export,使用yar协议提供服务 - +​ ```xml ``` - + 具体配置见motan-demo模块 YAR协议使用[yar-java](https://github.com/weibocom/yar-java)进行解析,java作为YAR client时可以直接使用 @@ -450,7 +457,7 @@ YAR协议使用[yar-java](https://github.com/weibocom/yar-java)进行解析,ja return config; } ``` - + 3、service的实现类上添加@MotanService注解,注解的配置参数与xml配置方式的service标签一致。 ```java @@ -463,7 +470,7 @@ YAR协议使用[yar-java](https://github.com/weibocom/yar-java)进行解析,ja } } ``` - + 4、使用[spring-boot](https://github.com/spring-projects/spring-boot)启动服务 ```java @@ -480,7 +487,20 @@ YAR协议使用[yar-java](https://github.com/weibocom/yar-java)进行解析,ja } } ``` - + +需要在pom中添加依赖 + +```xml + + org.springframework.boot + spring-boot-tools + 1.5.1.RELEASE + pom + +``` + + + server端详细配置请参考motan-demo模块 ####client端配置 @@ -504,7 +524,7 @@ server端详细配置请参考motan-demo模块 return config; } ``` - + 3、在使用motan service 的对象上添加@MotanReferer注解,注册配置与xml方式的referer标签一致 ```java @@ -522,7 +542,7 @@ server端详细配置请参考motan-demo模块 } } ``` - + 4、使用spring-boot启动client ```java @@ -535,7 +555,7 @@ server端详细配置请参考motan-demo模块 } } ``` - + client端详细配置请参考motan-demo模块 ## 使用OpenTracing From 4a05c51be85584a12af6f47cdf1e919e713b214f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B9=9B=E7=84=B6?= Date: Wed, 26 Apr 2017 14:21:17 +0800 Subject: [PATCH 2/2] fix basicService configure bug --- docs/wiki/en_quickstart.md | 268 ++---------------- docs/wiki/zh_quickstart.md | 140 ++++----- .../weibo/api/motan/config/ServiceConfig.java | 16 +- .../config/springsupport/AnnotationBean.java | 2 +- .../springsupport/ServiceConfigBean.java | 22 +- 5 files changed, 108 insertions(+), 340 deletions(-) diff --git a/docs/wiki/en_quickstart.md b/docs/wiki/en_quickstart.md index a5a6d823d..e648a9fc1 100644 --- a/docs/wiki/en_quickstart.md +++ b/docs/wiki/en_quickstart.md @@ -7,8 +7,8 @@ The quick start gives very basic example of running server and client on the same machine. For more details about using and developing Motan, please jump to [Documents](en_userguide). > The minimum requirements to run the quick start are: -> * JDK 1.7 or above. -> * A java-based project management software like [Maven][maven] or [Gradle][gradle]. +> * JDK 1.7 or above. +> * A java-based project management software like [Maven][maven] or [Gradle][gradle]. ## Using Motan in single machine environment @@ -19,19 +19,19 @@ The quick start gives very basic example of running server and client on the sam com.weibo motan-core - 0.3.0 + 0.1.1 com.weibo motan-transport-netty - 0.3.0 + 0.1.1 com.weibo motan-springsupport - 0.3.0 + 0.1.1 org.springframework @@ -53,9 +53,9 @@ The quick start gives very basic example of running server and client on the sam ``` 3. Write an implementation, create and start RPC Server. - + `src/main/java/quickstart/FooServiceImpl.java` - + ```java package quickstart; @@ -69,7 +69,7 @@ The quick start gives very basic example of running server and client on the sam ``` `src/main/resources/motan_server.xml` - + ```xml ``` - + `src/main/java/quickstart/Server.java` - + ```java package quickstart; - + import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; - + public class Server { - + public static void main(String[] args) throws InterruptedException { ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:motan_server.xml"); System.out.println("server start..."); } } ``` - + Execute main function in Server will start a Motan server listening on port 8002. 4. Create and start RPC Client. @@ -128,21 +128,20 @@ The quick start gives very basic example of running server and client on the sam import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; - ``` public class Client { - + public static void main(String[] args) throws InterruptedException { ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:motan_client.xml"); FooService service = (FooService) ctx.getBean("remoteService"); System.out.println(service.hello("motan")); } } - ​``` + ``` Execute main function in Client will invoke the remote service and print response. - + ## Using Motan in cluster environment In cluster environment, the external service discovery components such as Consul or ZooKeeper is needed to support the use of Motan. @@ -153,17 +152,17 @@ In cluster environment, the external service discovery components such as Consul #### Install and Start Consul ##### Install([Official Document](https://www.consul.io/intro/getting-started/install.html)) - + # Taking Linux as an example wget https://releases.hashicorp.com/consul/0.6.4/consul_0.6.4_linux_amd64.zip unzip consul_0.6.4_linux_amd64.zip sudo mv consul /bin - + ##### Start([Official Document](https://www.consul.io/intro/getting-started/agent.html)) Starting the test environment: consul agent -dev - + UI backend [http://localhost:8500/ui](http://localhost:8500/ui) #### Motan-Consul configuration @@ -182,7 +181,7 @@ UI backend [http://localhost:8500/ui](http://localhost:8500/ui) ```xml - ``` + ``` 3. Change the way of service discovery to registry in the configuration of server and client. @@ -191,19 +190,17 @@ UI backend [http://localhost:8500/ui](http://localhost:8500/ui) ```xml ``` - + client: ```xml ``` -4. After the server starts, you SHOULD call heartbeat switcher explicitly in order to start heartbeat for Consul in server.java. +4. After the server starts, you SHOULD call heartbeat switcher explicitly in order to start heartbeat for Consul. ```java - ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:motan_server.xml"); - //if you want to use zookeeper or consul - MotanSwitcherUtil.setSwitcherValue(MotanConstants.REGISTRY_HEARTBEAT_SWITCHER, true); + MotanSwitcherUtil.setSwitcherValue(MotanConstants.REGISTRY_HEARTBEAT_SWITCHER, true) ``` 5. Go to [UI backend](http://localhost:8500/ui). Verify whether the service is normal. @@ -240,17 +237,17 @@ Install and start ZooKeeper: 2. Add the definition of ZooKeeper registry in the configuration of server and client. single node ZooKeeper: - + ```xml ``` - + multi-nodes ZooKeeper: ```xml ``` - + 3. Change the way of service discovery to registry in the configuration of server and client. server: @@ -258,7 +255,7 @@ Install and start ZooKeeper: ```xml ``` - + client: ```xml @@ -277,212 +274,3 @@ Install and start ZooKeeper: [maven]:https://maven.apache.org [gradle]:http://gradle.org -[maven]: https://maven.apache.org -[gradle]: http://gradle.org - -## Other call examples - -### Provide YAR protocol services - -[YAR](https://github.com/laruence/yar) protocol is a RPC extension of php, the motan framework can provide yar protocol for RPC services -1、include motan-protocol-yar.jar - -```xml - - com.weibo - motan-protocol-yar - 0.3.0 - -``` - -2、Add annotations @YarConfig to the service interface class to declare the uri of the service - -```java - @YarConfig(path = "/openapi/yarserver/test") - public interface YarService { - public String hello(String name); - } -``` - -3、Config protocol name="yar" - -```xml - -``` - -4、Configure the export of service, using yar protocol to provide services - -```xml - -``` - -To get specific configuration,check motan-demo module. -We use [yar-java](https://github.com/weibocom/yar-java) to parse YAR protocol,java can be used directly as YAR client - -### Use the annotations to configure the motan - -#### Server configuration - -1、Declare Annotation to specify the packages to be parsed - -```java - @Bean - public AnnotationBean motanAnnotationBean() { - AnnotationBean motanAnnotationBean = new AnnotationBean(); - motanAnnotationBean.setPackage("com.weibo.motan.demo.server"); - return motanAnnotationBean; - } -``` - -2、Configure ProtocolConfig、RegistryConfig、BasicServiceConfig's bean Object,which function is consistent with the protocol, registry, and basicService tags in the xml configuration. - -```java - @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、Add the @MotanService annotation to the implementation class of the service. The configuration parameters of the annotation are consistent with the service tag of the xml configuration。 - -```java - @MotanService(export = "demoMotan:8002") - public class MotanDemoServiceImpl implements MotanDemoService { - - public String hello(String name) { - System.out.println(name); - return "Hello " + name + "!"; - } - } -``` - -4、Start service using [spring-boot](https://github.com/spring-projects/spring-boot) - -```java - @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..."); - } - } -``` - -we need to add dependencies in pom - -```xml - - org.springframework.boot - spring-boot-tools - 1.5.1.RELEASE - pom - -``` - -Please refer to the motan-demo module for detailed configuration of the server - -#### Client configuration - -1、Declare the configuration bean for Annotation, protocolConfig, and RegistryConfig. The server is configured similarly to the server。 - -2、Configure basicRefererConfig bean - -```java - @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、Add the @MotanReferer annotation to the object using the motan service, and the registration configuration is consistent with the xml way of the referer tag - -```java - @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、Using spring-boot to start service - -```java - @EnableAutoConfiguration - @SpringBootApplication - public class SpringBootRpcClientDemo { - - public static void main(String[] args) { - SpringApplication.run(SpringBootRpcClientDemo.class, args); - } - } -``` - -Please refer to the motan-demo module for detailed client configuration - -## Using OpenTracing - -Motan supports OpenTracing](http://opentracing.io) through the filter's SPI extension mechanism.Motan can support any trace implementation that implements the OpenTracing standard. The following steps are required to use OpenTracing. - -1、Introduced filter-opentracing extension - -```xml - - com.weibo - motan-protocol-yar - 0.3.0 - -``` - -2、If the third-party trace tool declares the io.opentracing.Tracer's SPI extension, you can directly introduce a third-party trace to the jar package. If the third party does not make a statement, turn to the third step. - -3、Customize a TracerFactory to implement TracerFactory interface, through getTracer () to get different tracer implementation. Set the tracerFactory of the OpenTracingContext to a custom TracerFactory。 - -You can refer to the filter-opentracing module src/test/java/com.weibo.api.motan.filter.opentracing.zipkin.demo to achieve server and client. \ No newline at end of file diff --git a/docs/wiki/zh_quickstart.md b/docs/wiki/zh_quickstart.md index e526505b2..57264a17b 100644 --- a/docs/wiki/zh_quickstart.md +++ b/docs/wiki/zh_quickstart.md @@ -13,8 +13,8 @@ 快速入门中会给出一些基本使用场景下的配置方式,更详细的使用文档请参考[用户指南](zh_userguide). > 如果要执行快速入门介绍中的例子,你需要: -> * JDK 1.7或更高版本。 -> * java依赖管理工具,如[Maven][maven]或[Gradle][gradle]。 +> * JDK 1.7或更高版本。 +> * java依赖管理工具,如[Maven][maven]或[Gradle][gradle]。 ## 简单调用示例 @@ -26,19 +26,19 @@ com.weibo motan-core - 0.3.0 + 0.2.2 com.weibo motan-transport-netty - 0.3.0 + 0.2.2 com.weibo motan-springsupport - 0.3.0 + 0.2.2 org.springframework @@ -60,14 +60,14 @@ ``` 3. 编写业务接口逻辑、创建并启动RPC Server。 - + `src/main/java/quickstart/FooServiceImpl.java` ```java package quickstart; public class FooServiceImpl implements FooService { - + public String hello(String name) { System.out.println(name + " invoked rpc service"); return "hello " + name; @@ -76,7 +76,7 @@ ``` `src/main/resources/motan_server.xml` - + ```xml 异步调用 异步调用与同步调用基本配置完全一样,只需要在接口类中加上@MotanAsync注解,然后client端稍作修改。server端不需要做任何修改。具体步骤如下: @@ -158,7 +157,7 @@ ```java package quickstart; - + @MotanAsync public interface FooService { public String hello(String name); @@ -166,33 +165,29 @@ ``` 2. 编译时,Motan自动生成异步service类,生成路径为target/generated-sources/annotations/,生成的类名为service名加上Async,例如service类名为FooService.java,则自动生成的类名为FooServiceAsync.java。 - 另外,需要将motan自动生产类文件的路径配置为项目source path,可以使用maven plugin或手动配置。pom.xml配置如下: - - - ​```xml - - - - org.codehaus.mojo - build-helper-maven-plugin - 1.10 - - - generate-sources - - add-source - - - - ${project.build.directory}/generated-sources/annotations - - - - - - - - ​``` +另外,需要将motan自动生产类文件的路径配置为项目source path,可以使用maven plugin或手动配置。pom.xml配置如下: + + + ```xml + + org.codehaus.mojo + build-helper-maven-plugin + 1.10 + + + generate-sources + + add-source + + + + ${project.build.directory}/generated-sources/annotations + + + + + + ``` 3. 在client端配置motan_client.xml时,在同步调用配置的基础上,只需要修改referer的interface为Motan自动生成的接口类即可。 @@ -247,17 +242,17 @@ #### Consul安装与启动 ##### 安装([官方文档](https://www.consul.io/intro/getting-started/install.html)) - + # 这里以linux为例 wget https://releases.hashicorp.com/consul/0.6.4/consul_0.6.4_linux_amd64.zip unzip consul_0.6.4_linux_amd64.zip sudo mv consul /bin - + ##### 启动([官方文档](https://www.consul.io/intro/getting-started/agent.html)) 测试环境启动: consul agent -dev - + ui后台 [http://localhost:8500/ui](http://localhost:8500/ui) #### Motan-Consul配置 @@ -276,7 +271,7 @@ ui后台 [http://localhost:8500/ui](http://localhost:8500/ui) ```xml - ``` + ``` 3. 在Motan client及server配置改为通过registry服务发现。 @@ -332,17 +327,17 @@ ui后台 [http://localhost:8500/ui](http://localhost:8500/ui) 2. 在server和client的配置文件中分别增加zookeeper registry定义。 zookeeper为单节点 - + ```xml ``` - + zookeeper多节点集群 ```xml ``` - + 3. 在Motan client及server配置改为通过registry服务发现。 client @@ -357,11 +352,9 @@ ui后台 [http://localhost:8500/ui](http://localhost:8500/ui) ``` -4. server程序启动后,需要在server.java中显式调用心跳开关,注册到zookeeper。 +4. server程序启动后,需要显式调用心跳开关,注册到zookeeper。 ```java - ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:motan_server.xml"); - //if you want to use zookeeper or consul MotanSwitcherUtil.setSwitcherValue(MotanConstants.REGISTRY_HEARTBEAT_SWITCHER, true) ``` @@ -374,7 +367,7 @@ ui后台 [http://localhost:8500/ui](http://localhost:8500/ui) ## 其他调用示例 ###提供YAR协议服务 - + [YAR](https://github.com/laruence/yar)协议是php的一个rpc扩展,motan框架可以提供yar协议的RPC服务 1、引入motan-protocol-yar.jar @@ -385,7 +378,7 @@ ui后台 [http://localhost:8500/ui](http://localhost:8500/ui) 0.2.1 ``` - + 2、在服务接口类上增加注解@YarConfig,声明服务的uri ```java @@ -394,20 +387,20 @@ ui后台 [http://localhost:8500/ui](http://localhost:8500/ui) public String hello(String name); } ``` - + 3、配置protocol的name="yar" ```xml ``` - + 4、配置service的export,使用yar协议提供服务 -​ + ```xml ``` - + 具体配置见motan-demo模块 YAR协议使用[yar-java](https://github.com/weibocom/yar-java)进行解析,java作为YAR client时可以直接使用 @@ -457,7 +450,7 @@ YAR协议使用[yar-java](https://github.com/weibocom/yar-java)进行解析,ja return config; } ``` - + 3、service的实现类上添加@MotanService注解,注解的配置参数与xml配置方式的service标签一致。 ```java @@ -470,7 +463,7 @@ YAR协议使用[yar-java](https://github.com/weibocom/yar-java)进行解析,ja } } ``` - + 4、使用[spring-boot](https://github.com/spring-projects/spring-boot)启动服务 ```java @@ -487,20 +480,7 @@ YAR协议使用[yar-java](https://github.com/weibocom/yar-java)进行解析,ja } } ``` - -需要在pom中添加依赖 - -```xml - - org.springframework.boot - spring-boot-tools - 1.5.1.RELEASE - pom - -``` - - - + server端详细配置请参考motan-demo模块 ####client端配置 @@ -524,7 +504,7 @@ server端详细配置请参考motan-demo模块 return config; } ``` - + 3、在使用motan service 的对象上添加@MotanReferer注解,注册配置与xml方式的referer标签一致 ```java @@ -542,7 +522,7 @@ server端详细配置请参考motan-demo模块 } } ``` - + 4、使用spring-boot启动client ```java @@ -555,7 +535,7 @@ server端详细配置请参考motan-demo模块 } } ``` - + client端详细配置请参考motan-demo模块 ## 使用OpenTracing diff --git a/motan-core/src/main/java/com/weibo/api/motan/config/ServiceConfig.java b/motan-core/src/main/java/com/weibo/api/motan/config/ServiceConfig.java index 342d3e98f..e2dc5d5f2 100644 --- a/motan-core/src/main/java/com/weibo/api/motan/config/ServiceConfig.java +++ b/motan-core/src/main/java/com/weibo/api/motan/config/ServiceConfig.java @@ -54,7 +54,7 @@ public class ServiceConfig extends AbstractServiceConfig { // service 对应的exporters,用于管理service服务的生命周期 private List> exporters = new CopyOnWriteArrayList>(); private Class interfaceClass; - private BasicServiceInterfaceConfig basicServiceConfig; + private BasicServiceInterfaceConfig basicService; private AtomicBoolean exported = new AtomicBoolean(false); // service的用于注册的url,用于管理service注册的生命周期,url为regitry url,内部嵌套service url。 private ConcurrentHashSet registereUrls = new ConcurrentHashSet(); @@ -153,8 +153,8 @@ private void doExport(ProtocolConfig protocolConfig, int port, List registr } String hostAddress = host; - if (StringUtils.isBlank(hostAddress) && basicServiceConfig != null) { - hostAddress = basicServiceConfig.getHost(); + if (StringUtils.isBlank(hostAddress) && basicService != null) { + hostAddress = basicService.getHost(); } if (NetUtils.isInvalidLocalHost(hostAddress)) { hostAddress = getLocalHostAddress(registryURLs); @@ -165,7 +165,7 @@ private void doExport(ProtocolConfig protocolConfig, int port, List registr map.put(URLParamType.nodeType.getName(), MotanConstants.NODE_TYPE_SERVICE); map.put(URLParamType.refreshTimestamp.getName(), String.valueOf(System.currentTimeMillis())); - collectConfigParams(map, protocolConfig, basicServiceConfig, extConfig, this); + collectConfigParams(map, protocolConfig, basicService, extConfig, this); collectMethodConfigParams(map, this.getMethods()); URL serviceUrl = new URL(protocolName, hostAddress, port, interfaceClass.getName(), map); @@ -231,12 +231,12 @@ private void afterUnexport() { } @ConfigDesc(excluded = true) - public BasicServiceInterfaceConfig getBasicServiceConfig() { - return basicServiceConfig; + public BasicServiceInterfaceConfig getBasicService() { + return basicService; } - public void setBasicServiceConfig(BasicServiceInterfaceConfig basicServiceConfig) { - this.basicServiceConfig = basicServiceConfig; + public void setBasicService(BasicServiceInterfaceConfig basicService) { + this.basicService = basicService; } public Map getProtocolAndPort() { diff --git a/motan-springsupport/src/main/java/com/weibo/api/motan/config/springsupport/AnnotationBean.java b/motan-springsupport/src/main/java/com/weibo/api/motan/config/springsupport/AnnotationBean.java index ea778bfab..fa5b8dba4 100644 --- a/motan-springsupport/src/main/java/com/weibo/api/motan/config/springsupport/AnnotationBean.java +++ b/motan-springsupport/src/main/java/com/weibo/api/motan/config/springsupport/AnnotationBean.java @@ -202,7 +202,7 @@ public Object postProcessAfterInitialization(Object bean, String beanName) throw serviceConfig.setBeanFactory(beanFactory); if (service.basicService() != null && service.basicService().length() > 0) { - serviceConfig.setBasicServiceConfig(beanFactory.getBean(service.basicService(), BasicServiceInterfaceConfig.class)); + serviceConfig.setBasicService(beanFactory.getBean(service.basicService(), BasicServiceInterfaceConfig.class)); } if (service.export() != null && service.export().length() > 0) { diff --git a/motan-springsupport/src/main/java/com/weibo/api/motan/config/springsupport/ServiceConfigBean.java b/motan-springsupport/src/main/java/com/weibo/api/motan/config/springsupport/ServiceConfigBean.java index a1ce09259..82f50fe4d 100644 --- a/motan-springsupport/src/main/java/com/weibo/api/motan/config/springsupport/ServiceConfigBean.java +++ b/motan-springsupport/src/main/java/com/weibo/api/motan/config/springsupport/ServiceConfigBean.java @@ -100,7 +100,7 @@ public void onApplicationEvent(ContextRefreshedEvent event) { * 检查并配置basicConfig */ private void checkAndConfigBasicConfig() { - if (getBasicServiceConfig() == null) { + if (getBasicService() == null) { if (MotanNamespaceHandler.basicServiceConfigDefineNames.size() == 0) { if (beanFactory instanceof ListableBeanFactory) { ListableBeanFactory listableBeanFactory = (ListableBeanFactory) beanFactory; @@ -116,9 +116,9 @@ private void checkAndConfigBasicConfig() { continue; } if (MotanNamespaceHandler.basicServiceConfigDefineNames.size() == 1) { - setBasicServiceConfig(biConfig); + setBasicService(biConfig); } else if (biConfig.isDefault() != null && biConfig.isDefault().booleanValue()) { - setBasicServiceConfig(biConfig); + setBasicService(biConfig); } } } @@ -128,11 +128,11 @@ private void checkAndConfigBasicConfig() { * 检查是否已经装配export,如果没有则到basicConfig查找 */ private void checkAndConfigExport() { - if (StringUtils.isBlank(getExport()) && getBasicServiceConfig() != null - && !StringUtils.isBlank(getBasicServiceConfig().getExport())) { - setExport(getBasicServiceConfig().getExport()); - if (getBasicServiceConfig().getProtocols() != null) { - setProtocols(new ArrayList(getBasicServiceConfig().getProtocols())); + if (StringUtils.isBlank(getExport()) && getBasicService() != null + && !StringUtils.isBlank(getBasicService().getExport())) { + setExport(getBasicService().getExport()); + if (getBasicService().getProtocols() != null) { + setProtocols(new ArrayList(getBasicService().getProtocols())); } } @@ -169,9 +169,9 @@ private void checkAndConfigExport() { * 检查并配置registry */ private void checkAndConfigRegistry() { - if (CollectionUtil.isEmpty(getRegistries()) && getBasicServiceConfig() != null - && !CollectionUtil.isEmpty(getBasicServiceConfig().getRegistries())) { - setRegistries(getBasicServiceConfig().getRegistries()); + if (CollectionUtil.isEmpty(getRegistries()) && getBasicService() != null + && !CollectionUtil.isEmpty(getBasicService().getRegistries())) { + setRegistries(getBasicService().getRegistries()); } if (CollectionUtil.isEmpty(getRegistries())) { for (String name : MotanNamespaceHandler.registryDefineNames) {