一、安装 zookeeper 作为注册中心
使用 Docker 方式安装 zookeeper
1、获取镜像
docker pull registry.docker-cn.com/library/zookeeper
2、启动 zookeeper 容器
docker run --name zk01 -p 2181:2181 --restart always -d ImagesId
3、查看已经运行的容器
docker ps
二、创建服务提供者
1、引入 dubbo 和 zkclient 的依赖
提供一下完整的 pom.xml 文件:
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.12.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.njf</groupId>
<artifactId>spring-boot-14-dubbo-provider</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-boot-14-dubbo-provider</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.6.1</version>
</dependency>
<!--引入 dubbo-->
<dependency>
<groupId>com.alibaba.spring.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.0.0</version>
<exclusions>
<exclusion>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
</exclusion>
</exclusions>
</dependency>
<!--引入 zk 客户端工具-->
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</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>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2、编写服务
编写一个提供服务的接口:
public interface TicketService {
public String getTicket();
}
提供该接口的实现类:
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.stereotype.Component;
@Component
@Service //将服务发布出去
public class TicketServiceImpl implements TicketService{
@Override
public String getTicket() {
return "《厉害了,我的国!》";
}
}
使用 @Component 注解把组件放在Spring容器中;
使用 Dubbo 的 @Service 注解把服务发布出去
3、配置 dubbo 的扫描包和注册中心地址
在application.properties中配置文件中进行配置:
#应用名称
dubbo.application.name=provider-ticket
#把应用发布到注册中心
dubbo.registries.address=zookeeper://192.168.1.6:2181
#将哪个包下的那些服务发布出去
dubbo.scan.base-packages=com.njf.dubbo.ticket.service
项目结构:
4、小结
将服务提供者注册到注册中心
(1)引入 dubbo 和 zkclient 相关依赖
(2)配置 dubbo 的扫描包和注册中心地址
(3)使用 @Service 发布服务
三、创建服务消费者
1、引入 dubbo 和 zkclient 的依赖
完整的 pom.xml
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.12.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.njf</groupId>
<artifactId>spring-boot-14-dubbo-consumer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-boot-14-dubbo-consumer</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!--引入 dubbo-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.6.1</version>
</dependency>
<!--引入 dubbo-->
<dependency>
<groupId>com.alibaba.spring.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.0.0</version>
<exclusions>
<exclusion>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
</exclusion>
</exclusions>
</dependency>
<!--引入 zk 客户端工具-->
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</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>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2、配置 dubbo 的注册中心地址
在 application.properties 中配置 dubbo 的注册中心
dubbo.application.name=consumer-user
dubbo.registry.address=zookeeper://192.168.1.6:2181
3、引用服务
import com.alibaba.dubbo.config.annotation.Reference;
import com.njf.dubbo.ticket.service.TicketService;
import org.springframework.stereotype.Service;
@Service
public class UserService {
//把引用的 service 放在项目中,必须要是同包名,同类名,然后远程引用,按照接口的全类名进行匹配
@Reference
TicketService ticketService;
public void hello() {
String ticket = ticketService.getTicket();
System.out.println("买到票了---> " + ticket);
}
}
项目结构:
4、小结
(1)引入依赖
(2)配置 dubbo 的注册中心地址
(3)引用服务
5、测试
测试类:
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringBoot14DubboConsumerApplicationTests {
@Autowired
UserService userService;
@Test
public void contextLoads() {
userService.hello();
}
}