SpringBoot整合Dubbo的第三种方式——XML配置 + @ImportResource

本文涉及的产品
注册配置 MSE Nacos/ZooKeeper,118元/月
服务治理 MSE Sentinel/OpenSergo,Agent数量 不受限
云原生网关 MSE Higress,422元/月
简介: SpringBoot整合Dubbo的第三种方式——XML配置 + @ImportResource

1.文档参照


dubbo配置官方文档

2.三个工程


2.1 公共接口工程

参考这篇文章:SpringBoot整合Dubbo的第一种方式


2.2 服务提供者

首先我们注释掉配置文件中的相关内容,只留下应用名(不留也可以)。

spring.application.name=boot-user-service-provider
#dubbo.application.name=boot-user-service-provider
#
#dubbo.scan.base-packages=com.szh.service.impl
#
#dubbo.registry.address=127.0.0.1:2181
#dubbo.registry.protocol=zookeeper
#
#dubbo.protocol.name=dubbo
#dubbo.protocol.port=20880
#
#dubbo.monitor.protocol=registry

下面我们编写核心的xml配置文件(不仅让我回想起以前学spring的时候啊,哈哈哈)

<?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" xmlns:dubb="http://code.alibabatech.com/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 http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
    <!-- 1、指定当前服务/应用的名字(同样的服务名字相同,不要和别的服务同名) -->
    <dubbo:application name="boot-user-service-provider"></dubbo:application>
    <!-- 2、指定注册中心的位置 -->
<!--    <dubb:registry address="zookeeper://127.0.0.1:2181"></dubb:registry>-->
    <dubbo:registry protocol="zookeeper" address="127.0.0.1:2181"></dubbo:registry>
    <!-- 3、指定通信规则(通信协议?通信端口) -->
    <dubbo:protocol name="dubbo" port="20880"></dubbo:protocol>
    <!-- 4、暴露服务,ref:指向服务的真正的实现对象 -->
    <dubbo:service interface="com.szh.gmall.service.UserService" ref="userServiceImpl" version="1.0.0"></dubbo:service>
    <!-- 服务的具体实现 -->
    <bean id="userServiceImpl" class="com.szh.service.impl.UserServiceImpl"></bean>
    <bean id="userServiceImpl2" class="com.szh.service.impl.UserServiceImpl2"></bean>
</beans>

由于这里的dubbo配置都在上面的xml中实现了,所以下面的实现类中 @DubboService 注解就可以注释掉了。 

package com.szh.service.impl;
import com.szh.gmall.bean.UserAddress;
import com.szh.gmall.service.UserService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;
/**
 * 1.将服务提供者注册到注册中心
 *      1) 引入dubbo依赖、zookeeper客户端依赖
 *      2) 配置服务提供者
 * 2.让服务消费者从注册中心订阅服务提供者的相关服务
 */
@Service
//@DubboService(interfaceClass = UserService.class, version = "1.0.0")
public class UserServiceImpl implements UserService {
    //The default value of ${dubbo.application.name} is ${spring.application.name}
    @Value("${spring.application.name}")
    private String applicationName;
    @Override
    public List<UserAddress> getUserAddressList(String userId) {
        UserAddress userAddress1 = new UserAddress(1, "浙江省杭州市", "1", "张三", "123456", "Y");
        UserAddress userAddress2 = new UserAddress(2, "湖北省武汉市", "1", "李四", "999999", "N");
        try {
            TimeUnit.MILLISECONDS.sleep(2000); //测试timeout
//            TimeUnit.MILLISECONDS.sleep(4000); //测试重试次数
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(applicationName + " old....");
        return Arrays.asList(userAddress1, userAddress2);
    }
}
package com.szh.service.impl;
import com.szh.gmall.bean.UserAddress;
import com.szh.gmall.service.UserService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;
/**
 * 1.将服务提供者注册到注册中心
 *      1) 引入dubbo依赖、zookeeper客户端依赖
 *      2) 配置服务提供者
 * 2.让服务消费者从注册中心订阅服务提供者的相关服务
 */
@Service
//@DubboService(interfaceClass = UserService.class, version = "2.0.0")
public class UserServiceImpl2 implements UserService {
    //The default value of ${dubbo.application.name} is ${spring.application.name}
    @Value("${spring.application.name}")
    private String applicationName;
    @Override
    public List<UserAddress> getUserAddressList(String userId) {
        UserAddress userAddress1 = new UserAddress(1, "浙江省杭州市", "1", "张三", "123456", "Y");
        UserAddress userAddress2 = new UserAddress(2, "湖北省武汉市", "1", "李四", "999999", "N");
        try {
            TimeUnit.MILLISECONDS.sleep(2000); //测试timeout
//            TimeUnit.MILLISECONDS.sleep(4000); //测试重试次数
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(applicationName + " new....");
        return Arrays.asList(userAddress1, userAddress2);
    }
}

主启动类上添加  @ImportResource注解,要加载类路径下的某个xml配置文件。

package com.szh;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.apache.dubbo.config.spring.context.annotation.EnableDubboConfig;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;
@SpringBootApplication
//@EnableDubbo(scanBasePackages = "com.szh")
@ImportResource(locations = "classpath:provider.xml")
public class BootUserServiceProviderApplication {
    public static void main(String[] args) {
        SpringApplication.run(BootUserServiceProviderApplication.class, args);
    }
}

2.3 服务消费者

首先我们注释掉配置文件中的相关内容,只留下应用名(不留也可以)、端口号最好留下,因为服务提供者启动之后会占用8080,这里将服务消费者声明在8081端口启动。

server.port=8081
spring.application.name=boot-order-service-consumer
#dubbo.application.name=boot-order-service-consumer
#
#dubbo.registry.address=zookeeper://127.0.0.1:2181
#dubbo.monitor.protocol=registry
package com.szh.controller;
import com.szh.gmall.bean.UserAddress;
import com.szh.gmall.service.OrderService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
 *
 */
@RestController
public class OrderController {
    @Autowired
    private OrderService orderService;
    @GetMapping(value = "/initOrder/{userId}")
    public List<UserAddress> initOrder(@PathVariable("userId") String userId) {
        return orderService.initOrder2(userId);
    }
}
package com.szh.service.impl;
import com.szh.gmall.bean.UserAddress;
import com.szh.gmall.service.OrderService;
import com.szh.gmall.service.UserService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.Method;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/**
 *
 */
@Service
public class OrderServiceImpl implements OrderService {
//    @DubboReference(interfaceClass = UserService.class, //服务接口名
//                    version = "1.0.0", //服务版本,与服务提供者的版本一致
//                    check = false,  //启动时检查提供者是否存在,true报错,false忽略
//                    timeout = 3000, //服务方法调用超时时间(毫秒)
//                    methods = @Method(name = "getUserAddressList"), //精确到服务接口的某个方法
//                    retries = 3) //远程服务调用重试次数,不包括第一次调用,不需要重试请设为0
    @Autowired
    private UserService userService;
    @Override
    public void initOrder(String userId) {
    }
    @Override
    public List<UserAddress> initOrder2(String userId) {
        System.out.println("用户id:" + userId);
        List<UserAddress> addressList = userService.getUserAddressList(userId);
        return addressList;
    }
}

下面是核心的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://dubbo.apache.org/schema/dubbo" xmlns:dubb="http://code.alibabatech.com/schema/dubbo"
       xmlns:context="http://www.springframework.org/schema/context"
       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 http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    <context:component-scan base-package="com.szh"></context:component-scan>
    <dubbo:application name="boot-order-service-consumer"></dubbo:application>
    <dubbo:registry address="zookeeper://127.0.0.1:2181"></dubbo:registry>
    <!-- 声明需要调用的远程服务的接口,生成远程服务代理 -->
    <dubbo:reference interface="com.szh.gmall.service.UserService" id="userServiceImpl" version="1.0.0" check="false" timeout = "3000">
        <dubbo:method name="getUserAddressList"></dubbo:method>
    </dubbo:reference>
</beans>
package com.szh;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;
@SpringBootApplication
//@EnableDubbo(scanBasePackages = "com.szh")
@ImportResource(locations = "classpath:consumer.xml")
public class BootOrderServiceConsumerApplication {
    public static void main(String[] args) {
        SpringApplication.run(BootOrderServiceConsumerApplication.class, args);
    }
}


3.启动测试


启动服务提供者和消费者之前,要先将zookeeper开启,然后再将dubbo管控台打开。

我这里为了方便,就直接在windows下启动zookeeper了,dubbo管控台开不开无所谓。

参考  https://blog.csdn.net/weixin_43823808/article/details/124494499

相关实践学习
基于MSE实现微服务的全链路灰度
通过本场景的实验操作,您将了解并实现在线业务的微服务全链路灰度能力。
相关文章
|
10天前
|
XML Ubuntu Linux
部署08---扩展-Win10配置WSL(Ubuntu)环境,WSL系统是什么意思,是Windows系统上的一个子系统, xml的一大特点是直链系统,直接链接你的CPU,硬盘和内存,如何用 WSL部署
部署08---扩展-Win10配置WSL(Ubuntu)环境,WSL系统是什么意思,是Windows系统上的一个子系统, xml的一大特点是直链系统,直接链接你的CPU,硬盘和内存,如何用 WSL部署
|
15天前
|
XML Java 数据库
配置applicationContext.xml文件
配置applicationContext.xml文件
|
12天前
|
XML Java 关系型数据库
Action:Consider the following: If you want an embedde ,springBoot配置数据库,补全springBoot的xml和mysql配置信息就好了
Action:Consider the following: If you want an embedde ,springBoot配置数据库,补全springBoot的xml和mysql配置信息就好了
|
13天前
|
XML Java 数据库
配置applicationContext.xml文件
配置applicationContext.xml文件
|
17天前
|
XML 数据格式
XML配置Servlet文件,不使用注解配置路径的方法
XML配置Servlet文件,不使用注解配置路径的方法
|
17天前
|
Dubbo Java 应用服务中间件
Spring Boot 调用 Dubbo 接口与编写 Dubbo 接口实战
Spring Boot 调用 Dubbo 接口与编写 Dubbo 接口实战
43 1
|
14天前
|
消息中间件 Java 数据库连接
理解java的springboot+mybatisplus+dubbo+nacos+kafka这一套技术栈
理解java的springboot+mybatisplus+dubbo+nacos+kafka这一套技术栈
21 0
|
2月前
|
Dubbo Java 应用服务中间件
微服务学习 | Springboot整合Dubbo+Nacos实现RPC调用
微服务学习 | Springboot整合Dubbo+Nacos实现RPC调用
|
2月前
|
Dubbo Java 应用服务中间件
阿里巴巴资深架构师深度解析微服务架构设计之SpringCloud+Dubbo
软件架构是一个包含各种组织的系统组织,这些组件包括Web服务器,应用服务器,数据库,存储,通讯层),它们彼此或和环境存在关系。系统架构的目标是解决利益相关者的关注点。
|
2月前
|
Dubbo Cloud Native 应用服务中间件
【阿里云云原生专栏】云原生环境下的微服务治理:阿里云 Dubbo 与 Nacos 的深度整合
【5月更文挑战第25天】阿里云Dubbo和Nacos提供微服务治理的强大工具,整合后实现灵活高效的治理。Dubbo是高性能RPC框架,Nacos则负责服务发现和配置管理。整合示例显示,通过Nacos注册中心,服务能便捷注册发现,动态管理配置。简化部署,提升适应性,但也需注意服务稳定性和策略规划。这种整合为云原生环境的微服务架构带来强大支持,未来应用前景广阔。
223 2