概述
初次接触 Dubbo
, 先跑个 Hello World
, 看看dubbo微服务究竟是如何运作的。下边是快速启动的完整项目。
首先放出工程结构, 方便大家构建demo
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 35 36 37 38 39 40 41 42 43 44
| ➜ dubbo-demo tree . ├── dubbo-api │ ├── pom.xml │ └── src │ ├── main │ │ ├── java │ │ │ └── cn │ │ │ └── idea360 │ │ │ └── api │ │ │ └── DemoService.java │ │ └── resources │ └── test │ └── java ├── dubbo-consumer │ ├── pom.xml │ └── src │ ├── main │ │ ├── java │ │ │ └── cn │ │ │ └── idea360 │ │ │ └── consumer │ │ │ └── Consumer.java │ │ └── resources │ │ └── dubbo │ │ └── consumer.xml │ └── test │ └── java ├── dubbo-provider │ ├── pom.xml │ └── src │ ├── main │ │ ├── java │ │ │ └── cn │ │ │ └── idea360 │ │ │ └── provider │ │ │ ├── DemoServiceImpl.java │ │ │ └── Provider.java │ │ └── resources │ │ └── dubbo │ │ └── provider.xml │ └── test │ └── java └── pom.xml
|
服务接口
首先创建一个 dubbo-api
服务,用来暴露服务接口
服务接口maven
pom.xml
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
| <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>dubbo-demo</artifactId> <groupId>cn.idea360</groupId> <version>1.0</version> </parent> <modelVersion>4.0.0</modelVersion>
<artifactId>dubbo-api</artifactId>
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <source>1.8</source> <target>1.8</target> <encoding>utf-8</encoding> </configuration> </plugin> </plugins> </build> </project>
|
服务接口定义
DemoService.java
1 2 3 4
| public interface DemoService {
String sayHello(String name); }
|
服务提供者
服务提供方maven
创建 dubbo-provider
服务
pom.xml
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 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
| <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>dubbo-demo</artifactId> <groupId>cn.idea360</groupId> <version>1.0</version> </parent> <modelVersion>4.0.0</modelVersion>
<artifactId>dubbo-provider</artifactId>
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <jdk.compile.source.version>1.8</jdk.compile.source.version> <jdk.compile.target.version>1.8</jdk.compile.target.version> <springframe.version>5.1.5.RELEASE</springframe.version> <junit.version>4.12</junit.version> <log4j.version>1.2.17</log4j.version> <dubbo.version>2.5.3</dubbo.version> <zookeeper.version>3.4.6</zookeeper.version> <zkclient.version>0.1</zkclient.version> </properties>
<dependencies> <dependency> <groupId>cn.idea360</groupId> <artifactId>dubbo-api</artifactId> <version>1.0</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${springframe.version}</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>${junit.version}</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>${log4j.version}</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>${dubbo.version}</version> </dependency> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>${zookeeper.version}</version> </dependency> <dependency> <groupId>com.github.sgroschupf</groupId> <artifactId>zkclient</artifactId> <version>${zkclient.version}</version> </dependency> </dependencies>
<build> <finalName>Provider</finalName>
<plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <source>1.8</source> <target>1.8</target> <encoding>utf-8</encoding> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>3.1.0</version> <configuration> <archive> <manifest> <mainClass>cn.idea360.provider.Provider</mainClass> </manifest> </archive> </configuration> </plugin> </plugins> </build>
</project>
|
在服务提供方实现接口
DemoServiceImpl.java
1 2 3 4 5 6 7
| public class DemoServiceImpl implements DemoService {
@Override public String sayHello(String name) { return "Hello " + name; } }
|
用 Spring 配置声明暴露服务
dubbo/provider.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| <?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://code.alibabatech.com/schema/dubbo" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<dubbo:application name="dubbo-provider" />
<dubbo:registry address="zookeeper://127.0.0.1:2181" client="zkclient" />
<dubbo:protocol name="dubbo" port="20880" />
<dubbo:service interface="cn.idea360.api.DemoService" ref="demoService" />
<bean id="demoService" class="cn.idea360.provider.DemoServiceImpl" /> </beans>
|
加载 Spring 配置
Provider.java:
1 2 3 4 5 6 7 8 9 10 11 12 13
| package cn.idea360.provider;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Provider { public static void main(String[] args) throws Exception { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"dubbo/provider.xml"}); context.start();
System.out.println("服务已经启动..."); System.in.read(); } }
|
服务消费者
服务消费者maven
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 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
| <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>dubbo-demo</artifactId> <groupId>cn.idea360</groupId> <version>1.0</version> </parent> <modelVersion>4.0.0</modelVersion>
<artifactId>dubbo-consumer</artifactId>
<packaging>jar</packaging>
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <jdk.compile.source.version>1.8</jdk.compile.source.version> <jdk.compile.target.version>1.8</jdk.compile.target.version> <springframe.version>5.1.5.RELEASE</springframe.version> <junit.version>4.12</junit.version> <log4j.version>1.2.17</log4j.version> <dubbo.version>2.5.3</dubbo.version> <zookeeper.version>3.4.6</zookeeper.version> <zkclient.version>0.1</zkclient.version> </properties>
<dependencies> <dependency> <groupId>cn.idea360</groupId> <artifactId>dubbo-api</artifactId> <version>1.0</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${springframe.version}</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>${junit.version}</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>${log4j.version}</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>${dubbo.version}</version> </dependency> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>${zookeeper.version}</version> </dependency> <dependency> <groupId>com.github.sgroschupf</groupId> <artifactId>zkclient</artifactId> <version>${zkclient.version}</version> </dependency> </dependencies>
<build> <finalName>Consumer</finalName> </build>
</project>
|
通过 Spring 配置引用远程服务
dubbo/consumer.xml:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| <?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://code.alibabatech.com/schema/dubbo" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<dubbo:application name="dubbo-consumer" />
<dubbo:registry address="zookeeper://127.0.0.1:2181" client="zkclient" />
<dubbo:reference id="demoService" interface="cn.idea360.api.DemoService" />
<dubbo:reference id="ipConsumerService" interface="cn.idea360.api.DemoService" url="dubbo://localhost:20880" />
</beans>
|
加载Spring配置,并调用远程服务
Consumer.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 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
| package cn.idea360.consumer;
import cn.idea360.api.DemoService; import com.alibaba.dubbo.config.ApplicationConfig; import com.alibaba.dubbo.config.ReferenceConfig; import com.alibaba.dubbo.config.RegistryConfig; import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Consumer {
public static void main(String[] args) throws Exception { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"dubbo/consumer.xml"}); context.start();
DemoService demoService = (DemoService)context.getBean("demoService"); String hello = demoService.sayHello("world"); System.out.println("xml通过服务发现调用结果: " + hello);
DemoService ipConsumerService = (DemoService)context.getBean("ipConsumerService"); System.out.println("xml通过ip直连调用结果: " + ipConsumerService.sayHello("world"));
ReferenceConfig<DemoService> reference = new ReferenceConfig<>(); reference.setApplication(new ApplicationConfig("testConsume")); reference.setRegistry(new RegistryConfig("zookeeper://127.0.0.1:2181")); reference.setInterface(DemoService.class); System.out.println("code通过服务发现调用结果: " + reference.get().sayHello("world"));
ReferenceConfig<DemoService> reference1 = new ReferenceConfig<>(); reference1.setApplication(new ApplicationConfig("testConsume")); reference1.setUrl("dubbo://192.168.0.101:20880/cn.idea360.api.DemoService?anyhost=true&application=dubbo-provider&dubbo=2.5.3&interface=cn.idea360.api.DemoService&methods=sayHello&pid=66102&side=provider×tamp=1669043877926"); reference1.setInterface(DemoService.class); System.out.println("code通过ip调用结果: " + reference1.get().sayHello("world")); } }
|
说明: dubbo注册url可以从zk中获取, 例如示例demo中zk配置为
1 2 3 4 5 6 7
| # dubbo基础信息 [zk: localhost:2181(CONNECTED) 7] ls /dubbo/cn.idea360.api.DemoService [configurators, consumers, providers, routers]
# 服务接口注册信息 [zk: localhost:2181(CONNECTED) 10] ls /dubbo/cn.idea360.api.DemoService/providers [dubbo://192.168.0.101:20880/cn.idea360.api.DemoService?anyhost=true&application=dubbo-provider&dubbo=2.5.3&interface=cn.idea360.api.DemoService&methods=sayHello&pid=66747&side=provider×tamp=1669045212728]
|
-
该接口需单独打包,在服务提供方和消费方共享
-
对服务消费方隐藏实现
-
也可以使用 IoC 注入
常用测试
pom.xml
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 35 36 37 38 39 40
| <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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>cn.idea360</groupId> <artifactId>dubbo-consumer</artifactId> <version>1.0</version>
<properties> <maven.compiler.source>11</maven.compiler.source> <maven.compiler.target>11</maven.compiler.target> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties>
<dependencies> <dependency> <groupId>cn.easyliao</groupId> <artifactId>webcall-config-api</artifactId> <version>0.3.1.1</version> </dependency> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo</artifactId> <version>2.7.18</version> </dependency> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-dependencies-zookeeper</artifactId> <version>2.7.18</version> <type>pom</type> </dependency> <dependency> <groupId>org.javassist</groupId> <artifactId>javassist</artifactId> <version>3.29.2-GA</version> </dependency> </dependencies> </project>
|
consumer1
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 35 36 37 38
| package cn.idea360.dubbo.consumer;
import cn.easyliao.webcall.config.service.api.ConfigLoadService; import com.alibaba.fastjson.JSON; import org.apache.dubbo.config.ApplicationConfig; import org.apache.dubbo.config.ReferenceConfig; import org.apache.dubbo.config.RegistryConfig;
public class Consumer {
public static void main(String[] args) { ApplicationConfig application = new ApplicationConfig(); application.setName("demo-consumer");
RegistryConfig registry = new RegistryConfig(); registry.setAddress("zookeeper://10.10.10.208:2181");
ReferenceConfig<ConfigLoadService> reference = new ReferenceConfig<>(); reference.setApplication(application); reference.setRegistry(registry); reference.setInterface(ConfigLoadService.class); reference.setGroup("test-prd18"); reference.setVersion("0.0.1");
ConfigLoadService configLoadService = reference.get(); System.out.println("code通过服务发现调用结果: " + JSON.toJSONString(configLoadService.getCompanyGlobalConfig(1))); } }
|
consumer2
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 35 36 37 38 39
| package cn.idea360.dubbo.consumer;
import cn.easyliao.webcall.config.service.api.ConfigLoadService; import com.alibaba.fastjson.JSON; import org.apache.dubbo.config.ReferenceConfig; import org.apache.dubbo.config.RegistryConfig; import org.apache.dubbo.config.bootstrap.DubboBootstrap; import org.apache.dubbo.config.utils.ReferenceConfigCache;
public class DubboBootstrapConsumer {
public static void main(String[] args) {
ReferenceConfig<ConfigLoadService> reference = new ReferenceConfig<>(); reference.setInterface(ConfigLoadService.class); reference.setGroup("test-prd18"); reference.setVersion("0.0.1"); reference.setUrl("dubbo://10.10.10.217:7515/cn.easyliao.webcall.config.service.api.ConfigLoadService?xxx");
DubboBootstrap bootstrap = DubboBootstrap.getInstance(); bootstrap.application("demo-consumer") .registry(new RegistryConfig("zookeeper://10.10.10.208:2181")) .reference(reference) .start();
ConfigLoadService configLoadService = ReferenceConfigCache.getCache().get(ConfigLoadService.class); System.out.println("code通过服务发现调用结果: " + JSON.toJSONString(configLoadService.getCompanyGlobalConfig(1))); } }
|
consumer3
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
| public class BaseConsumer {
private static final String ADREESS = "nacos://xxx.net:80/nacos?namespace=framework"; private static final String GROUP = "framework"; private static final String VERSION = "0.0.1";
protected static <T> T getProvider(Class<T> cls) { ApplicationConfig application = new ApplicationConfig(); application.setName("demo-consumer");
RegistryConfig registry = new RegistryConfig(); registry.setAddress(ADREESS);
ReferenceConfig<T> reference = new ReferenceConfig<>(); reference.setApplication(application); reference.setRegistry(registry); reference.setInterface(cls); reference.setGroup(GROUP); reference.setVersion(VERSION);
return reference.get(); } }
|
consumer4
如下配置文件,我们可以知道服务暴露在本地的7514端口
1 2 3 4 5
| dubbo.application.name=crud-provider dubbo.registry.address=10.10.10.215:2181 dubbo.registry.protocol=zookeeper dubbo.protocol.name=dubbo dubbo.protocol.port=7514
|
使用 telnet 命令进行访问,如下出现 Dubbo 字样时说明连接成功
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| ➜ blog git:(master) ✗ telnet 10.10.10.215 7514 Trying 10.10.10.215... Connected to 10.10.10.215. Escape character is '^]'.
dubbo>ls PROVIDER: cn.easyliao.webcall.config.service.api.ConfigCRUDService cn.easyliao.webcall.config.service.api.ConfigLoadService
dubbo>ls cn.easyliao.webcall.config.service.api.ConfigLoadService cn.easyliao.webcall.config.service.api.ConfigLoadService (as provider): getCompany getCompanyGlobalConfig
dubbo>invoke cn.easyliao.webcall.config.service.api.ConfigLoadService.getCompany(1) Use default service cn.easyliao.webcall.config.service.api.ConfigLoadService. result: {"companyId":1,"companyName":"北京易聊科技有限公司","fax":"010-12345555","industry":"0","phoneNumber":"11111111","street":"朝阳区社区-同步","webSite":"http://www.easyliao555.cn"} elapsed: 32 ms. dubbo>
|
最后
本篇为dubbo学习入门笔记。欢迎大家关注微信公众号【当我遇上你】