dubbo官方文档
提供一个可被调用的接口
提供方:实现接口的方法逻辑,启动应用程序,接收消费方的调用
消费方:确认要调用的接口,找到注册中心,调用提供方,获取接口的返回结果
示例文档参考:https://github.com/apache/dubbo
<properties>
<dubbo.version>3.2.5dubbo.version>
properties>
<dependencies>
<dependency>
<groupId>org.apache.dubbogroupId>
<artifactId>dubboartifactId>
<version>${dubbo.version}version>
dependency>
<dependency>
<groupId>org.apache.dubbogroupId>
<artifactId>dubbo-dependencies-zookeeperartifactId>
<version>${dubbo.version}version>
<type>pomtype>
dependency>
dependencies>
声明GreetingsService 接口
public interface GreetingsService {
String sayHi(String name);
}
GreetingsService 具体实现类
import com.jyl.api.GreetingsService;
public class GreetingsServiceImpl implements GreetingsService {
public String sayHi(String name) {
return "hi, " + name;
}
}
服务端Application
import com.jyl.api.GreetingsService;
import org.apache.dubbo.config.ApplicationConfig;
import org.apache.dubbo.config.RegistryConfig;
import org.apache.dubbo.config.ServiceConfig;
import java.util.concurrent.CountDownLatch;
public class Application {
// 先获取zookeeper部署的ip地址
private static String zookeeperHost = System.getProperty("zookeeper.address", "127.0.0.1");
public static void main(String[] args) throws Exception {
// 创建服务配置 服务对应的接口类GreetingsService 设置泛型
ServiceConfig<GreetingsService> service = new ServiceConfig<GreetingsService>();
// 设置应用程序的名称
service.setApplication(new ApplicationConfig("first-dubbo-provider"));
// 设置注册中心 指定zookeeper的连接地址
service.setRegistry(new RegistryConfig("zookeeper://" + zookeeperHost + ":2181"));
// 设置提供的接口
service.setInterface(GreetingsService.class);
// 设置提供具体的实现类
service.setRef(new GreetingsServiceImpl());
// 设置完成 执行导出命令
service.export();
System.out.println("dubbo service started");
// 保证线程一直挂起
new CountDownLatch(1).await();
}
}
客户端Application
import com.jyl.api.GreetingsService;
import org.apache.dubbo.config.ApplicationConfig;
import org.apache.dubbo.config.ReferenceConfig;
import org.apache.dubbo.config.RegistryConfig;
public class Application {
// 先获取zookeeper部署的ip地址
private static String zookeeperHost = System.getProperty("zookeeper.address", "127.0.0.1");
public static void main(String[] args) {
// 创建客户配置 服务对应的接口类GreetingsService 设置泛型
ReferenceConfig<GreetingsService> reference = new ReferenceConfig<GreetingsService>();
// 设置应用程序的名称
reference.setApplication(new ApplicationConfig("first-dubbo-consumer"));
// 设置注册中心 指定zookeeper的连接地址
reference.setRegistry(new RegistryConfig("zookeeper://" + zookeeperHost + ":2181"));
// 设置提供的接口
reference.setInterface(GreetingsService.class);
// 获取到远程的 接口调用
GreetingsService service = reference.get();
String message = service.sayHi("dubbo");
System.out.println(message);
}
}
示例文档参考地址:https://github.com/apache/dubbo-spring-boot-project/tree/master/dubbo-spring-boot-samples
<properties>
<dubbo.version>2.7.7dubbo.version>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starterartifactId>
dependency>
<dependency>
<groupId>org.apache.dubbogroupId>
<artifactId>dubbo-spring-boot-starterartifactId>
<version>${dubbo.version}version>
dependency>
<dependency>
<groupId>com.jylgroupId>
<artifactId>dubbo-demo-apiartifactId>
<version>1.0-SNAPSHOTversion>
dependency>
<dependency>
<groupId>org.apache.dubbogroupId>
<artifactId>dubbo-dependencies-zookeeperartifactId>
<version>${dubbo.version}version>
<type>pomtype>
<exclusions>
<exclusion>
<groupId>org.slf4jgroupId>
<artifactId>slf4j-log4j12artifactId>
exclusion>
exclusions>
dependency>
dependencies>
// 说明这个类是作为dubbo的服务被调用的
@DubboService(version = "1.0.0")
public class DemoServiceImpl implements DemoService {
@Override
public String sayHello(String name) {
return "Hello," + name;
}
}
server.port=8090
# 配置dubbo涉及的相关参数
spring.application.name=dubbo-demo-provider
# 配置dubbo服务的扫描路径
dubbo.scan.base-packages=com.jyl.provider.service
# 协议名称
dubbo.protocol.name=dubbo
dubbo.protocol.port=23456
# dubbo注册中心地址
dubbo.registry.address=zookeeper://127.0.0.1:2181
创建dubbo-demo-consumer SpringBoot项目
引入依赖 和 dubbo-demo-provider项目依赖一致
实现consumer,注意配置类和主程序入口
@SpringBootApplication
public class DubboDemoConsumerApplication {
//对于调用端
@DubboReference(version = "1.0.0")
DemoService demoService;
public static void main(String[] args) {
SpringApplication.run(DubboDemoConsumerApplication.class,args);
}
@Bean
public ApplicationRunner runner(){
return args -> System.out.println(demoService.sayHello("dubbo-spring-boot xxx"));
}
}
# 设置端口 避免端口号占用
server.port=8899
spring.application.name=dubbo-demo-consumer
# dubbo注册中心地址
dubbo.registry.address=zookeeper://127.0.0.1:2181
先启动demo-provider ,后启动 demo-consumer
寄语【自勉】:想要成功,必须甩掉所有懒惰的借口,起不来、跑不动、戒不掉……成功的路上只需要我可以,一定行!拼搏的滋味最甜美!新的一天,为梦想全力以赴!
希望可以帮助到@你