市面上大概是用两种方式使用微服务,①dubbo,②,springcloud;
dubbo是使用rpc进行服务调度,而springcloud是用轻量级的http方式;dobbo侧重于服务间通信,在负载均衡,网关,路由,熔断方面依赖于第三方,而springcloud则提供了一整套完整的解决方案。且zookeeper偏向于保证一致性,而springcloud 的eureka 是偏向于高可用的。
1,准备
①,docker安装zookeeper
#拉取zookeeper,使用镜像加速,否则很可能下载不下来
docker pull registry.docker-cn.com/library/zookeeper
#运行zookeeper镜像
#--name 指定容器名,-p 表示暴露的端口 --restart 自动重启 -d 后台运行,最后带上镜像id
docker run --name zk -p 2181:2181 --restart always -d bf5cbc9d5cac
②,dubbo现在是apache的项目
③,点击view to GitHub,这里有springboot整合dubbo的详细说明
2,版本说明
这里以springboot的1.5x版本为例
3,项目结构
①,利用idea先创建一个空工程,然后给其添加,api模块,provide模块,customer模块
②,eclipse中直接创建一个类型为pom是父工程,然后右键父工程给其添加maven模块即可。
4,api模块
①,作用:用于定义公共的接口,给provider和customer依赖
②,pom.xml
<--用于构建项目-->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
③,定义一个买票接口
public interface TicketService {
String buyTicket();
}
④,利用maven插件将api工程安装到maven仓库,供provider和customer依赖
⑤,双击install
⑥,当控制台输出BUILD SUCCESS,说明安装成功
5,provider配置
①,pom.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.12.RELEASE</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--引入dubbo的场景启动器-->
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>0.1.0</version>
</dependency>
<!-- 引入zookeeper的客户端 -->
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
<!--引入公共的api-->
<dependency>
<groupId>com.api</groupId>
<artifactId>api</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
②,application.properties 配置
#应用名
dubbo.application.name=ticket-provider
#注册地址
dubbo.registry.address=zookeeper://192.168.43.28:2181
#需要扫描的包
dubbo.scan.base-packages=com.provider.ticketprovider.ticket.service.impl
③,编写提供者
import com.api.api.ticket.service.TicketService;
import org.springframework.stereotype.Component;
import com.alibaba.dubbo.config.annotation.Service;
@Component
//这个是dobbo的Service 注解
@Service
public class TicketServiceImpl implements TicketService {
@Override
public String buyTicket() {
return "《死亡笔记》";
}
}
由于TicketServiceImpl 加上了dubbo的@Service注解,在应用启动后,会将这个提供者注册到zookeeper,这样消费者就能消费这个服务。
6,customer配置
①,pom.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.12.RELEASE</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--引入dubbo的场景启动器-->
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>0.1.0</version>
</dependency>
<!-- 引入zookeeper的客户端 -->
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
<!--引入公共的api-->
<dependency>
<groupId>com.api</groupId>
<artifactId>api</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
②,application.properties 配置
dubbo.application.name=ticket-customer
dubbo.registry.address=zookeeper://192.168.43.28:2181
server.port=8083
③,controller层代码
import com.alibaba.dubbo.config.annotation.Reference;
import com.api.api.ticket.service.TicketService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class CustomerController {
// 将会得到Ticket服务
@Reference
private TicketService ticketService;
@RequestMapping("/")
public String index(){
return ticketService.buyTicket();
}
}
7,测试
①,先启动provider
②,再启动customer
③,浏览器调度消费者的消费方法
④,看到上图,说明调度成功