dubbo-spring的集成XML

			                    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类

发布了7 篇原创文章 · 获赞 0 · 访问量 34

猜你喜欢

转载自blog.csdn.net/weixin_46066995/article/details/104958515
今日推荐