dubbo+zookeeper下载安装
下载zookeeper:
下载地址
解压,并进入bin目录,启动
如果闪退可以编辑脚本,在指定位置加上暂停脚本
报错内容说没有conf/zoo.cfg,就复制zoo_sample.cfg重命名为zoo.cfg
再次启动脚本,启动成功。
dubbo-admin控制台在github:用于检测zookeeper的注册情况
github地址
选择稳定版的
直接下载
解压,idea打开,读readme,进行环境配置
cmd执行:
构建
mvn clean package -Dmaven.test.skip=true
运行
mvn --projects dubbo-admin-server spring-boot:run
根据提示访问:http://localhost:38080
账号密码:root/root
DEMO展示
编程入门:
创建了两个springboot项目作为生产者消费者进行演示:
java1.8
springboot2.7.10
maven3.9.6
spring-boot-starter-web
生产者
pom.xml配置文件
<!-- 导入dubbo+zookeeper的依赖-->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>3.3.0-beta.2</version>
</dependency>
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
<!-- zookeeper的日志和springboot的日志会冲突-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>4.3.0</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>4.3.0</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-x-discovery</artifactId>
<version>4.3.0</version>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.9.2</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
</exclusions>
</dependency>
application.properties配置文件
spring.application.name=provider
server.port=8001
# zk 注册
dubbo.application.name=dubbo-provider
dubbo.registry.address=zookeeper://127.0.0.1:2181
dubbo.scan.base-packages=com.rong.service
代码
package com.rong.service;
public interface TicketService {
public String getTicket();
}
package com.rong.service;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Component;
@DubboService
@Component
public class TicketServiceImpl implements TicketService{
@Override
public String getTicket() {
return "卖票+i";
}
}
消费者
pom.xml
<!-- 导入dubbo+zookeeper的依赖-->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>3.3.0-beta.2</version>
</dependency>
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
<!-- zookeeper的日志和springboot的日志会冲突-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>4.3.0</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>4.3.0</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-x-discovery</artifactId>
<version>4.3.0</version>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.9.2</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
</exclusions>
</dependency>
application.properties
spring.application.name=consumer
server.port=8002
# zk 消费
dubbo.application.name=dubbo-consumer
dubbo.registry.address=zookeeper://127.0.0.1:2181
dubbo.application.qos-port=22221
代码
接口
package com.rong.service;
public interface TicketService {
public String getTicket();
}
消费者类
package com.rong.service;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Component;
@Component
public class UserService {
@DubboReference
TicketService TICKET_SERVICE;
public void buyTicket(){
String ticket = TICKET_SERVICE.getTicket();
System.out.println("comsumer"+"::zookeeper::"+ticket);
}
}
springboot测试类
package com.rong;
import com.rong.service.UserService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class ConsumerApplicationTests {
@Autowired
UserService userService;
@Test
void contextLoads() {
userService.buyTicket();
}
}
附加一:
pom.xml依赖内容可由
<!-- 导入dubbo+zookeeper的依赖-->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>3.3.0-beta.2</version>
</dependency>
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
<!-- zookeeper的日志和springboot的日志会冲突-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>4.3.0</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>4.3.0</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-x-discovery</artifactId>
<version>4.3.0</version>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.9.2</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
</exclusions>
</dependency>
替换为
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-dependencies-zookeeper</artifactId>
<version>3.2.10</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>3.2.10</version>
</dependency>
附加二:
dubbo项目创建可以从官网创建:
官网地址
根据具体情况选择版本,并生成
程序可直接使用(api模块分离形式请参考生成项目)