開發(fā)環(huán)境
- eclipse 4.7.3a
- jdk 8
- zookeeper-3.5.4-beta
- maven 3.5.2
Dubbo 架構
- 服務提供方
- 服務消費方
- 注冊中心 (使用zookeeper作為本例注冊中心)
- 監(jiān)控中心
準備工作
為了更好的理解Dubbo的工作流程疯暑,我們先下載dubbo源碼和管理后臺源碼
# 下載dubbo源碼
git clone git@github.com:freeseawind/incubator-dubbo.git
# 下載管理后臺源碼
git clone https://github.com/dubbo/dubbo-ops.git
使用以下命令分別做一次構建,然后我們可以和好基友happy的去玩兩把游戲等待編譯結束
mvn clean install -Dmaven.test.skip
N分鐘后,自從Dubbo “高富帥“ 以后茶没,這個構建流程就越來越麻麻dei(慢)了登下。好吧匠童,這咱都能忍巩趁,但是what竟然編譯失敗栈幸,這簡直不能忍撰茎。
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.
6.0:testCompile (default-testCompile) on project dubbo-config-spring: Compilatio
n failure -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e swit
ch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please rea
d the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureExc
eption
[ERROR]
[ERROR] After correcting the problems, you can resume the build with the command
[ERROR] mvn <goals> -rf :dubbo-config-spring
嗯嵌牺、啊、唉龄糊,研究了半天還是無果逆粹,決定先跳過dubbo-config-spring
的編譯,如果有知道的朋友歡迎告知本菜 (T T)炫惩。
編碼時刻
我們使用spring boot 2.x + dubbo 來作為本次學習的例子
使用xml配置方式見官方DEMO, 這里演示如何在spring boot 2中運行Dubbo 的 hello world
1. 創(chuàng)建Maven項目
├── copycat-dubbo
│ ├── pom.xml
│ └── copycat-dubbo-example
│ └── src/main/java
│ └── src/main/resource
│ └── dubbo.properties
│ └── log4j.properties
│ └── application-customer.properties
│ └── application.properties
│ └── pom.xml
copycat-dubbo項目pom文件部分配置如下:
<!-- 以上部分忽略-->
<dependencyManagement>
<dependencies>
<dependency>
<!-- Import dependency management from Spring Boot -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.0.2.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.6.2</version>
</dependency>
<!-- zookeeper依賴包 -->
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-client</artifactId>
<version>4.0.1</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>4.0.1</version>
</dependency>
</dependencies>
</dependencyManagement>
copycat-dubbo-example項目pom文件部分配置如下:
<dependencies>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-client</artifactId>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
2. 定義服務接口和實現(xiàn)
定義服務接口
public interface DemoService
{
String sayHello(String name);
}
定義接口實現(xiàn)
@Service(timeout = 5000)
public class DemoServiceImpl implements DemoService
{
@Override
public String sayHello(String name)
{
return "Hello " + name;
}
}
細心的朋友會發(fā)現(xiàn)在接口的實現(xiàn)上僻弹,增加了
@Service(timeout = 5000)
的注解,這個注解的主要作用是暴露和注冊服務他嚷。
3. 服務消費者
服務消費者配置
@Configuration
public class ConsumerConf
{
@Bean
public ApplicationConfig applicationConfig()
{
ApplicationConfig applicationConfig = new ApplicationConfig();
applicationConfig.setName("consumer-test");
return applicationConfig;
}
/**
* 服務消費者配置
*
* @author freeseawind
* @return
*/
@Bean
public ConsumerConfig consumerConfig()
{
ConsumerConfig consumerConfig = new ConsumerConfig();
consumerConfig.setTimeout(3000);
return consumerConfig;
}
/**
* 注冊中心配置
*
* @author freeseawind
* @return
*/
@Bean
public RegistryConfig registryConfig()
{
RegistryConfig registryConfig = new RegistryConfig();
registryConfig.setAddress("zookeeper://127.0.0.1:2181");
registryConfig.setClient("curator");
return registryConfig;
}
}
這里的配置信息只是把xml替換成API的方式蹋绽,通過Spring的IOC容器對Bean進行管理。詳見API 參考手冊 和schema 配置參考手冊
消費者啟動類
@SpringBootApplication
@Controller
@DubboComponentScan(basePackages = "github.freeseawind.springboot.helloworld.consumer")
public class ConsumerApplication
{
@Reference
private DemoService demoService;
public static void main(String[] args)
{
SpringApplication.run(ConsumerApplication.class, args);
}
@RequestMapping("/a")
@ResponseBody
public String sayHello()
{
return demoService.sayHello("freeseawind");
}
}
@DubboComponentScan 是Dubbo自定義bean加載的擴展筋蓖。它在消費方主要的作用是卸耘,掃描需要注入的服務接口(該服務接口以@Reference注解標識),同時向注冊中心發(fā)起注冊消費者服務請求粘咖。
3. 服務提供者
服務提供者配置
@Configuration
public class ProviderConf
{
@Bean
public ApplicationConfig applicationConfig()
{
ApplicationConfig applicationConfig = new ApplicationConfig();
applicationConfig.setName("provider-test");
return applicationConfig;
}
@Bean
public RegistryConfig registryConfig()
{
RegistryConfig registryConfig = new RegistryConfig();
registryConfig.setAddress("zookeeper://127.0.0.1:2181");
registryConfig.setClient("curator");
return registryConfig;
}
@Bean
public ProtocolConfig protocolConfig()
{
ProtocolConfig protocolConfig = new ProtocolConfig();
protocolConfig.setPort(20881);
protocolConfig.setName("dubbo");
return protocolConfig;
}
}
服務提供者啟動類
@SpringBootApplication
@EnableAutoConfiguration
@DubboComponentScan(basePackages = "github.freeseawind.service.impl")
public class ProviderApplication
{
public static void main(String[] args)
{
SpringApplication.run(ProviderApplication.class, args);
}
}
@DubboComponentScan 掃描的是服務接口的實現(xiàn)(該實現(xiàn)以@Service注解標識)蚣抗,同時向注冊中心發(fā)起注冊服務提供者請求。
順序啟動服務
- 注冊中心
- 服務提供者
- 服務消費者
- 管理后臺
1. 啟動注冊中心
略
2. 啟動服務提供者
截取部分啟動日志如下
[DUBBO] Export dubbo service github.freeseawind.service.DemoService to url dubbo://169.254.63.31:20881/
[DUBBO] Start NettyServer bind /0.0.0.0:20881, export /169.254.63.31:20881
[DUBBO] Subscribe: provider://169.254.63.31:20881/github.freeseawind.service.DemoService?
3. 啟動服務消費者
配置啟動參數(shù)
截取部分啟動日志如下
[DUBBO] Subscribe: consumer://169.254.63.31/github.freeseawind.service.DemoService?
[DUBBO] Successed connect to server /169.254.63.31:20881 from NettyClient 169.254.63.31
[DUBBO] Start NettyClient xiaolong-PC/169.254.63.31 connect to the server /169.254.63.31:20881
從啟動日志中我們可以看到消費者和服務提供者建立了連接
4. 啟動管理后臺
訪問 http://localhost:7001/
用戶名:root 密碼:root
相關配置在application.properties中可找到
我們可以看到服務提供者和消費者均已經(jīng)注冊成功瓮下,并可在管理后臺查看