整体架构:
下载链接:
Doubbo:https://github.com/apache/dubbo
Zookeeper:https://downloads.apache.org/zookeeper/
Doubbo-admin:https://github.com/apache/dubbo-admin
1 首先安装Zookpeer
[root@bogon tmp]# ls apache-zookeeper-3.7.0-bin.tar.gz jdk1.8.0_141 jdk8.tar.gz [root@bogon tmp]# tar -zxvf apache-zookeeper-3.7.0-bin.tar.gz [root@bogon tmp]# tar -zxvf jdk8.tar.gz [root@bogon tmp]# mv jdk1.8.0_141/ /usr/local/ [root@bogon tmp]# mv apache-zookeeper-3.7.0-bin/ /usr/local/zookpeer [root@bogon tmp]# cd /usr/local/ [root@bogon local]# ls bin etc games include jdk1.8.0_141 lib lib64 libexec sbin share src zookeeper [root@bogon local]# vim /etc/profile # --------写入文件------------ export JAVA_HOME=/usr/local/jdk1.8.0_141 export JER_HOME=${JAVA_HOME}/jre export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib:$CLASSPATH export JAVA_PATH=${JAVA_HOME}/bin:${JRE_HOME}/bin export PATH=$PATH:${JAVA_PATH} # --------写入文件完成------------ [root@bogon local]# source /etc/profile [root@bogon local]# java -version java version "1.8.0_141" Java(TM) SE Runtime Environment (build 1.8.0_141-b15) Java HotSpot(TM) 64-Bit Server VM (build 25.141-b15, mixed mode) [root@bogon local]# cd zookeeper/ [root@bogon conf]# cp zoo_sample.cfg zoo.cfg [root@bogon conf]# cd ../bin [root@bogon bin]# ./zkServer.sh start [root@bogon bin]# ./zkServer.sh start ZooKeeper JMX enabled by default Using config: /usr/local/zookeeper/bin/../conf/zoo.cfg Starting zookeeper ... STARTED [root@bogon bin]# ./zkCli.sh Connecting to localhost:2181 ...... [zk: localhost:2181(CONNECTED) 1] ls / [zookeeper]
zookpeer的启动和重启命令:
[root@bogon bin]# ./zkServer.sh start //启动 [root@bogon bin]# ./zkServer.sh stop //停止 [root@bogon bin]# ./zkServer.sh status //查看当前状态 [root@bogon bin]# ./zkServer.sh restart //重启 [root@bogon bin]# ./zkServer.sh version //查看当前版本
2 创建Spring Boot项目
2.1 服务提供方dubbo_provider
pom.xml依赖:
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>com.alibaba.boot</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>0.2.0</version> </dependency> <dependency> <groupId>org.ymx</groupId> <artifactId>doubbo_interface</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies>
application.properties配置文件:
server.port=8081 dubbo.application.name=my_provider dubbo.registry.protocol=zookeeper dubbo.registry.address=192.168.190.129:2181 dubbo.protocol.name=dubbo
UserServiceImpl接口实现类:
import com.alibaba.dubbo.config.annotation.Service; import org.ymx.doubbo_interface.service.UserService; /** * @desc: 接口实现类 * @author: YanMingXin * @create: 2021/8/7-9:32 **/ @Service public class UserServiceImpl implements UserService { @Override public String sayHello(String name) { return "Hello " + name; } }
主启动类:
@SpringBootApplication @EnableDubbo(scanBasePackages = "org.ymx.*") public class DoubboProviderApplication { public static void main(String[] args) { SpringApplication.run(DoubboProviderApplication.class, args); } }
2.2 服务消费方dubbo_consumer
pom.xml依赖
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>com.alibaba.boot</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>0.2.0</version> </dependency> <dependency> <groupId>org.ymx</groupId> <artifactId>doubbo_interface</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies>
application.properties配置文件
dubbo.application.name=my_consumer dubbo.registry.protocol=zookeeper dubbo.registry.address=192.168.190.129:2181 dubbo.protocol.name=dubbo
UserController控制器类
/** * @desc: Controller控制类 * @author: YanMingXin * @create: 2021/8/7-10:08 **/ @RestController public class UserController { @Reference private UserService userService; @RequestMapping("/hello/{name}") public String hello(@PathVariable("name") String name) { return userService.sayHello(name); } }
主启动类:
@SpringBootApplication @EnableDubbo(scanBasePackages = "org.ymx.*") public class DoubboConsumerApplication { public static void main(String[] args) { SpringApplication.run(DoubboConsumerApplication.class, args); } }
2.3 公共接口dubbo_interface
pom.xml依赖
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies>
application.properties配置文件
空
UserService接口类
/** * @desc: 公共接口 * @author: YanMingXin * @create: 2021/8/7-10:07 **/ public interface UserService { String sayHello(String name); }
主启动类:
@SpringBootApplication public class DoubboInterfaceApplication { public static void main(String[] args) { SpringApplication.run(DoubboInterfaceApplication.class, args); } }
@SpringBootApplication public class DoubboInterfaceApplication { public static void main(String[] args) { SpringApplication.run(DoubboInterfaceApplication.class, args); } }
2.4 启动后观察Zookpeer并测试
doubbo_interface不需要启动,只需要install就可以了
[zk: localhost:2181(CONNECTED) 14] ls / [dubbo, zookeeper] [zk: localhost:2181(CONNECTED) 15] ls /dubbo [org.ymx.doubbo_consumer.service.UserService, org.ymx.doubbo_provider.service.UserService]
测试:
3 dubbo管理界面
放到tomcat中
修改配置文件,在
修改内容
dubbo.registry.address=zookeeper://192.168.190.129:2181 # 注册中心地址 dubbo.admin.root.password=root # root账户的密码 dubbo.admin.guest.password=guest # guest账户的密码
启动
访问:http://localhost:8080/dubbo-admin/