Dubbo是一个分布式服务框架,用于多个系统间的RPC相互调用
# 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文件
公共common子模块4.0.0 com.buddha springboot-dubbo-zookeeper 1.0.0 provider consumer common pom org.springframework.boot spring-boot-starter-parent 2.3.1.RELEASE UTF-8 1.8 1.8 3.0.4 4.2.0 org.apache.dubbo dubbo-spring-boot-starter ${dubbo-boot.version} org.apache.curator curator-x-discovery ${zkclient.version} org.springframework.boot spring-boot-maven-plugin
pom.xml文件
springboot-dubbo-zookeeper com.buddha 1.0.0 4.0.0 common org.springframework.boot spring-boot-maven-plugin
service接口文件
package com.buddha.service;
public interface User {
String getName(String name);
}
消费者provider子模块
pom.xml文件
springboot-dubbo-zookeeper com.buddha 1.0.0 4.0.0 provider org.springframework.boot spring-boot-starter org.apache.dubbo dubbo-spring-boot-starter org.apache.curator curator-x-discovery com.buddha common 1.0.0 org.springframework.boot spring-boot-maven-plugin
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文件
springboot-dubbo-zookeeper com.buddha 1.0.0 4.0.0 consumer org.springframework.boot spring-boot-starter-web org.apache.dubbo dubbo-spring-boot-starter org.apache.curator curator-x-discovery com.buddha common 1.0.0 org.springframework.boot spring-boot-maven-plugin
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



