基于代码构建客户端和服务端
maven依赖
<dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.6.2</version> </dependency>
1.基于服务端配置对象
1.ServiceConfig 服务配置对象
2.ProtocolConfig对象,协议对象
3.ApplicationConfig 应用对象
public void openServer(int port) {
// 服务配置
ServiceConfig serviceConfig = new ServiceConfig();
//设置服务接口
serviceConfig.setInterface(UserService.class);
//设置开放协议
serviceConfig.setProtocol(new ProtocolConfig("dubbo",port));
//设置空的注册中心
serviceConfig.setRegistry(new RegistryConfig(RegistryConfig.NO_AVAILABLE));
//设置服务当前所在的应用
serviceConfig.setApplication(new ApplicationConfig("server-app"));
//设置服务实现对象
serviceConfig.setRef(new UserServiceImpl());
//暴露服务
serviceConfig.export();
System.out.println("服务端开启:" + port);
}
public static void main(String[] args) throws IOException {
new SimpleServer().openServer(20880);
System.in.read();
}
2.客户端调用
1.ReferenceConfig 引用对象
2.ApplicationConfig 应用对象
//基于URL构建远程服务
public UserService buildRemoteService(String remoteUrl) {
ReferenceConfig<UserService> config = new ReferenceConfig();
config.setInterface(UserService.class);
config.setUrl(remoteUrl);
config.setApplication(new ApplicationConfig("yong-app"));
return config.get();
}
public static void main(String[] args) {
YongClient client = new YongClient();
UserService userService = client.buildRemoteService("dubbo://127.0.0.1:20880/com.fashion.common.dao.UserService");
System.out.println(userService.getUser(1));
}
3.有了这两个对象就可以开始远程调用了
4.起多个服务报错,address占用 启动了qos 22222
解决办法:dubbo 默认开启了qos 控制台命令 ,用的是2222 端口
在resource中创建 dubbo.properties 在里面加入内容:dubbo.application.qos.enable=false
二.基于xml配置
1.provider配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
<!-- 配置application -->
<dubbo:application name="simple-server"/>
<!-- 配置注册中心 -->
<dubbo:registry address="multicast://224.188.1.2:19306"/>
<!-- 配置协议 -->
<dubbo:protocol name="dubbo" port="-1" />
<!-- 配置 接口 -->
<dubbo:service interface="com.fashion.common.dao.UserService" ref="userService"/>
<!-- 配置服务实现 -->
<bean id="userService" class="com.fashion.common.dao.UserServiceImpl"/>
</beans>
1.1 调用
public static void main(String[] args) throws IOException {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring-provider.xml");
context.start();
System.in.read();
}
2.consumer配置
2.1xml配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
<!-- 配置客户端 也就是应用名称application -->
<dubbo:application name="young-consumer"/>
<!-- 配置注册中心地址-->
<dubbo:registry address="multicast://224.188.1.2:19306"/>
<!-- 引用地址 -->
<dubbo:reference id="userService" interface="com.fashion.common.dao.UserService"/>
</beans>
2-2调用
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new
ClassPathXmlApplicationContext("spring-consumer.xml");
context.start();
UserService userService = context.getBean(UserService.class);
try {
String cmd = "";
while (!"exit".equals(cmd = read())) {
System.out.println(userService.getUser(1));
}
} catch (IOException e) {
e.printStackTrace();
}
}
private static String read() throws IOException {
byte[] b = new byte[1024];
LineNumberReader lineNumberReader = new LineNumberReader(new InputStreamReader(System.in));
return lineNumberReader.readLine();
}
三.dubbo配置说明
标签 |
用途 |
解释 |
<dubbo:application/> |
公共 |
用于配置当前应用信息,不管该应用是提供者还是消费者 |
<dubbo:registry/> |
公共 |
用于配置连接注册中心相关信息 |
<dubbo:protocol/> |
服务 |
用于配置提供服务的协议信息,协议由提供方指定,消费方被动接受 |
<dubbo:service/> |
服务 |
用于暴露一个服务,定义服务的元信息,一个服务可以用多个协议暴露,一个服务也可以注册到多个注册中心 |
<dubbo:provider/> |
服务 |
当 ProtocolConfig 和 ServiceConfig 某属性没有配置时,采用此缺省值,可选 |
<dubbo:consumer/> |
引用 |
当 ReferenceConfig 某属性没有配置时,采用此缺省值,可选 |
<dubbo:reference/> |
引用 |
用于创建一个远程服务代理,一个引用可以指向多个注册中心 |
<dubbo:method/> |
公共 |
用于 ServiceConfig 和 ReferenceConfig 指定方法级的配置信息 |
<dubbo:argument/> |
公共 |
用于指定方法参数配置 |
配置关系图
所有配置项分为三大类。
- 服务发现:表示该配置项用于服务的注册与发现,目的是让消费方找到提供方。
- 服务治理:表示该配置项用于治理服务间的关系,或为开发测试提供便利条件。
- 性能调优:表示该配置项用于调优性能,不同的选项对性能会产生影响。