注:下面使用dubbo依賴的是zookeeper註冊中心,這裏沒有詳細的介紹。在配置以前,請自行準備好zookeeper環境。java
後續若是寫zookeeper的配置會補放連接git
compile group: 'com.alibaba', name: 'dubbo', version: '2.5.10'//dubbo compile group: 'org.apache.zookeeper', name: 'zookeeper', version: '3.3.3'//zookeeper compile group: 'com.github.sgroschupf', name: 'zkclient', version: '0.1'//zkclient
目錄結構github
實體類spring
//這裏實體對象實現了Serializable接口,dubbo規定,在遠程調用實體對象時必需要實現Serializable接口以保證明體對象可以被序列化,如不實現會報錯 public class Student implements Serializable { private String name; private int age; public Student(String name, int age) { this.name = name; this.age = age; } public Student() { } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
service方法apache
public interface TestService { //返回字符串測試 String hello(String word); //返回實體對象測試,注意實體類要實現 Serializable 接口 List<Student> selectAllStudent(); }
實現service方法app
//給service起個名字 別人調用提供接口方法時就是來實現本實現類的方法,和xml配置文件中所對應,讓spring IOC 注入所管理 @Service("testService") public class TestServiceImpl implements TestService { @Autowired private GoodsStoreMapper goodsStoreMapper; @Override public String hello(String word) { return "提供者:"+word; } @Override public List<Student> selectAllStudent() { System.out.println("------被調用了------"); List<Student> list = new ArrayList<Student>(); list.add(new Student("張三" , 1)); list.add(new Student("李四" , 2)); list.add(new Student("王五" , 3)); return list; } }
applicationContext.xmlide
<?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"> <!-- 提供方應用信息,用於計算依賴關係--> <!--name給當前服務起一個名字--> <dubbo:application name="springBootProvider"></dubbo:application> <!--protocol指定註冊中心類型 這裏用的是zookeeper--> <!--address註冊中心地址 ip地址端口號--> <dubbo:registry protocol="zookeeper" address="127.0.0.1:2181" /> <!-- 用dubbo協議在20880端口暴露服務--> <dubbo:protocol name="dubbo" port="20880" /> <!-- 聲明須要暴露的服務接口--> <dubbo:service interface="cn.appsys.service.TestService" ref="testService" /> </beans>
啓動類測試
@SpringBootApplication //讀取配置文件 @ImportResource(locations = {"classpath:config/applicationContext.xml"}) public class Start { public static void main(String[] args) { SpringApplication.run(Start.class , args); } }
運行服務端,首先先啓動zookeeper註冊中心this
啓動啓動類,注意zookeeper的變化spa
這裏看到 在配置文件中外放的接口已經被註冊進來了,調用方想調用就要經過此節點來調用服務。
目錄結構
客戶端 實體 方法都和服務端的保持一致
注意:方法所在的包也要和服務端保持一致,接口名都須要和服務端保持一致
applicationContext.xml配置文件
<?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="springBootConsumer"></dubbo:application> <dubbo:registry protocol="zookeeper" address="127.0.0.1:2181" /> <!--包名 接口名 必需要和服務提供方保持一致--> <dubbo:reference id="testService" interface="cn.appsys.service.TestService" ></dubbo:reference> </beans>
啓動測試類
@SpringBootApplication //讀取配置文件 @ImportResource(locations = {"classpath:config/applicationContext.xml"}) public class Start { public static void main(String[] args) { //SpringApplication.run返回一個ApplicationContext對象 ApplicationContext ac = SpringApplication.run(Start.class , args); //經過ApplicationContext對象的getBean獲取實現方法 TestService testService = ac.getBean(TestService.class); //調用方法 String s = testService.hello("aaaaaaaa"); System.out.println(s); List<Student> stus = testService.selectAllStudent(); for (Student stu : stus) { System.out.println(stu.getName()); } } }
啓動運行
調用成功,再看一下服務端的變化
成功!
這裏在放上若是不在服務端實體類不實現Serializable接口的報錯信息
Exception in thread "main" com.alibaba.dubbo.rpc.RpcException: Failed to invoke the method selectAllStudent in the service cn.appsys.service.TestService. Tried 3 times of the providers [192.168.1.104:20880] (1/1) from the registry 127.0.0.1:2181 on the consumer 192.168.1.104 using the dubbo version 2.0.1. Last error is: Failed to invoke remote method: selectAllStudent, provider: dubbo://192.168.1.104:20880/cn.appsys.service.TestService?anyhost=true&application=springBootConsumer&check=false&dubbo=2.0.1&generic=false&interface=cn.appsys.service.TestService&methods=hello,login,selectAllStudent&pid=8928®ister.ip=192.168.1.104&remote.timestamp=1533373429375&side=consumer×tamp=1533373564236, cause: Failed to send response: Response [id=3, version=2.0.0, status=20, event=false, error=null, result=RpcResult [result=[cn.appsys.entity.Student@45a35c25, cn.appsys.entity.Student@419cd049, cn.appsys.entity.Student@49e561e], exception=null]], cause: java.lang.IllegalStateException: Serialized class cn.appsys.entity.Student must implement java.io.Serializable java.lang.IllegalStateException: Serialized class cn.appsys.entity.Student must implement java.io.Serializable
1)序列化 咱們全部須要用來傳輸數據的實體必定要實現序列化,否則必定會報錯 2)業務注入不進來 例如咱們在Controller中注入了一個業務,@Controller使用的是Spring註解,@Reference使用的是Dubbo,若是Spring先進行掃描,那麼業務必定是注入不進去的。如全部咱們dubbo也要掃描controller。 3)超時設置 根據業務來設定不一樣的超時配置,若是一個服務很龐大處理的時間相對來講時間會比較長,能夠會一直引發超時錯誤。