SpringBoot + Dubbo + zookeeper整合

文章目录

  • 狂神说
  • 1. 创建项目
    • 1.1 创建一个模块,实现服务提供者:provider-server
      • 1.1.1 添加依赖
      • 1.1.2 在springboot配置文件中配置dubbo相关属性!
      • 1.1.3 编写服务提供者类添加功能
    • 1.2 创建一个模块,实现服务消费者:consumer-server
      • 1.2.1 导入依赖
      • 1.2.2 配置参数
      • 1.2.3 编写服务消费者类
    • 1.3 测试

狂神说

https://mp.weixin.qq.com/s?__biz=Mzg2NTAzMTExNg==&mid=2247483947&idx=1&sn=0c8efabbaf9b8ca835d862e6e0a2254f&scene=19#wechat_redirect

1. 创建项目

创建一个空项目

1.1 创建一个模块,实现服务提供者:provider-server

1.1.1 添加依赖

dubbo-springboot


<dependency>
   <groupId>org.apache.dubbogroupId>
   <artifactId>dubbo-spring-boot-starterartifactId>
   <version>2.7.3version>
dependency>    

zookeeper


<dependency>
   <groupId>com.github.sgroschupfgroupId>
   <artifactId>zkclientartifactId>
   <version>0.1version>
dependency>

【新版的坑】zookeeper及其依赖包,解决日志冲突,还需要剔除日志依赖;


<dependency>
   <groupId>org.apache.curatorgroupId>
   <artifactId>curator-frameworkartifactId>
   <version>2.12.0version>
dependency>
<dependency>
   <groupId>org.apache.curatorgroupId>
   <artifactId>curator-recipesartifactId>
   <version>2.12.0version>
dependency>
<dependency>
   <groupId>org.apache.zookeepergroupId>
   <artifactId>zookeeperartifactId>
   <version>3.4.14version>
   
   <exclusions>
       <exclusion>
           <groupId>org.slf4jgroupId>
           <artifactId>slf4j-log4j12artifactId>
       exclusion>
   exclusions>
dependency>

1.1.2 在springboot配置文件中配置dubbo相关属性!

#当前应用名字
dubbo.application.name=provider-server
#注册中心地址
dubbo.registry.address=zookeeper://127.0.0.1:2181
#扫描指定包下服务
dubbo.scan.base-packages=com.kuang.provider.service

1.1.3 编写服务提供者类添加功能

编写接口

package com.kuang.provider.service;

public interface TicketService {
   public String getTicket();
}

编写实现类

import org.apache.dubbo.config.annotation.Service;
import org.springframework.stereotype.Component;

@Service //将服务发布出去
@Component //放在容器中
public class TicketServiceImpl implements TicketService {
   @Override
   public String getTicket() {
       return "《Java》";
  }
}

1.2 创建一个模块,实现服务消费者:consumer-server

1.2.1 导入依赖



<dependency>
   <groupId>org.apache.dubbogroupId>
   <artifactId>dubbo-spring-boot-starterartifactId>
   <version>2.7.3version>
dependency>


<dependency>
   <groupId>com.github.sgroschupfgroupId>
   <artifactId>zkclientartifactId>
   <version>0.1version>
dependency>

<dependency>
   <groupId>org.apache.curatorgroupId>
   <artifactId>curator-frameworkartifactId>
   <version>2.12.0version>
dependency>
<dependency>
   <groupId>org.apache.curatorgroupId>
   <artifactId>curator-recipesartifactId>
   <version>2.12.0version>
dependency>
<dependency>
   <groupId>org.apache.zookeepergroupId>
   <artifactId>zookeeperartifactId>
   <version>3.4.14version>
   
   <exclusions>
       <exclusion>
           <groupId>org.slf4jgroupId>
           <artifactId>slf4j-log4j12artifactId>
       exclusion>
   exclusions>
dependency>

1.2.2 配置参数

#当前应用名字
dubbo.application.name=consumer-server
#注册中心地址
dubbo.registry.address=zookeeper://127.0.0.1:2181

正常步骤是需要将服务提供者的接口打包,然后用pom文件导入,我们这里使用简单的方式,直接将服务的接口拿过来,路径必须保证正确,即和服务提供者相同;

1.2.3 编写服务消费者类

编写service

package com.kuang.consumer.service;

import com.kuang.provider.service.TicketService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Service;

@Service //注入到容器中
public class UserService {

   @Reference //远程引用指定的服务,他会按照全类名进行匹配,看谁给注册中心注册了这个全类名
   TicketService ticketService;

   public void bugTicket(){
       String ticket = ticketService.getTicket();
       System.out.println("在注册中心买到"+ticket);
  }

}

1.3 测试

开启zookeeper

@RunWith(SpringRunner.class)
@SpringBootTest
public class ConsumerServerApplicationTests {

   @Autowired
   UserService userService;

   @Test
   public void contextLoads() {

       userService.bugTicket();

  }

}

你可能感兴趣的:(分布式,spring,boot学习,zookeeper,spring,boot,java)