分布式的流程图:
先创建一个提供者类
写一个接口和接口实现类
接口如下图:
public interface TestService {
public String getTestMethod(String param);
public String getTestMethod1(String param);
}
接口实现类如下图:
import com.xdl.service.TestService;
public class TestServiceImpl implements TestService {
public String getTestMethod(String param) {
String result = null;
switch (param) {
case "1":
result = "1";
break;
case "2":
result = "2";
break;
case "3":
result = "3";
break;
default:
result = "0";
break;
}
return result;
}
@Override
public String getTestMethod1(String param) {
String result = null;
switch (param) {
case "1":
result = "1";
break;
case "2":
result = "2";
break;
case "3":
result = "3";
break;
default:
result = "0";
break;
}
return result;
}
}
配置文件如下:
<?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://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd ">
<!-- 具体的实现bean -->
<bean id="testService" class="com.xdl.service.impl.TestServiceImpl" />
<!-- 提供方应用信息,用于计算依赖关系 -->
<dubbo:application name="provider" />
<!-- 使用zookeeper注册中心暴露服务地址 ip根据自己的设置-->
<dubbo:registry address="zookeeper://192.168.100.116:2181" />
<!-- 用dubbo协议在20880端口暴露服务 -->
<dubbo:protocol name="dubbo" port="29014" />
<!-- 声明需要暴露的服务接口 -->
<dubbo:service interface="com.xdl.service.TestService" ref="testService" />
</beans>
如果需要提供更多服务就可以复制下面代码改成相应的接口路径和接口名(首字母小写)
提供别的接口(功能)供接收者调用
<!-- 声明需要暴露的服务接口 -->
<dubbo:service interface="com.xdl.service.TestService" ref="testService" />
<dubbo:service interface="com.xdl.service.TestService2" ref="testService2" />
注册服务
这里用main方法来代替调用服务
package com.test;
import java.io.IOException;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestServiceTest {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
new String[]{"application.xml"}); //读取xml配置文件 获得提供者对象
context.start();
System.out.println("提供者服务已注册成功");
try {
System.in.read();//让此程序一直跑,表示一直提供服务
} catch (IOException e) {
e.printStackTrace();
}
}
}
创建消费者
配置文件如下:
<?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://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd ">
<!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->
<dubbo:application name="consumer" />
<!-- 使用xxx注册中心暴露发现服务地址 ip地址和上一个提供者配置文件中的保持一致-->
<dubbo:registry protocol="zookeeper" address="zookeeper://192.168.100.116:2181" />
<!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
<dubbo:reference id="testService" interface="com.xdl.service.TestService" />
</beans>
这里测试用main方法测试
在mvc中类似于contrller层 响应请求
import java.io.IOException;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.xdl.service.TestService;
public class ConsumerServiceTest {
public static void main(String[] args) {
//读取xml配置文件 获得bean组件
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "application.xml" });
context.start();
TestService testService = (TestService) context.getBean("testService");
//调用接口的方法 System.out.println(testService.getTestMethod("1"));
System.out.println(testService.getTestMethod1("2"));
try {
System.in.read();
} catch (IOException e) {
e.printStackTrace();
}
}
先启动提供者(接口)和注册中心(zookeeper)
再调用服务
dubbo监控中心访问
http://192.168.100.116(ip地址是配置文件设置的)/dubbo-admin-2.5.4-SNAPSHOT(部署在tmocat上 应用的war包或者jar包名称)/
如图: