一、版本与环境搭建:
写在前面:截至 2021-12-17 当前Dubbo最新包与Jdk 17还不适配 调试无果后采用Jdk jdk1.8.0_311完成验证
1.Dubbo官网:Apache Dubbo
2.Zookeeper官网:Download
3.Centos7部署Docker参考:菜鸟学院
安装完设置启动并设置开机自启:
systemctl start docker systemctl enable docker
4.Docker官网:Docker
Centos7拉取Zookeeper命令,容器启动命令:
docker pull zookeeper:3.7.0
5.Linux服务器,百度云首次申领三个月免费试用:百度云服务器
6.最终还是手动搭建了Zookeeper环境,地址如下,大概3个月内都能用:
zookeeper://106.12.148.211:2181
| springboot | 2.6.1 |
|---|---|
| dubbo | 3.0.2.1 |
| zookeeper | 3.7.0 |
| jdk | 1.8.0_311 |
服务器如下:
二、聚合项目 RPC调用
项目结构,先上图:
启动服务,调用测试:
http://127.0.0.1:8090/test/hello?name=hongxu_moon
父项目配置:
4.0.0 org.springframework.boot spring-boot-starter-parent 2.6.1 com.demo zookeeper pom 1.0-SNAPSHOT service consumer provider 8 8 8 3.0.2.1 4.3.16.RELEASE 5.1.0 org.springframework.boot spring-boot-starter-web org.apache.dubbo dubbo ${dubbo.version} org.apache.dubbo dubbo-spring-boot-starter ${dubbo.version} org.apache.dubbo dubbo-dependencies-zookeeper ${dubbo.version} pom
接口模块SERVICE:
zookeeper com.demo 1.0-SNAPSHOT 4.0.0 com.demo service 1.0-SNAPSHOT jar 8 8 zhx_moon_service
接口类:
package com.demo.service;
public interface TestService {
String test(String name);
}
生产者PROVIDER:
zookeeper com.demo 1.0-SNAPSHOT 4.0.0 provider 1.0-SNAPSHOT 8 8 com.demo service 1.0-SNAPSHOT zhx_moon_provider src/main/java ***.yml false org.springframework.boot spring-boot-maven-plugin
实现类:
package com.demo;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@EnableDubbo
public class ProviderApplication {
public static void main(String[] args) {
SpringApplication.run(ProviderApplication.class,args);
}
}
package com.demo.serviceImpl;
import com.demo.service.TestService;
import org.apache.dubbo.config.annotation.DubboService;
@DubboService(version = "1.0.0",interfaceClass = TestService.class)
public class TestServiceImpl implements TestService {
@Override
public String test(String name) {
return "Hello" + name;
}
}
YML配置:
server:
port: 8089
spring:
application:
name: auto_provider
dubbo:
application:
name: providers
id: providers
registry:
address: zookeeper://106.12.148.211:2181
timeout: 100000
protocol:
name: dubbo
port: 20880
scan:
base-packages: com.demo.serviceImpl
消费者模块CONSUMERS:
zookeeper com.demo 1.0-SNAPSHOT 4.0.0 consumer 1.0-SNAPSHOT 8 8 com.demo service 1.0-SNAPSHOT
消费接口:
package com.demo;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@EnableDubbo
public class ConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class,args);
}
}
package com.demo.controller;
import com.demo.service.TestService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/test")
public class TestController {
@DubboReference(version = "1.0.0")
TestService testService;
@GetMapping("/hello")
public String test(String name){
return testService.test(name);
}
}
YML配置:
server:
port: 8090
spring:
application:
name: auto_consumer
dubbo:
application:
name: consumer
id: consumer
registry:
address: zookeeper://106.12.148.211:2181
timeout: 10000
scan:
base-packages: com.demo.controller



