Dubbo与SpringBoot的结合使用还是比较简单的。首先创建一个基础服务接口模块。
1.基础服务接口模块的搭建
pom文件的内容如下
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
1.创建一个接口类
public interface DubboBaseTestService {
TestUser getUser();
}
2.创建基础的实体类
@Getter
@Setter
@NoArgsConstructor
public class TestUser implements Serializable {
private String userId;
private String userName;
public TestUser(String userId, String userName) {
this.userId = userId;
this.userName = userName;
}
}
2.服务提供模块的搭建
pom文件的内容如下
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>0.2.0</version>
</dependency>
<!--这里的dubboInterfaceTest就是上面的基础模块-->
<dependency>
<artifactId>demo</artifactId>
<version>1.0-SNAPSHOT</version>
<groupId>dubboInterfaceTest</groupId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
1.创建服务实现类
//这里的Service标签的Dubbo的Service标签
@Service
public class DubboProviderService implements DubboBaseTestService {
@Resource
UserService userService;
@Override
public TestUser getUser() {
return userService.getUser();
}
}
2.编写业务相关的代码
public interface UserService {
TestUser getUser();
}
//这里的Service标签的Spring的Service标签
@Service("userService")
public class UserServiceImpl implements UserService{
@Override
public TestUser getUser() {
return new TestUser("232","szh");
}
}
3.在启动类上面加上EnableDubbo注解,配置配置文件
在启动类上面加上EnableDubbo标签,这个标签包含了@EnableDubboConfig和
@DubboComponentScan这两个标签,后面会进行代码分析
@SpringBootApplication
@EnableDubbo(scanBasePackages = {"dubboprovidertest.demo"})
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
spring:
application:
name: dubbo-service
server:
port: 8080
dubbo:
application:
name: dubbo-service
registry:
address: zookeeper://localhost:2181
protocol:
name: dubbo
port: 20880
3.服务消费模块的搭建
pom文件的内容如下
<dependency>
<artifactId>demo</artifactId>
<version>1.0-SNAPSHOT</version>
<groupId>dubboInterfaceTest</groupId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>0.2.0</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
1.创建服务消费类
// 这里的Service标签是spring的
@Service
public class UserService implements DubboBaseTestService {
@Reference // dubbo的接口引用注解
private DubboBaseTestService userFacadeService;
@Override
public TestUser getUser() {
return userFacadeService.getUser();
}
}
2.创建测试用的Controller
@RestController
public class TestController {
@Resource
private UserService userService;
@RequestMapping("getUser")
public TestUser getUser(){
return userService.getUser();
}
}
3.在启动类上面加上EnableDubbo注解,配置配置文件
@SpringBootApplication
@EnableDubbo(scanBasePackages = {"dubboconsumertest.demo"})
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
spring:
application:
name: dubbo-consummer
server:
port: 8081
dubbo:
application:
name: dubbo-consummer
registry:
address: zookeeper://localhost:2181
protocol:
name: dubbo
port: 20880
启动上面三个项目,然后访问http://localhost:8081/getUser接口,会显示
{"userId":"232","userName":"szh"}
一个基础的dubbo跟spring boot的结合框架就搭出来了。接下来就是对源码的分析了。
第一步,Dubbo在Spring Boot启动时如何运行的Dubbo在Spring Boot启动时如何运行的