ZooKeeper 是一个分布式的,开放源码的分布式应用程序协调服务。它是一个为分布式应用提供一致性服务的软件,提供的功能包括:配置维护、域名服务、分布式同步、组服务等。
Dubbo是Alibaba开源的分布式服务框架,它最大的特点是按照分层的方式来架构,使用这种方式可以使各个层之间解耦合(或者最大限度地松耦合)。从服务模型的角度来看, Dubbo采用的是一种非常简单的模型,要么是提供方提供服务,要么是消费方消费服务,所以基于这一点可以抽象出服务提供方(Provider)和服务消费方(Consumer)两个角色。
这里只需要用到2181这个端口,只把它暴露,其他的两个端口不需要。
This image includes EXPOSE 2181 2888 3888 8080 (the zookeeper client port, follower port, election port, AdminServer port respectively), so standard container linking will make it automatically available to the linked containers. Since the Zookeeper “fails fast” it’s better to always restart it.
【TicketService.java】
public interface TicketService { public String getTicket(); }【TicketServiceImpl.java】
import com.alibaba.dubbo.config.annotation.Service; import org.springframework.stereotype.Component; @Component // 将服务发送出去 @Service public class TicketServiceImpl implements TicketService{ @Override public String getTicket() { return "《姜子牙》"; } }添加在Applicaiton上面@EnableDubbo注解
@SpringBootApplication @EnableDubbo // 开启Dubbo服务 public class ProviderTickerApplication { public static void main(String[] args) { SpringApplication.run(ProviderTickerApplication.class, args); } }1.引入依赖‘
<!-- https://mvnrepository.com/artifact/org.apache.dubbo/dubbo-spring-boot-starter --> <dependency> <groupId>com.alibaba.boot</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>0.1.0</version> </dependency> <!--引入zookeeper的客户端工具--> <!-- https://mvnrepository.com/artifact/com.github.sgroschupf/zkclient --> <dependency> <groupId>com.github.sgroschupf</groupId> <artifactId>zkclient</artifactId> <version>0.1</version> </dependency>2.配置dubbo的注册中心地址
dubbo.application.name=consumer-user dubbo.registry.address=zookeeper://192.168.64.129:21813.引用服务
【UserService.java】
@Service public class UserService{ @Reference TicketService ticketService; public void hello(){ String ticket = ticketService.getTicket(); System.out.println("买到票了:"+ticket); } }