dubbo-spring的集成XML
1,提出需求
某個電商系統,訂單服務需要調用用戶服務獲取某個用戶的所有地址;
我們現在 需要創建兩個服務模塊進行測試
測試預期結果:
訂單服務web模塊在A服務器,用戶服務模塊在B服務器,A可以遠程調用B的功能。
2.創建空項目
3,創建ego-user-service-provider
4,創建好了項目,就創建UserAddress類
5,創建UserService
6,創建UserServiceImpl
7 創建ego-order-service-consumer
創建OrderService接口
創建UserAddress
創建OrderServiceImpl
以上項目存在的問題
現在的思路是consumer–provider裏面的userServiceImpl
此時還不能做遠程調用
解決重複代碼問題
創建ego-interface
刪除ego-user-service-provider裏面需要被提取抽來的內容
刪除ego-order-service-consumer裏面的內容
引用公共類,相當說把他們兩個類的公共代碼抽取到另外一個類,所有隻需要引用公共類的pom GVA座標位置添加到該類即可。
使用dubbo對ego-user-service-provider項目進行改造
1,引入相關jar包
<!--核心包-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.6.7</version>
</dependency>
<!--java連接zk的包。因爲服務啓動時要去註冊,所要一定要引入java操作zk的包-->
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.11</version>
</dependency>
<!-- curator-framework -->
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>4.1.0</version>
</dependency>
<!--因爲dubbo底層使用性能非常好的netty【通信框架】-->
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>4.1.32.Final</version>
</dependency>
2,添加配置信息 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">
<!--聲明註冊到zk的名字 應該程序的名稱-->
<dubbo:application name="ego-user-service-provider"></dubbo:application>
<!--聲明註冊中心的地址和方式-->
<dubbo:registry address="zookeeper://115.28.104.00:2181" ></dubbo:registry>
<!--使用dubbo協議,將服務暴露在20880端口 -->
<dubbo:protocol name="dubbo" port="20880" />
<!--使用spring創建要暴露的對象UserServiceImpl-->
<bean id="userServiceImpl" class="com.sxt.service.impl.UserServiceImpl"></bean>
<!--使用dubbo暴露服務-->
<dubbo:service interface="com.sxt.service.UserService" ref="userServiceImpl"></dubbo:service>
</beans>
3,創建test類,測試
public class AppTestProvider {
public static void main(String[] args) throws IOException {
ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
context.start();
System.out.println("提供者啓動成功。服務註冊成功");
//不讓程序關閉
System.in.read();
}
}
4,查看監控頁面
使用dubbo對ego-user-service-consumer項目進行改造
1,引入同上jar包
2,添加一個方法,使配置能夠注入值
3,添加配置信息 applicationContext.xml
4,測試
public class AppTestConsumer {
public static void main(String[] args) {
ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
context.start();
OrderService orderService = context.getBean(OrderService.class);
List<UserAddress> userAddresses = orderService.initOrder("1");
for (UserAddress userAddress : userAddresses) {
System.out.println(userAddress.getId()+" "+userAddress.getUserAddress()+" "+userAddress.getUserId());
}
System.out.println("消費者調用成功");
}
}
5,要序列化pojo的useradress類