springboot整合dubbo\zookeeper做注册中心
springboot整合dubbo发布服务,zookeeper做注册中心。前期的安装zookeeper以及启动zookeeper集群就不说了。
dubbo-admin-2.5.4.war:dubbo服务管理项目,下载完后部署到tomcat即可查看(登录的用户名和密码默认都是root)。
pom文件引入dubbo以及zkcli包:
<!-- 引入dubbo-spring-boot-starter以及zkclient依赖 --> <dependency> <groupId>com.alibaba.spring.boot</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>2.0.0</version> </dependency> <dependency> <groupId>com.101tec</groupId> <artifactId>zkclient</artifactId> <version>0.9</version> </dependency>
1.dubbo服务端(提供dubbo服务)
目录结构如下:
配置文件如下
application.properties
############################################################
#
# DUBBO相关配置
#
############################################################
#当前服务/应用名称
spring.dubbo.application.name=provider
#注册中心的协议和地址
#spring.dubbo.registry.address=127.0.0.1:2181
#spring.dubbo.registry.protocol=zookeeper
spring.dubbo.server=true
spring.dubbo.registry=zookeeper://127.0.0.1:2181
#通信规则(通信协议和接口)
spring.dubbo.protocol.name=dubbo
spring.dubbo.protocol.port=20880
User.java
package org.springboot.dubbo.bean; import java.io.Serializable; public class User implements Serializable { private String userName; private String password; public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public User(String userName, String password) { super(); this.userName = userName; this.password = password; } }
UserService.java
package org.springboot.dubbo.service; import java.util.List; import org.springboot.dubbo.bean.User; public interface UserService { List<User> getAllUsers(); User getUserByUserName(String username); }
UserServiceImpl.java (注解发布服务,注意@Service注解是dubbo提供的)
package org.springboot.dubbo.service; import java.util.ArrayList; import java.util.List; import org.springboot.dubbo.bean.User; import org.springframework.stereotype.Component; import com.alibaba.dubbo.config.annotation.Service; @Service(version = "1.0.0") // dubbo的service注解,不具备spring的@service注解的功能 @Component public class UserServiceImpl implements UserService { public List<User> getAllUsers() { List<User> users = new ArrayList<User>(); for (int i = 0; i < 20; i++) { User user = new User("usernnamei" + i, "password" + i); users.add(user); } return users; } public User getUserByUserName(String username) { return new User(username, username); } }
应用启动类:
package org.springboot.dubbo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import com.alibaba.dubbo.spring.boot.annotation.EnableDubboConfiguration; @SpringBootApplication @EnableDubboConfiguration public class MySpringBootApplication { public static void main(String[] args) throws InterruptedException { // 入口运行类 SpringApplication.run(MySpringBootApplication.class, args); Thread.sleep(50 * 1000); } }
启动之后查看dubbo管理界面:
2.dubbo客户端(使用dubbo提供的服务)
目录结构:
application.properties
############################################################
#
# DUBBO相关配置
#
############################################################
#当前服务/应用名称
spring.dubbo.application.name=consumer
#注册中心的协议和地址
spring.dubbo.server=true
spring.dubbo.registry=zookeeper://127.0.0.1:2181
#通信规则(通信协议和接口)
spring.dubbo.protocol.name=dubbo
spring.dubbo.protocol.port=20880
spring.dubbo.scan=org.springboot.dubbo
UserController.java(通过@Reference消费服务)
package org.springboot.dubbo.controller; import java.util.List; import org.springboot.dubbo.bean.User; import org.springboot.dubbo.service.UserService; import org.springframework.stereotype.Controller; import com.alibaba.dubbo.config.annotation.Reference; @Controller public class UserController { @Reference(version = "1.0.0") UserService userService; public List<User> getAllUsers() { return userService.getAllUsers(); } public User getUserByUserName(String username) { return userService.getUserByUserName(username); } }
结果:
[org.springboot.dubbo.bean.User@31741aad, org.springboot.dubbo.bean.User@3d836d38, org.springboot.dubbo.bean.User@7f779714, org.springboot.dubbo.bean.User@45fe36d, org.springboot.dubbo.bean.User@334c0b59, org.springboot.dubbo.bean.User@457d1a5e, org.springboot.dubbo.bean.User@184e34f6, org.springboot.dubbo.bean.User@36770ef1, org.springboot.dubbo.bean.User@42760c26, org.springboot.dubbo.bean.User@5417b4e0, org.springboot.dubbo.bean.User@60640941, org.springboot.dubbo.bean.User@479bbbd7, org.springboot.dubbo.bean.User@414944fb, org.springboot.dubbo.bean.User@314fdc39, org.springboot.dubbo.bean.User@72033b79, org.springboot.dubbo.bean.User@3235bf69, org.springboot.dubbo.bean.User@66a9a03f, org.springboot.dubbo.bean.User@a57bcc1, org.springboot.dubbo.bean.User@6292835, org.springboot.dubbo.bean.User@780eb2bb]
org.springboot.dubbo.bean.User@605dfc60
管理界面查看如下:
至此基本完成了简单的dubbo环境搭建。
git地址: https://github.com/qiao-zhi/springboot