标签:apache 对象 rop work bom dea ati gem ESS
实验环境:win7 64,idea2018.3.3
环境搭建
1、新建一个空项目
2、然后添加一个maven模块作为父模块名字为ticketparent
3、然后添加一个spring模块名字为supplier-ticket用来生产对象
4、然后再添加一个spring模块名字为consumer-user用来消费
5、给maven父模块再指定一层父模块
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
6、给父模块添加依赖
<properties>
<spring-boot.version>2.1.1.RELEASE</spring-boot.version>
<dubbo.version>2.7.0</dubbo.version>
</properties>
<dependencyManagement>
<dependencies>
<!-- Spring Boot -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- Aapche Dubbo -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-dependencies-bom</artifactId>
<version>${dubbo.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
<version>${dubbo.version}</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
</exclusion>
<exclusion>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
</exclusion>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!-- Dubbo Spring Boot Starter -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.7.0</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
</dependency>
</dependencies>
7、给consumer-user和supplier-ticket这两个spring模块指定ticketparent为父模块
如果pom提升出错这里把之前那个springboot父模块代替掉然后删除groupId就可以了
8、现在需要客户端工具连接zookeeper因为不管是消费者还是生产者我们都需要连接zookeeper所以
我们在父模块再次添加dependence添加curator客户端工具比较多要导三个包
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
</dependency><dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
</dependency><dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
</dependency>
编写代码
1、给supplier-ticket这个模块新建一个接口
2、然后再新建一个实现类 然后添加Service注解注意是org.apache.dubbo.config.annotation.Service这个包
3、然后在supplier-ticket这个模块下application.perproties文件中下面三条属性
spring.application.name=supplier-ticket
#路径是你主机的路径
dubbo.registry.address=zookeeper://192.168.3.108:2181
#扫描这个包下带@Service注解的类 注意这个包名很重要这里写什么消费者那边也要写什么
dubbo.scan.base-packages=com.vawu.service
4、启动supplier-ticket
6、把service包粘贴到consumer-user下 再次强调两个模块的包名必须相同不然无法分布式获取对象
7、再次在application.properties文件中写入下面的配置和上面一样只不过不用配置扫描包了
spring.application.name=supplier-ticket
#路径是你主机的路径
dubbo.registry.address=zookeeper://192.168.3.108:2181
8、在ConsumerUserApplicationTests测试类中写下面的代码,这个Reference别引错了还是dubbo包下的
运行结果
2019-03-17
Dubbo2.7用curator客户端从Zookeeper中获取对象
标签:apache 对象 rop work bom dea ati gem ESS
原文地址:https://www.cnblogs.com/vaHome/p/10547901.html