4.0.0
com.itheima
dubbo-spring-boot-demo
1.0-SNAPSHOT
3.2.0-beta.4
2.7.8
17
17
UTF-8
org.springframework.boot
spring-boot-dependencies
${spring-boot.version}
pom
import
org.apache.dubbo
dubbo-bom
${dubbo.version}
pom
import
org.apache.dubbo
dubbo-dependencies-zookeeper-curator5
${dubbo.version}
pom
org.springframework.boot
spring-boot-maven-plugin
${spring-boot.version}
4.0.0
com.itheima
dubbo-spring-boot-demo
1.0-SNAPSHOT
../pom.xml
com.itheima
dubbo-spring-boot-demo-consumer
1.0-SNAPSHOT
war
com.itheima
dubbo-spring-boot-demo-interface
0.0.1-SNAPSHOT
org.apache.dubbo
dubbo-spring-boot-starter
org.apache.dubbo
dubbo-dependencies-zookeeper-curator5
pom
slf4j-reload4j
org.slf4j
org.springframework.boot
spring-boot-starter-web
4.0.0
com.itheima
dubbo-spring-boot-demo
1.0-SNAPSHOT
../pom.xml
com.itheima
dubbo-spring-boot-demo-provider
1.0-SNAPSHOT
war
com.itheima
dubbo-spring-boot-demo-interface
0.0.1-SNAPSHOT
org.apache.dubbo
dubbo-spring-boot-starter
org.apache.dubbo
dubbo-dependencies-zookeeper-curator5
pom
slf4j-reload4j
org.slf4j
org.springframework.boot
spring-boot-starter
4.0.0
com.itheima
dubbo-spring-boot-demo-interface
0.0.1-SNAPSHOT
参考https://blog.csdn.net/qq_41428418/article/details/133786336
参考https://blog.csdn.net/qq_41428418/article/details/133857185
server:
port: 9000
dubbo:
application:
name: dubbo-provider
protocol:
name: dubbo
port: -1
registry:
address: zookeeper://${zookeeper.address:192.168.56.10}:2181
要在启动类加上@EnableDubbo注解
server:
port: 8000
dubbo:
application:
name: dubbo-consumer
qos-port: 33333
protocol:
name: dubbo
port: -1
registry:
address: zookeeper://${zookeeper.address:192.168.56.10}:2181
消费者如果不提供服务,也就是不把接口注册到注册中心时,可以不添加@EnableDubbo注解
package com.itheima.service;
public interface UserService {
public String sayHello();
}
在dubbo-provider中实现dubbo-interface中创建的UserService
package com.itheima.service.impl;
import com.itheima.service.UserService;
import org.apache.dubbo.config.annotation.DubboService;
import org.apache.dubbo.config.annotation.Service;
//@Service定义bean
//将这个类提供的方法〈服务)对外发布。将访问的地址ip,端口,路径注册到注册中心中
@DubboService
public class UserServiceImpl implements UserService {
@Override
public String sayHello() {
return "hello dubbo!~welcome!";
}
}
在dubbo-consumer中创建一个controller,并引用UserService。
package com.itheima.controller;
import com.itheima.service.UserService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/user")
public class UserController {
//1.从zookeeper注册中心获取userService的访问urL
//2.进行远程调用RPC
//3. 将结果封装为一个代理对象,给变量赋值
@DubboReference
private UserService userService;
@GetMapping("/sayHello")
public String sayHello() {
return userService.sayHello();
}
}
1、执行docker ps
查看zookeeper容器状态,复制zookeeper对应的 CONTAINER ID
2、执行docker exec -it ${CONTAINER ID} /bin/bash
3、进入后ls查看目录,执行cd bin
进入bin目录
4、执行./zkCli.sh
脚本,连接zookeeper客户端
5、执行ls / 查看目录 我们可以看到,现在只有一个zookeeper
1、服务启动后,再执行ls /查看内容可以看到多了一个dubbo
2、执行ls /dubbo 查看,可以看到我们注册的接口的路径
3、继续查看,可以看还存在consumer和provider,查看consumer和provider,可以看到具体的注册中心,到这里说明我们已经注册完成了
官方中文文档