1. 基础知识
1.1 什么是RPC
远程过程调用,简单的理解是一个节点请求另一个节点提供的服务
2. dubbo配置
2.1 安装ZooKeeper(Windows10环境)
1 2 3 4 5 6 7 8 9 10 11 12 13
| $ cd bin $ zkServer.cmd ... some info ...
$ cd bin $ ./zkServer.sh start ... some info ...
|
- zookeeper会占用8080端口,通过在
zoo.cfg
中添加 admin.serverPort=8888
解除8080端口的占用
2.2 安装监控中心(可选)
1 2 3
| $ git clonegit clone https://github.com/apache/dubbo-admin.git $ cd dubbo-admin $ mvn clean package
|
1 2 3
| $ cd dubbo-admin-ui npm install npm run dev
|
1 2 3 4
| $ cd dubbo-admin-server $ mvn clean package $ cd target $ java -jar dubbo-admin-server-0.3.0-SNAPSHOT.jar
|
2.3 Spring Demo
2.3.1 Provider
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| <?xml version="1.0" encoding="UTF-8"?> <project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion>
<groupId>indi.pancras</groupId> <artifactId>provider</artifactId> <version>1.0-SNAPSHOT</version>
<properties> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties>
<dependencies> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo</artifactId> <version>2.7.3</version> </dependency> <dependency> <groupId>org.apache.curator</groupId> <artifactId>curator-framework</artifactId> <version>2.12.0</version> </dependency> <dependency> <groupId>org.apache.curator</groupId> <artifactId>curator-recipes</artifactId> <version>2.12.0</version> </dependency> </dependencies>
</project>
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://dubbo.apache.org/schema/dubbo" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
<dubbo:application name="provider"/>
<dubbo:registry address="zookeeper://localhost:2181"/>
<dubbo:protocol name="dubbo" port="31111"/>
<dubbo:service interface="provider.interfaces.ComputationalStorageService" ref="css"/> <bean id="css" class="provider.interfaces.impl.ComputationalStorageServiceImpl"/> </beans>
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| package provider;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.io.IOException;
import provider.interfaces.ComputationalStorageService;
public class MainApplication { public static void main(String[] args) throws IOException { ClassPathXmlApplicationContext ioc = new ClassPathXmlApplicationContext("provider.xml"); ioc.start(); ComputationalStorageService bean = ioc.getBean(ComputationalStorageService.class); System.out.println(bean); System.in.read(); } }
|
- ComputationalStorageService.java
1 2 3 4 5 6 7 8 9 10 11
| package provider.interfaces;
public interface ComputationalStorageService { public String sayHello(String msg);
public Integer wordCount(String filename); }
|
- ComputationalStorageServiceImpl.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| package provider.interfaces.impl;
import provider.interfaces.ComputationalStorageService;
public class ComputationalStorageServiceImpl implements ComputationalStorageService { public String sayHello(String msg) { return "Hello: " + msg; }
@Override public Integer wordCount(String filename) { return null; } }
|
2.3.2 Consumer
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| <?xml version="1.0" encoding="UTF-8"?> <project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion>
<groupId>indi.pancras</groupId> <artifactId>consumer</artifactId> <version>1.0-SNAPSHOT</version>
<properties> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties>
<dependencies> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo</artifactId> <version>2.7.3</version> </dependency> <dependency> <groupId>org.apache.curator</groupId> <artifactId>curator-framework</artifactId> <version>2.12.0</version> </dependency> <dependency> <groupId>org.apache.curator</groupId> <artifactId>curator-recipes</artifactId> <version>2.12.0</version> </dependency> </dependencies>
</project>
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://dubbo.apache.org/schema/dubbo" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
<dubbo:application name="consumer"/>
<dubbo:registry address="zookeeper://localhost:2181"/>
<dubbo:reference interface="provider.interfaces.ComputationalStorageService" id="demoService" timeout="5000" retries="3" version="*">
</dubbo:reference>
</beans>
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| package consumer;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.io.IOException;
import provider.interfaces.ComputationalStorageService;
public class MainApplication { public static void main(String[] args) throws IOException { ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("consumer.xml");
ComputationalStorageService bean = applicationContext.getBean(ComputationalStorageService.class); String hello = bean.sayHello("Mike"); System.out.println(hello); } }
|
- ComputationalStorageService.java
1 2 3 4 5 6 7 8 9 10 11
| package provider.interfaces;
public interface ComputationalStorageService { public String sayHello(String msg);
public Integer wordCount(String filename); }
|