概述
Dubbo是一个分布式服务框架,用于多个系统间的RPC相互调用
Docker安装Zookeeper
# 1、查看镜像
docker search zookeeper
# 2、拉取镜像
docker pull zookeeper
# 3、查看镜像
docker images
# 4、运行容器
# 命令限制它的内存大小,并映射端口2181到本地
docker run -d --name myZookeeper --restart always -e JVMFLAGS="-Xmx1024m" -p 2181:2181 zookeeper
# 5、查看容器是否运行
docker ps
# 6、进入容器
docker exec -it myZookeeper /bin/bash
# 7、进入到bin目录下,连接zkClinet.sh
cd bin
zkClinet.sh
项目搭建
父模块搭建
pom.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.buddha</groupId>
<artifactId>springboot-dubbo-zookeeper</artifactId>
<version>1.0.0</version>
<modules>
<module>provider</module>
<module>consumer</module>
<module>common</module>
</modules>
<packaging>pom</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.1.RELEASE</version>
<relativePath/>
</parent>
<!-- 统一管理jar包版本 -->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<dubbo-boot.version>3.0.4</dubbo-boot.version>
<zkclient.version>4.2.0</zkclient.version>
</properties>
<dependencyManagement>
<dependencies>
<!-- dubbo-spring-boot依赖 -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>${dubbo-boot.version}</version>
</dependency>
<!-- zookeeper客户端(curator-framework curator-recipes都含有curator-x-discovery包-->
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-x-discovery</artifactId>
<version>${zkclient.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
公共common子模块
pom.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>springboot-dubbo-zookeeper</artifactId>
<groupId>com.buddha</groupId>
<version>1.0.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>common</artifactId>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
service接口文件
package com.buddha.service;
public interface User {
String getName(String name);
}
消费者provider子模块
pom.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>springboot-dubbo-zookeeper</artifactId>
<groupId>com.buddha</groupId>
<version>1.0.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>provider</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-x-discovery</artifactId>
</dependency>
<dependency>
<groupId>com.buddha</groupId>
<artifactId>common</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
application.yml配置文件
server:
port: 8080
dubbo:
application:
name: provider
registry:
address: zookeeper://192.168.123.199:2181
protocol:
name: dubbo
port: 20880
scan:
base-packages: com.buddha.service
主程序main类
package com.buddha;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@EnableDubbo
public class ProviderMain8080 {
public static void main(String[] args) {
SpringApplication.run(ProviderMain8080.class, args);
}
}
service实现类
package com.buddha.Service;
import com.buddha.service.User;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Component;
@Component
@DubboService
public class UserImpl implements User {
@Override
public String getName(String name) {
return "hello " + name;
}
}
消费者consumer子模块
pom.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>springboot-dubbo-zookeeper</artifactId>
<groupId>com.buddha</groupId>
<version>1.0.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>consumer</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-x-discovery</artifactId>
</dependency>
<dependency>
<groupId>com.buddha</groupId>
<artifactId>common</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
application.yml配置文件
server:
port: 80
dubbo:
application:
name: consumer
registry:
address: zookeeper://192.168.123.199:2181
protocol:
name: dubbo
port: 20880
main主类
package com.buddha;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@EnableDubbo
public class ConsumerMain80 {
public static void main(String[] args) {
SpringApplication.run(ConsumerMain80.class, args);
}
}
controller类
package com.buddha.controller;
import com.buddha.service.User;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class SiteController {
@DubboReference
private User user;
@RequestMapping("/user")
public String test(@RequestParam("name") String name) {
return user.getName(name);
}
}
从而实现了从一个系统到另外一个系统RPC调用的DEMO
最后
以上就是隐形海燕为你收集整理的SpringBoot整合Dubbo3.0+Zookeeper项目搭建的全部内容,希望文章能够帮你解决SpringBoot整合Dubbo3.0+Zookeeper项目搭建所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复