创建一个服务提供者,直接使用springboot 项目就行
导入以来 POM
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.dubbo/dubbo-spring-boot-starter 这里是dubbo的启动器-->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.7.8</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.github.sgroschupf/zkclient -->
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
编写service 服务
HelloService 就一个方法就行
public interface HelloService {
String sayHello(String msg);
}
HelloServiceImpl
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Service;
//添加道spring容器
@Service
//提供注册的服务 后期会扫描到
@DubboService
public class HelloServiceImpl implements HelloService{
@Override
public String sayHello(String msg) {
return "HELLO " + msg;
}
}
yaml 文件
server:
port: 9001
dubbo:
# 提供注册的名字
application:
name: provider-service-9001
# 注册中心的地址
registry:
address: zookeeper://127.0.0.1:2181
# 提供注册的包名
scan:
base-packages: com/immortal/providerservice9001/service
这个之前的文章也讲过
这里缺少一个jar包 org/apache/curator/framework/CuratorFrameworkFactory
<!-- 导入一下-->
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>2.12.0</version>
</dependency>
重新启动
还是报错
百度了一下,导入的jar包之间有的地方不兼容,还缺少一些jar,最后找到能运行的依赖 如下:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.dubbo/dubbo-spring-boot-starter -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.7.8</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.github.sgroschupf/zkclient -->
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
<!-- 导入一下-->
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>4.2.0</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>4.2.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.zookeeper/zookeeper -->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.14</version>
</dependency>
等启动好 去dubbo-admin看一下
发现已经注册进来了
pom文件 和上面的一样就行
UserServiceImpl
import com.immortal.providerservice9001.service.HelloService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;
@Service
public class UserServiceImpl implements UserService{
@DubboReference //通过注册中心调用的
HelloService helloService;
@Override
public String hello() {
return helloService.sayHello("SpringBoot+Dubbo+Zookeeper");
}
}
yaml 文件
server:
port: 8001
dubbo:
application:
name: consumer-service-8001
registry:
address: zookeeper://127.0.0.1:2181
@RestController
public class HelloController {
@Autowired
UserService userService;
@GetMapping("hello")
public String hello(){
return userService.hello();
}
}
启动去访问 127.0.0.1:8001/hello
finish ok
小demo 坑非常多 大家慢慢踩
git 地址
https://gitee.com/immortal_mode/dubbo-zookeeper-springboot
原文:https://www.cnblogs.com/immortal-mode/p/14690392.html