(四)Dubbo 2.7 整合Spring(xml形式)

2022-01-06 13:58:35 浏览数 (1)

Dubbo整合Spring(xml形式)

Dubbo 在 spring xml 形式的配置要比SpringBoot 注解形式的配置 可读性要更友好。

springboot项目也推荐使用 xml 形式配置 dubbo

因为Dubbo 是可以具体到方法级别的,使用xml配置起来更直观。(官方也推荐)

本文就来使用 Dubbo 2.7 整合 Spring。

项目结构如下:

父类项目依赖 pom.xml

代码语言:javascript复制
	<properties>
        <dubbo.version>2.7.13</dubbo.version>
        <spring.version>4.3.16.RELEASE</spring.version>
    </properties>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-framework-bom</artifactId>
                <version>${spring.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>org.apache.dubbo</groupId>
                <artifactId>dubbo-bom</artifactId>
                <version>${dubbo.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

            <dependency>
                <groupId>org.apache.dubbo</groupId>
                <artifactId>dubbo-dependencies-zookeeper</artifactId>
                <version>${dubbo.version}</version>
                <type>pom</type>
            </dependency>

        </dependencies>
    </dependencyManagement>

1、创建api

创建dubbo-samples-xml-api项目,定义一个接口:

OrderService.java

代码语言:javascript复制
public interface OrderService {
   List<Order> getOrderInfo(long orderId);
}

Order.java:

代码语言:javascript复制
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Order implements Serializable {

    private static final long serialVersionUID = -3757842094691885448L;
    Long orderId;
    String orderName;

}

2、创建provider

dubbo-provider.xml:

代码语言:javascript复制
<context:component-scan base-package="com.dubbo.impl"/>
<dubbo:application name="dubbo-samples-provider-xml"/>
<dubbo:registry address="zookeeper://${zookeeper.address:127.0.0.1}:${zookeeper.port:2181}"/>

<dubbo:protocol name="dubbo" port="20883"/>
<dubbo:service interface="com.dubbo.api.OrderService" ref="orderServiceImpl" protocol="dubbo" group="one"/>

OrderServiceImpl.java ,实现接口:

代码语言:javascript复制
@Service("orderServiceImpl")
@Slf4j
public class OrderServiceImpl implements OrderService {

    @Override
    public List<Order> getOrderInfo(long orderId) {
        
        List<Order> list = new ArrayList<>();
        Order order1 = new Order();
        order1.setOrderId(199L);
        order1.setOrderName("MacBook Pro 13");

        Order order2 = new Order();
        order2.setOrderId(200L);
        order2.setOrderName("RTX 2060");

        list.add(order1);
        list.add(order2);

        return list;
    }
}

启动类Provider:

代码语言:javascript复制
public class Provider {
    public static void main(String[] args) throws Exception {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"spring/dubbo-provider.xml"});
        context.start();
        System.out.println("provider service start ......");
        new CountDownLatch(1).await();
    }
}

3、创建consumer

dubbo-consumer.xml:

代码语言:javascript复制
<dubbo:application name="dubbo-samples-consumer-xml"/>

<dubbo:registry address="zookeeper://${zookeeper.address:127.0.0.1}:${zookeeper.port:2181}"/>

<dubbo:reference id="orderServiceImpl" check="false"
interface="com.dubbo.api.OrderService" protocol="dubbo" group="one"/>

实现类 OrderServiceImpl:

代码语言:javascript复制
@Service("orderServiceImpl")
@Slf4j
public class OrderServiceImpl implements OrderService {

    @Override
    public List<Order> getOrderInfo(long orderId) {
        log.info("OrderServiceImpl方法");
        log.info("request from consumer: {}", RpcContext.getContext().getRemoteAddress());
        log.info("protocol: {}",RpcContext.getContext().getProtocol());
        log.info("response from provider: {}",RpcContext.getContext().getLocalAddress());
        List<Order> list = new ArrayList<>();
        Order order1 = new Order();
        order1.setOrderId(199L);
        order1.setOrderName("MacBook Pro 13");

        Order order2 = new Order();
        order2.setOrderId(200L);
        order2.setOrderName("RTX 2060");

        list.add(order1);
        list.add(order2);

        return list;
    }
}

启动类Consumer:

代码语言:javascript复制
/**
 * @author 醋酸菌HaC | WebSite


	

0 人点赞