一.项目搭建
1.步骤
-
建立一个空项目,添加两个springboot模块:provide-server和consumer-server 一个提供服务另一个消费服务(略)
-
两个模块的pom.xml中都导入依赖
-
编写provide-server代码
-
编写consumer-server代码
-
启动dubbo和zookeeper(略)
-
启动provide-server和consumer-server(略)
-
查看dubbo后台
2.两个模块的pom.xml中都导入依赖
<!-- Dubbo Spring Boot Starter -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.7.3</version>
</dependency>
<!--zkclient-->
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
<!-- 引入zookeeper -->
<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>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.14</version>
<!--排除这个slf4j-log4j12-->
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
</exclusions>
</dependency>
3.编写provide-server代码
-
添加service目录添加接口和实现类以及配置application.properties文件
TicketService:
public interface TickerService {
String getTicket();
}
TicketServiceImpl:
-
注意注解:@Service是dubbo的,@Component是spring的
import org.apache.dubbo.config.annotation.Service;
import org.springframework.stereotype.Component;
@Service //这个是dubbo的service注解不是spring的,加了这个注解在项目启动后就自动注册到注册中心zookeeper中
@Component //这个在spring中注册,由spring管理的组件,不用Service是因为为了避免与dubbo的service冲突(而忘了import)
public class TickerServiceImpl implements TickerService {
@Override
public String getTicket() {
return "getTicket success!";
}
}
applictaion.properties:向zookeeper注册服务
server.port=8001
#当前应用名字
dubbo.application.name=provider-server
#注册中心地址
dubbo.registry.address=zookeeper://127.0.0.1:2181
#扫描指定包下服务
dubbo.scan.base-packages=ustc.wzh.service
4.编写consumer-server代码
-
添加service目录使用服务的类以及配置application.properties文件,最后还要添加一个和调用接口相同路径名的接口
UserService:
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Service;
@Service //spring容器管理
public class UserService {
//拿到远程的provider-server提供的服务
@Reference //dubbo的注解用于引用服务,引用服务方式有两种:1.Pom坐标,2.需要定义路径相同的接口名
TickerService tickerService;
public void buyTicket(){
String ticket = tickerService.getTicket();
System.out.println("在注册中心拿到:"+ticket);
}
}
在consumer-server的测试类中使用:
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import ustc.wzh.service.UserService;
@SpringBootTest
class ConsumerServerApplicationTests {
@Autowired
UserService userService;
@Test
void contextLoads() throws InterruptedException {
while (true){
Thread.sleep(1000);
userService.buyTicket();
}
}
}
application.properties:
server.port=8002
#当前应用名字
dubbo.application.name=consumer-server
#注册中心地址
dubbo.registry.address=zookeeper://127.0.0.1:2181
在service中添加一个TicketService接口直接将provide-server中的TicketService接口拷过来即可
5.查看dubbo后台