zookeeper能够在分布式项目中承担如下几个功能:java
1)注册中心linux
2)存储数据: 分布式锁git
3)观察者模式:作到多个分布式服务器之间的数据同步github
要么使用docker搭建,要么使用原生的方式搭建。spring
tar -zxvf jdk.....tar.gzdocker
vim /etc/profilevim
复制如下内容到该文件的末尾api
export JAVA_HOME=/usr/local/java/jdk1.8.0_191springboot export JRE_HOME=/usr/local/java/jdk1.8.0_191/jrebash export CLASSPATH=$CLASSPATH:$JAVA_HOME/lib:$JAVA_HOME/jre/lib export PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH:$HOME/bin |
:wq
source /etc/profile
在zookeeper/conf文件夹内,修改zoo_sample.cfg中的内容后并重命名成zoo.cfg
注意: 要使用linux中的命令,必须两种方式: 要么./zkServer.sh 要么是完整路径:
/usr/local/zookeeper/zookeeper-3.4.14/bin/zkServer.sh
./zkServer.sh start
./zkServer.sh status
./zkServer.sh stop
相关概念:
1) 工程: 包含多个项目的一个统称
接口项目取消父依赖
在项目中建立具体的接口
编写实现类,实现接口,并重写方法,注意要把api的项目依赖进来。
<?
xml version="1.0" encoding="UTF-8"
?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!--
配置应用名称-->
<dubbo:application name="product-service-provider"/>
<!--
配置注册中心的地址-->
<dubbo:registry address="zookeeper://10.31.163.100:2181"/>
<!--
配置当前服务在dubbo中的协议端口-->
<dubbo:protocol port="20880"/>
<!--
配置接口-->
<dubbo:service interface="com.qf.service.product.api.IProductService" ref="productService"/>
<!--
配置接口的实现类-->
<bean id="productService" class="com.qf.product.service.impl.ProductServiceImpl"/></beans> |
注意:前提是要启动zk注册中心
public class TestServiceProvider {
@Test
public void testService() throws IOException {
ClassPathXmlApplicationContext context =
new ClassPathXmlApplicationContext(new String[]{"classpath:provider.xml"});
//将服务提交给dubbo的container,而后注册到注册中心上
context.start();
System.out.println("商品服务开始提供服务");
System.out.println("按任意键中止");
//让服务在收到任意输入内容以前不会结束
System.in.read();
}
}复制代码 |
当看到这个结果,就表示服务发布成功
<!--
引入dubbo的依赖-->
<dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.5.8</version> </dependency>
<!-- zookeeper
客户端依赖 -->
<dependency> <groupId>com.github.sgroschupf</groupId> <artifactId>zkclient</artifactId> <version>0.1</version> </dependency> <dependency> <groupId>com.qf</groupId> <artifactId>my-dubbo-product-api</artifactId> <version>1.0.0-SNAPSHOT</version> </dependency> |
<?
xml version="1.0" encoding="UTF-8"
?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!--
配置消费者应用的名字-->
<dubbo:application name="product-consumer"/>
<!--
配置注册中心的地址-->
<dubbo:registry address="zookeeper://10.31.163.100:2181"/>
<!--
配置要使用的接口所在的路径-->
<dubbo:reference interface="com.qf.service.product.api.IProductService" id="productService"/></beans> |
@Test
out
.println(s);} |
小结:
(1)引入依赖
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>0.2.0</version>
</dependency>
(2)将xml配置文件放进resources中
(3)在springboot的入口类上打上注解:@ImportResource("classpath:provider.xml")
(1)引入依赖
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>0.2.0</version>
</dependency>
(2)将xml配置文件放进resources中
(3)在springboot的入口类上打上注解:@ImportResource("classpath:consumer.xml")
(4)使用autowired将接口的实现类的对象自动注入进来,至关因而经过dubbo(zk)来提供服务的提供者的类。
@SpringBootTest
out
.println(productService.showProduct());} } |
(1)引入依赖
(2)编写application.yml文件,加入如下配置(原本在xml中的配置将写在yml中)
server: |
(3)编写服务提供者类并实现接口,并在接口上打上注解:
@Component
@Service <==import com.alibaba.dubbo.config.annotation.Service;
(4)在入口类上打上注解
@EnableDubbo
(1)引入依赖
(2)编写application.yml文件,加入如下配置(原本在xml中的配置将写在yml中)
server: |
(3)在须要用到服务提供者类的时候,使用@Reference注解
来自于com.alibaba.dubbo.config.annotation.Reference;复制代码
@SpringBootTest
out
.println(productService.showProduct());} } |
(4)在入口类上打上注解
@EnableDubbo