前言
本文是dubbo系列的第一篇免猾,在正式講解dubbo的源碼之前织中,需要搭建一套dubbo的樣例苗缩,用于我們閱讀源碼以及理解dubbo的核心功能崭参,本文是以springboot
為基礎(chǔ)的,其他的通過xml配置的太麻煩了慰于。
版本說明
springboot starter : 0.1.1
dubbo版本: 2.6.2
新建項目
新建項目dubbo-provider钮科, dubbo-service,dubbo-interface婆赠,dubbo-consumer這四個項目绵脯,項目結(jié)構(gòu)如下
pom配置
dubbo-provider pom.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.4.RELEASE</version>
</parent>
<properties>
<dubbo-spring-boot>1.0.0</dubbo-spring-boot>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.14.8</version>
</dependency>
</dependencies>
</dependencyManagement>
dubbo-service 和dubbo-consumer的pom文件相同 pom.xml
<dependencies>
<!-- Spring Boot Dubbo 依賴 -->
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>0.1.1</version>
</dependency>
<!-- Spring Boot Web 依賴 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Spring Boot Test 依賴 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<artifactId>dubbo-interface</artifactId>
<version>1.0-SNAPSHOT</version>
<groupId>com.dubbo</groupId>
</dependency>
</dependencies>
dubbo-interface pom文件
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
<testSource>${java.version}</testSource>
<testTarget>${java.version}</testTarget>
</configuration>
</plugin>
</plugins>
</build>
編碼
dubbo-service
啟動類
@SpringBootApplication
@DubboComponentScan("com.dubbo.provider.facade")
public class ProviderApplication {
public static void main(String[] args) {
SpringApplication.run(ProviderApplication.class);
}
}
application.yml文件
spring:
application:
name: dubbo-service
profiles:
active: dev
server:
port: 7100
dubbo:
application:
name: dubbo-service
registry:
address: zookeeper://localhost:2181
protocol:
name: dubbo
port: 20880
裝飾層
@Service 注解是**com.alibaba.dubbo.config.annotation.Service ** 的,不是spring提供的
/**
* @Author 張云和
* @Date 2018/9/11
* @Time 14:48
*/
@Service
public class UserFacade implements UserFacadeService {
@Resource
private UserService userService;
@Override
public UserDTO getUser(Integer userId) {
UserPO userPO = userService.getUser(userId);
if(null==userPO){
return null;
}
UserDTO userDTO = new UserDTO();
userDTO.setUserId(userPO.getUserId());
userDTO.setUserName(userPO.getUserName());
return userDTO;
}
}
UserFacadeService 為暴露出去的接口服務(wù)页藻,在dubbo-interface中進(jìn)行定義桨嫁,方便消費(fèi)者和生產(chǎn)者都能引用到
內(nèi)部service層
@Service // 這個注解是spring的注解,不是dubbo的
public class UserServiceImpl implements UserService {
@Override
public UserPO getUser(Integer userId) {
// 此處沒有從數(shù)據(jù)庫查詢
UserPO userPO = new UserPO();
userPO.setUserId(userId);
userPO.setUserName("測試用戶:"+userId);
userPO.setLastLoginTime(new Date());
userPO.setPassword("password");
return userPO;
}
}
dubbo-consumer
啟動類
@SpringBootApplication
@DubboComponentScan("com.dubbo.consumer.service")
public class ConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class);
}
}
建立一個service類份帐,用來引用dubbo-service中提供的接口服務(wù)
@Service // spring的
public class UserService implements UserFacadeService{
@Reference // dubbo的接口引用注解
private UserFacadeService userFacadeService;
@Override
public UserDTO getUser(Integer userId) {
return userFacadeService.getUser(userId);
}
}
建立controller用來測試接口
@RestController
public class TestController {
@Resource
private UserService userService;
@RequestMapping("getUser")
public UserDTO getUser(Integer userId){
return userService.getUser(userId);
}
}
application.yml
spring:
application:
name: dubbo-consumer
profiles:
active: dev
server:
port: 7101
dubbo:
application:
name: dubbo-consumer
registry:
address: zookeeper://localhost:2181
protocol:
name: dubbo
port: 20880
測試
打開瀏覽器璃吧,訪問http://localhost:7101/getUser?userId=1 ,得到結(jié)果
{"userId":1,"userName":"測試用戶:1"}
本文搭建demo的項目結(jié)構(gòu)是按照我正式生產(chǎn)環(huán)境進(jìn)行搭的,所以跟其他博客上的項目結(jié)構(gòu)有些區(qū)別废境,不是那么簡單畜挨,有需要demo的可以私信我! 一起交流學(xué)習(xí)噩凹!