tar -zxvf zookeeper-版本号.tar.gz1.3、解压后,在apache-zookeeper-版本号-binconf目录下,将zoo_sample.cfg文件复制一份改一个名字:zoo.cfg 1.4、修改zoo.cfg文件,一个存放数据,一个存放日志地址 1.5、启动zookeeper,双击apache-zookeeper-3.6.0-binbin目录下zkServer.cmd命令 2、下载安装dubbo组件 2.1、下载地址:https://gitee.com/kabuka/dubbohttps://gitee.com/kabuka/dubbo 2.2、下载完解压:
2.2.1、在dubbo-admin目录下,打开命令行,输入:mvn clean install,在target目录产生一个war包
2.2.2、将war包放入tomcat的webapps目录下,
2.2.3、在tomcat的bin目录下执行startup.bat启动tomcat
2.3、启动成功,在浏览器访问dubbo, localhost:端口/dubbo-admin-版本号/,登录用户和密码:root/root3、搭建项目 3.1、先创建一个空的maven项目 3.2、pom.xml
3.3、构建公共工程:common-api 3.3.1、新建一个maven模块:common-api4.0.0 org.springframework.boot spring-boot-starter-parent2.7.0 com.example dubbo-zookeeper0.0.1-SNAPSHOT dubbo-zookeeper Demo project for Spring Boot 1.8 org.springframework.boot spring-boot-starter-weborg.springframework.boot spring-boot-starter-testtest com.alibaba.boot dubbo-spring-boot-starter0.2.0 org.apache.zookeeper zookeeper3.4.8 org.slf4j slf4j-log4j12log4j log4jcom.101tec zkclient0.10 org.springframework.boot spring-boot-maven-plugin
3.3.2、pom.xml
3.3.3、创建一个HelloService接口:4.0.0 dubbo-zookeeper common-apicom.example 0.0.1-SNAPSHOT
public interface HelloService {
public String hello(String message);
}
3.4、构建server-provider
3.4.1、新建一个Maven模块,用于暴露Dubbo服务:server-provider
3.4.2、pom.xml(引入common-api模块)
3.4.3、创建一个HelloServiceImpl接口的实现类:4.0.0 server-provider com.example common-api0.0.1-SNAPSHOT com.example common-api${project.version}
@Service(interfaceClass = HelloService.class)
@Component
public class HelloServiceImpl implements HelloService {
@Override
public String hello(String message) {
System.out.println(">>>>>>>>>SUCCESS");
return "hello:"+message;
}
}
注:@Service注解为Dubbo提供的com.alibaba.dubbo.config.annotation.Service,不是Spring的那个。interfaceClass是指要发布服务的接口。
3.4.4、在application.yml中配置Dubbo:server:
port: 8080
dubbo:
application:
# 服务名称,保持唯一
name: server-provider
# zookeeper地址,用于向其注册服务
registry:
address: zookeeper://127.0.0.1:2181
#暴露服务方式
protocol:
# dubbo协议,固定写法
name: dubbo
# 暴露服务端口 (默认是20880,不同的服务提供者端口不能重复)
port: 20880
3.4.5、在启动类中我们加入@EnableDubbo注解,开启dubbo功能
@EnableDubbo
@SpringBootApplication
public class ProviderApplication {
public static void main(String[] args) {
SpringApplication.run(ProviderApplication.class, args);
}
}
3.5、构建Server-Consumer
3.5.1、新建一个Maven模块,用于消费Dubbo服务:server-consumer
3.5.2、pom.xml
3.5.5、启动类4.0.0 server-consumer com.example dubbo-zookeeper0.0.1-SNAPSHOT com.example common-api${project.version}
@EnableDubbo
@SpringBootApplication
public class ServerProviderApplication {
public static void main(String[] args) {
SpringApplication.run(ServerProviderApplication.class, args);
}
}
3.5.4、在application.yml中配置Dubbo:
server:
port: 8081
dubbo:
application:
# 服务名称,保持唯一
name: server-consumer
# zookeeper地址,用于从中获取注册的服务
registry:
address: zookeeper://127.0.0.1:2181
protocol:
# dubbo协议,固定写法
name: dubbo
3.5.5、controller
@RestController
public class HelloController {
@Reference
private HelloService helloService;
@GetMapping("/hello/{message}")
public String hello(@PathVariable String message) {
return this.helloService.hello(message);
}
}
注:这里一定要使用dubbo的@reference注解 ,不然消费者就不能注册到dubbo中,这里的@reference类似于Spring的@Autowired。
4、测试test
4.1、分别启动Server-Provider和Server-Consumer,访问http://localhost:端口号/hello/test
4.2、访问dubbo http://localhost:端口号/dubbo-admin-版本号/,在dubbo的页面看到服务的提供者和消费者,到此就搭建成功了



