SpringBoot 整合Dubbo
1.搭建Zookeeper环境
进入zookeeper官网
点击download,选择zookeeper下载,解压。
进入解压好的文件找到conf文件夹。
复制zoo_sample将其重命名zoo
修改zookeeper存储数据的路径dataDir,
可以在zookeeper文件夹里面新建一个data文件夹,将dataDir的值修改成data的路径
双击bin目录下的zkServer.cmd启动zookeeper
2.搭建dubbo监控环境
进入dubbo官网,选择github找到dubbo admin,将其clone下来,解压
将dubbo-admin使用maven打包
打包好之后在dubbo-admin下的target里会有一个jar包,启动cmd 使用
java -jar dubbo-admin-0.0.1-SNAPSHOT.jar将控制台启动
浏览器访问localhost:7001
账号,密码默认都是: root
3.创建一个maven工程将接口写在该项目里
4.创建一个服务提供者,将存放接口的项目依赖导入进来
idea 在服务提供者项目的pom文件使用alt + ins 选择创建的接口依赖
进入dubbo官网,选择dubbo Spring Boot,找到dubbo启动器将其复制到pom文件里
2020-06-21 09:47:01.589 ERROR 17288 --- [ main] o.s.boot.SpringApplication : Application run failed
java.lang.NoClassDefFoundError: org/apache/curator/framework/CuratorFrameworkFactory
at org.apache.dubbo.remoting.zookeeper.curator.CuratorZookeeperClient.<init>(CuratorZookeeperClient.java:66) ~[dubbo-2.7.7.jar:2.7.7]
at org.apache.dubbo.remoting.zookeeper.curator.CuratorZookeeperTransporter.createZookeeperClient(CuratorZookeeperTransporter.java:26) ~[dubbo-2.7.7.jar:2.7.7]
at org.apache.dubbo.remoting.zookeeper.support.AbstractZookeeperTransporter.connect(AbstractZookeeperTransporter.java:70) ~[dubbo-2.7.7.jar:2.7.7]
at org.apache.dubbo.remoting.zookeeper.ZookeeperTransporter$Adaptive.connect(ZookeeperTransporter$Adaptive.java) ~[dubbo-2.7.7.jar:2.7.7]
at org.apache.dubbo.configcenter.support.zookeeper.ZookeeperDynamicConfiguration.<init>(ZookeeperDynamicConfiguration.java:70) ~[dubbo-2.7.7.jar:2.7.7]
at org.apache.dubbo.configcenter.support.zookeeper.ZookeeperDynamicConfigurationFactory.createDynamicConfiguration(ZookeeperDynamicConfigurationFactory.java:37) ~[dubbo-2.7.7.jar:2.7.7]
还需要导入curator依赖否则会出现上面的错误
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>2.13.0</version>
</dependency>
注:使用zookeeper 作为注册中心,需要操作zookeeper
dubbo2.6以前的版本使用zkclient;
dubbo2.6(包括)以后的版本使用curator
因为使用的dubbo版本是2.7.7故使用curator操作zookeeper
在application.properties 配置dubbo相关信息
#服务名称
dubbo.application.name=boot-user-service-provider
#注册中心位置
dubbo.registry.address=zookeeper://127.0.0.1:2181
#通信规则协议
dubbo.protocol.name=dubbo
#通信端口
dubbo.protocol.port=20880
使用@DubboService暴露服务
import com.guiming.gmall.bean.User;
import com.guiming.gmall.service.UserService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.List;
@DubboService
@Component
public class UserServiceImpl implements UserService {
@Override
public List<User> getUserAddress(int userId) {
User user1 = new User(1,1,"xxxxxxxxxx","小明","xxxxxxxxxxx");
User user2 = new User(2,1,"xxxxxxxxxx","小明","xxxxxxxxxxx");
List<User> users = new ArrayList<>();
users.add(user1);
users.add(user2);
return users;
}
}
使用@EnableDubbo开启基于注解的dubbo功能
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@EnableDubbo
@SpringBootApplication
public class BootUserServiceProviderApplication {
public static void main(String[] args) {
SpringApplication.run(BootUserServiceProviderApplication.class, args);
}
}
5. 创建服务消费者
创建一个spring boot的web项目
在pom文件也添加上和服务提供者相同的依赖
在application.properties 配置dubbo相关信息
dubbo.application.name=boot-order-service-consumer
dubbo.registry.address=zookeeper://127.0.0.1:2181
使用@DubboReference远程调用服务
import com.guiming.gmall.bean.User;
import com.guiming.gmall.service.OrderService;
import com.guiming.gmall.service.UserService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class OrderServiceImpl implements OrderService {
@DubboReference
UserService userService;
@Override
public List<User> initOrder(int userId) {
return userService.getUserAddress(userId);
}
}
使用@EnableDubbo开启基于注解的dubbo功能
在服务消费者工程里写一个controller用于测试
import com.guiming.gmall.bean.User;
import com.guiming.gmall.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class initController {
@Autowired
OrderService orderService;
@RequestMapping("init")
public List<User> initUser(@RequestParam int userId){
return orderService.initOrder(userId);
}
}
启动服务提供者工程
启动服务消费者工程
访问controller中处理的请求
查看控制台