搭建springboot+zookeeper+dubbo
作者:互联网
一 搭建工程的初衷
之所以再简单的搭建一次dubbo+zookeeper主要是因为很久没有用到过了,都已经忘记是怎么使用的了。想再次学习记录一下。
二 启动zookeeper,搭建工程
1、项目目录总览
2、父工程中的pom
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.2.RELEASE</version> <relativePath /> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> <modules> <module>yk-dubbo-Service</module> <module>yk-dubbo-consumer</module> <module>yk-dubbo-API</module> </modules>
3、API工程
此工程是对外提供RPC接口的工程,写一个接口类供演示使用
package com.yk.service; public interface HelloDubboService { public String hello(String text); }
3、服务提供者工程
- pom配置
<dependency> <groupId>com.yk</groupId> <artifactId>yk-dubbo-API</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> <dependency> <groupId>com.alibaba.boot</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>0.2.0</version> </dependency> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4.13</version> </dependency>
- YML配置
#Dubbo 服务提供者配置 spring: application: name: provider server: port: 8081 dubbo: application: name: provider registry: # zookeeper 注册中心地址 address: zookeeper://192.168.1.123:2181 protocol: name: dubbo # dubbo端口 port: 20880
- springboot入口文件配置
@EnableDubbo //开启dubbo服务 @SpringBootApplication(exclude = DataSourceAutoConfiguration.class) public class DubboServiceApplication { public static void main(String[] args) { SpringApplication.run(DubboServiceApplication.class, args); } }
- serviceImpl代码
import java.util.Date; import com.alibaba.dubbo.config.annotation.Service; //使用dubbo的 import com.yk.service.HelloDubboService; @Service(version = "helloDubboService") public class HelloDubboServiceImpl implements HelloDubboService { @Override public String hello(String name) { return "于"+new Date() + ",发送者:" + name; } }
4、服务消费者工程
- pom
<dependency> <groupId>com.yk</groupId> <artifactId>Springboot-dubbo-API</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> <dependency> <groupId>com.alibaba.boot</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>0.2.0</version> </dependency> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4.13</version> </dependency>
- YML配置与Springboot入口文件和上面的配置一样,启动的端口号更改一下。
- controller代码
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.alibaba.dubbo.config.annotation.Reference; import com.yk.service.HelloDubboService; @RestController public class DubboController { //@Reference 使用dubbo的 @Reference(version = "helloDubboService") private HelloDubboService h; @RequestMapping("/") public String demo() { return h.hello("张三"); } }
五、启动zookeeper ,启动消费者和提供者
这样一个简单的使用zookeeper+dubbo就可以成功了
三 使用dobbo-Admin查看服务的提供者和消费者
将下载好的dubbo-admin-2.6.0 .war扔进tomcat里面解压出来,更改文件名为dubbo
修改dubbo/WEB-INF/dubbo.properties
dubbo.registry.address=zookeeper://192.168.1.123:2181 //配置Zookeeper中心地址,集群时填写集群地址 dubbo.admin.root.password=root //客户端默认用户名为root dubbo.admin.guest.password=root //客户端密码
四 总结
这个demo搭建的非常简陋,主要也是记录一下大致的过程。后续也会深入学习
标签:dubbo,yk,springboot,zookeeper,boot,com,public 来源: https://www.cnblogs.com/yangk1996/p/10843007.html