1.父工程pom中指定依赖以及版本
4.0.0 org.example demo-basepom 1.0-SNAPSHOT service-api service-provider service-consumer 2.7.5 org.apache.dubbo dubbo${dubbo.version} org.apache.dubbo dubbo-common${dubbo.version} org.apache.dubbo dubbo-registry-zookeeper${dubbo.version} org.apache.dubbo dubbo-remoting-apiorg.apache.dubbo dubbo-commonorg.apache.dubbo dubbo-registry-nacos${dubbo.version} org.apache.dubbo dubbo-rpc-dubbo${dubbo.version} org.apache.dubbo dubbo-remoting-apiorg.apache.dubbo dubbo-commonorg.apache.dubbo dubbo-remoting-netty4${dubbo.version} org.apache.dubbo dubbo-remoting-apiorg.apache.dubbo dubbo-serialization-hessian2${dubbo.version} org.apache.dubbo dubbo-commonlog4j log4j1.2.16 org.slf4j slf4j-api1.7.5 org.slf4j slf4j-log4j121.7.5 com.alibaba fastjson1.2.62 org.apache.maven.plugins maven-compiler-plugin3.3 1.8 1.8
2. 创建提供者模块
2.1 引入依赖
demo-base org.example 1.0-SNAPSHOT 4.0.0 service-providerorg.example service-api1.0-SNAPSHOT org.apache.dubbo dubboorg.apache.dubbo dubbo-registry-zookeeperorg.apache.dubbo dubbo-rpc-dubboorg.apache.dubbo dubbo-remoting-netty4org.apache.dubbo dubbo-serialization-hessian2
2.2 编写dubbo的配置类
#程序名称 dubbo.application.name=service-provider #协议使用dubbo协议 dubbo.protocol.name=dubbo #指定dubbo的通信端口(随意指定) dubbo.protocol.port=20880
2.3 编写提供者的启动类,先写静态内部类作为dubbo的配置类,配置相关属性以及注册中心地址,然后再main方法中创建上下文对象,启动容器即可。
package com.lwb;
import org.apache.dubbo.config.RegistryConfig;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import java.io.IOException;
public class DubboMain {
public static void main(String[] args) throws IOException {
// 创建一个上下文对象
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ProviderConfiguration.class);
context.start();// 让容器启动
System.in.read(); // 让程序停在这
}
// 写一个服务提供者的配置类
@Configuration
@EnableDubbo(scanbasePackages = "com.lwb.service.impl") // 类似组件扫描,也需要指定一个包,这里扫描的是实现类的包
@PropertySource("classpath:/dubbo-provider.properties") // 加载配置文件
static class ProviderConfiguration{
// 加载注册中心的配置方法
@Bean
public RegistryConfig registryConfig(){
// 创建一个注册中心的对象
RegistryConfig registryConfig = new RegistryConfig();
// 赋值注册中心地址 需要启动zookeeper
registryConfig.setAddress("zookeeper://127.0.0.1:2181");
return registryConfig;
}
}
}
---------------------------------------------------------------------------------------------------------------------------------
3.公共API模块只需要编写一个接口,用来调用即可。
--------------------------------------------------------------------------------------------------------------------------------
4.消费者模块
4.1引入依赖,和提供者的依赖一样
4.2编写组件类
package com.lwb.bean;
import com.lwb.service.HelloService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Component;
@Component
public class ConsumerComponent {
// 引入dubbo的service对象
@Reference
private HelloService helloService;
public String sayHello(String name){
return helloService.sayHello(name);
}
}
4.3编写配置文件
dubbo.application.name=service-consumer #zookeeper地址 dubbo.registry.address=zookeeper://127.0.0.1:2181
4.4编写启动测试类
package com.lwb;
import com.lwb.bean.ConsumerComponent;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import java.io.IOException;
public class ConsumerMain {
public static void main(String[] args) throws IOException {
// 因为这是基于注解+配置类的方式进行dubbo参数的配置
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ConsumerConfiguration.class);
context.start();
// 获取消费者组件
ConsumerComponent service = context.getBean(ConsumerComponent.class);
while (true){
System.in.read();
String world = service.sayHello("world");
System.out.println("result===="+world);
}
}
@Configuration
@PropertySource("classpath:/dubbo-consumer.properties")
@ComponentScan(basePackages = "com.lwb.bean") // spring的组件扫描
@EnableDubbo //因为要引入dubbo组件,所以需要该注解
static class ConsumerConfiguration{
}
}
运行效果:
说明:在控制台按几次键,就会打印几次结果。



