天天看点

跑通SpringBoot + dubbo + zookeeper

先安装并启动zookeeper

  1. 去zookeeper下载网站进行下载​​https://archive.apache.org/dist/zookeeper/​​
  2. 博主下载的是3.4.9
  3. 解压并启动
tar -zxvf apache-zookeeper-3.4.9.bin.tar.gz 
 mv apache-zookeeper-3.4.9 zookeeper-3.4.9
 
 cd zookeeper-3.4.9/conf
 ll      
跑通SpringBoot + dubbo + zookeeper

进入conf目录下后, 可以看到zoo_sample.cfg, 需要先复制一份,不然启动会报错找不到zoo.cfg

cp zoo_sample.cfg zoo.cfg      

然后修改一下zoo.cfg中的存储目录, 将默认的dataDir修改为zookeeper-3.4.9下面的data目录下, data需要新建

跑通SpringBoot + dubbo + zookeeper
cd ..
mkdir data
cd bin      

开始启动zookeeper服务端

./zkServer.sh start      

启动成功后会直接后台启动, 如下即为启动成功

跑通SpringBoot + dubbo + zookeeper

测试是否成功

启动zookeeper客户端

./zkCli.sh      

启动成功后输入, 看到zookeeper即为启动成功

ls /      
跑通SpringBoot + dubbo + zookeeper

博主因为启动了后面的dubbo-admin,所以有dubbo的目录,这不重要, 初次启动只要有zookeeper就行

安装dubbo-admin进行dubbo监控

安装dubbo-admin, 从github上clone​​duboo-admin​​

  1. 下载
git clone https://github.com/apache/dubbo-admin.git
cd dubbo-admin      

注意这里要先去ui里运行一下npm install

cd dubbo-admin-ui
npm install 

sudo npm install      
  1. 打包并启动

    回到dubbo-admin目录, 使用maven打包一下, 记得要跳过测试, 不然会很慢

mvn clean package -Dmaven.skip.test=ture      

进入​

​dubbo-amdin/dubbo-admin-server/src/main/resources 修改​

​​application.properties​

​, 新增​

​​server.port=8081`

这里必须要改一下端口号, 因为zookeeper启动会占用8080

然后启动

mvn --projects dubbo-admin-server spring-boot:run      

访问​

​locahost:8081​

​​

跑通SpringBoot + dubbo + zookeeper

登录, 账户和密码都是root, dubbo-admin默认的密码,可以在application.properties中进行修改

新建项目

创建一个多模块的项目, 格式如下:

分别为:

接口模块

提供者模块

消费者模块

创建统一接口

新建一个模块, pom设置格式​

​<packaging>jar</packaging>​

​​其他的什么jar都不需要引入,

跑通SpringBoot + dubbo + zookeeper

新建接口类,代码如下:

public interface SayHello {  
        
     public String sayHello(String name);  
}      

创建服务提供者

pom:

<properties>
  <dubbo.version>3.0.8dubbo.version>
properties>
<dependencies>
  <dependency>  
      <groupId>org.apache.dubbogroupId>  
      <artifactId>dubbo-spring-boot-starterartifactId>  
      <version>${dubbo.version}version>  
  dependency>  
    
  <dependency>  
      <groupId>org.apache.dubbogroupId>  
      <artifactId>dubbo-rpc-apiartifactId>  
      <version>${dubbo.version}version>  
  dependency>  
    
  <dependency>  
      <groupId>org.apache.dubbogroupId>  
      <artifactId>dubbo-rpc-dubboartifactId>  
      <version>${dubbo.version}version>  
  dependency>  
    
  <dependency>  
      <groupId>org.apache.dubbogroupId>  
      <artifactId>dubbo-registry-zookeeperartifactId>  
      <version>${dubbo.version}version>  
  dependency>  
    
  <dependency>  
      <groupId>org.apache.dubbogroupId>  
      <artifactId>dubbo-configcenter-zookeeperartifactId>  
      <version>${dubbo.version}version>  
  dependency>  
    
  <dependency>  
      <groupId>org.apache.dubbogroupId>  
      <artifactId>dubbo-metadata-report-zookeeperartifactId>  
      <version>${dubbo.version}version>  
  dependency>  
    
  <dependency>  
      <groupId>org.apache.dubbogroupId>  
      <artifactId>dubbo-config-springartifactId>  
      <version>${dubbo.version}version>  
  dependency>  
    
  <dependency>  
      <groupId>org.apache.dubbogroupId>  
      <artifactId>dubbo-remoting-netty4artifactId>  
      <version>${dubbo.version}version>  
  dependency>  
    
  <dependency>  
      <groupId>org.apache.dubbogroupId>  
      <artifactId>dubbo-serialization-hessian2artifactId>  
      <version>${dubbo.version}version>  
  dependency>
  
dependencies>      

yml配置文件:

dubbo:  
  application:  
  
    name: dubbo-test-provider  
  protocol:  
  
    name: dubbo  
    port: 20881  
  registry:  
  
    address: zookeeper://127.0.0.1:2181  
  config-center:  
    address: zookeeper://127.0.0.1:2181  
  metadata-report:  
    address: zookeeper://127.0.0.1:2181  
  scan:  
  
    base-packages: com.exceedy.dubbotest.dubbotestinterface.service      

消费者和提供者pom和配置文件(除服务名称外)均一致

实现

  1. 在启动类中增加​

    ​EnableDubbo​

    ​注解,然后让提供者进入等待状态
@SpringBootApplication  
@EnableDubbo  
public class ProviderApplication {  
  
    public static void main(String[] args) throws InterruptedException {  
        SpringApplication.run(ProviderApplication.class, args);  
        System.out.println("dubbo provider start");  
        new CountDownLatch(1).await();  
    }  
  
}      
  1. 实现SayService
// springBoot的注解
@Service  
// 版本更新到3.0.8后, dubbo原本的@Service标记为了删除, 所以使用新注解
// DubboService 意味着暴露服务
@DubboService  
public class SayHelloImpl implements SayHello {  
  
    @Override  
    public String sayHello(String name) {  
        System.out.println("Hello, " + name);  
        return "Hello, " + name;  
    }  
}      
  1. 启动后刷新dubbo-admin界面
    跑通SpringBoot + dubbo + zookeeper
    看到服务已经注册,说明提供者已经完成

创建服务消费者

@SpringBootApplication  
public class ConsumersApplication {  
  
    @DubboReference  
    SayHello sayHello;  
  
    public static void main(String[] args) {  
        ConfigurableApplicationContext run = SpringApplication.run(ConsumersApplication.class, args);  
        ConsumersApplication bean = run.getBean(ConsumersApplication.class);  
        String result = bean.doSayHi("张三");  
        System.out.println(result);  
  
    }  
  
    public String doSayHi(String name) {  
        return sayHello.sayHello(name);  
    }      

一些坑

  1. 不知道是否是3.0.8版本的原因, 在注解​

    ​@EnableDubbo(scanBasePackages = {"com.exceedy.dubbotest.dubbotestinterface.service"})​

    ​ 中加入扫描包启动后一直注册不了,只要在配置文件中配置了才能注册到注册中心
  2. 3.0.8版的dubbo只支持zookeeper3.4.x系列版本, 所以安装时要注意, 并且zookeeper3.4.x系列版本只支持jdk8, 所以jdk也要注意不能太高

    ps: 图片中用的是3.7.1版本,就是因为先安装的zookeeper, 不知道dubbo不支持,所以用成了3.7.1, 而想跑通就必须先安装zookeeper, 后面博主又换成了3.4.9, 唉,都是泪哇