From e3ce50b0eed0c1e1738372e410aa3d0a4c29f1cc Mon Sep 17 00:00:00 2001 From: wenqisun Date: Fri, 22 Apr 2016 17:24:27 +0800 Subject: [PATCH] English Quickstart --- docs/wiki/en_quickstart.md | 263 +++++++++++++++++++++++++++++++++++++ 1 file changed, 263 insertions(+) create mode 100644 docs/wiki/en_quickstart.md diff --git a/docs/wiki/en_quickstart.md b/docs/wiki/en_quickstart.md new file mode 100644 index 000000000..285e2db0b --- /dev/null +++ b/docs/wiki/en_quickstart.md @@ -0,0 +1,263 @@ +- [Quickstart](#) + - [Using Motan in single machine environment](#Using Motan in single machine environment) + - [Using Motan in cluster environment](#Using Motan in cluster environment) + - [Using Consul as the registry](#Using Consul as the registry) + - [Using ZooKeeper as the registry](#Using ZooKeeper as the registry) + +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.6 or above. +> * A java-based project management software like [Maven][maven] or [Gradle][gradle]. + +## Using Motan in single machine environment + +1. Add dependency to pom. + + ```xml + + com.weibo + motan-core + 0.0.1 + + + com.weibo + motan-transport-netty + 0.0.1 + + + + + com.weibo + motan-springsupport + 0.0.1 + + + org.springframework + spring-context + 4.2.4.RELEASE + + ``` + +2. Create an interface for both service provider and consumer. + + `src/main/java/quickstart/FooService.java` + + ```java + package quickstart; + + public interface FooService { + public String hello(String name); + } + ``` + +3. Implement the service provider. + + + `src/main/java/quickstart/FooServiceImpl.java` + + ```java + package quickstart; + + import org.springframework.context.ApplicationContext; + import org.springframework.context.support.ClassPathXmlApplicationContext; + + public class FooServiceImpl implements FooService { + + public String hello(String name) { + System.out.println(name + " invoked rpc service"); + return "hello " + name; + } + + public static void main(String[] args) throws InterruptedException { + ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:motan_server.xml"); + System.out.println("server start..."); + } + } + ``` + + `src/main/resources/motan_server.xml` + + ```xml + + + + + + + + + ``` + + Executing main function in FooServiceImpl will start a motan server listening on port 8002. + +4. Implement the service consumer. + + `src/main/resources/motan_client.xml` + + ```xml + + + + + + + ``` + + `src/main/java/quickstart/Client.java` + + ```java + package quickstart; + + 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")); + } + } + ``` + + Executing 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. + + +### Using Consul as the registry + +#### 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 + +1. Add motan-registry-consul in the pom of server and client. + + ```xml + + com.weibo + motan-registry-consul + 0.0.1 + + ``` + +2. Add the definition of consul registry in the configuration of server and client. + + ```xml + + ``` + +3. Change the way of service discovery to registry in the configuration of server and client. + + server: + + ```xml + + ``` + + client: + + ```xml + + ``` + + +4. After the server starts, you need to call the hearbeat switch explicitly in order to register in Consul. + + ```java + MotanSwitcherUtil.setSwitcher(ConsulConstants.NAMING_PROCESS_HEARTBEAT_SWITCHER, true) + ``` + +5. Go to [UI backend](http://localhost:8500/ui). Verify whether the service is normal. + +6. Start client, call service. + +### Using ZooKeeper as the registry + +#### Install and Start ZooKeeper([Official Document](https://zookeeper.apache.org/doc/trunk/zookeeperStarted.html)) + +Install and start ZooKeeper: + + wget http://mirrors.cnnic.cn/apache/zookeeper/zookeeper-3.4.8/zookeeper-3.4.8.tar.gz + tar zxvf zookeeper-3.4.8.tar.gz + + cd zookeeper-3.4.8/conf/ + cp zoo_sample.cfg zoo.cfg + + cd ../ + sh bin/zkServer.sh start + +#### Motan-ZooKeeper configuration + +1. Add motan-registry-zookeeper in the pom of server and client. + + ```xml + + com.weibo + motan-registry-zookeeper + 0.0.1 + + ``` + +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: + + ```xml + + ``` + + client: + + ```xml + + ``` + +4. Start client, call service. + + +[maven]:https://maven.apache.org +[gradle]:http://gradle.org +