dubbo 快速启动-结合spring注解使用com.alibaba.dubbo.container.Main启动

开始

是在之前的项目上

https://blog.csdn.net/ko0491/article/details/85168055

只更改Main类

在这里插入图片描述

之前的

现在的

在这里插入图片描述

public class Main3 {

    public static void main(String[] args) throws IOException {
        // com.alibaba.dubbo.container.Main.main(args);
        //
        // ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
        // new String[]{ "META-INF/spring/service-provider.xml" });
        /**
         * 使用spring文件
         */
        /*
         * ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( new String[]{
         * "META-INF/spring/spring.xml" }); context.start();
         */
        com.alibaba.dubbo.container.Main.main(args);
        System.out.println("provider启动了");
    }

}

是不是很简单,有点小意外

com.alibaba.dubbo.container.Main.main方法介绍

在这里插入图片描述
    public static final String CONTAINER_KEY = "dubbo.container";

args传进来是空
config最终获取到的是spring


在这里插入图片描述

args = spring

再向下


在这里插入图片描述

将容器添加到list集合中,只有一个


在这里插入图片描述

在这里插入图片描述

是spring容器
在这里插入图片描述

这个将会跳过,不匹配,关闭服务时


在这里插入图片描述

循环窗口集合,上面的只有一个
com.alibaba.dubbo.container.spring.SpringContainer

在start()方法


在这里插入图片描述

com.alibaba.dubbo.container.spring.SpringContainer start()方法

在这里插入图片描述

介绍下几个常量

    public static final String SPRING_CONFIG = "dubbo.spring.config";
    public static final String DEFAULT_SPRING_CONFIG = "classpath*:META-INF/spring/*.xml";
    private static final Logger logger = LoggerFactory.getLogger(SpringContainer.class);
    static ClassPathXmlApplicationContext context;

获取配置,默认没有


在这里插入图片描述
在这里插入图片描述

最终就是这个路径了

   public static final String DEFAULT_SPRING_CONFIG = "classpath*:META-INF/spring/*.xml";

写死了下面就是创建spring ApplicationContext过程与启动


在这里插入图片描述

我们的项目结构

配置文件刚好在META-INF/spring下,自动加载这个目录下的所有spring配置文件

在这里插入图片描述

spring.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:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:cache="http://www.springframework.org/schema/cache"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd">
    <!-- 当我们需要使用 注解时 开启注解 -->
    <context:annotation-config />
    <!--配置扫描包 -->
    <context:component-scan base-package="com.ghgcn.dubbo.service" />
    <!-- 这里不用了,会自动加载这个目录下的所有spring配置文件 -->
    <!-- <import resource="service-provider.xml"/> -->
</beans>

service-provider.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:dubbo="http://dubbo.apache.org/schema/dubbo" xmlns="http://www.springframework.org/schema/beans"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
       http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">

    <dubbo:application name="dubbo-provider" />

    <!-- 注册中心地址 -->
    <!-- <dubbo:registry address="multicast://224.5.6.7:1234" /> -->
    <dubbo:registry protocol="zookeeper" address="zookeeper://127.0.0.1:2181" />

    <!-- 声明协议与商品 -->
    <dubbo:protocol name="dubbo" port="20886" />

    <!-- 声明服务的实现类 旧的方式-->
    <!-- <bean id="greetingService" class="com.ghgcn.dubbo.service.impl.GreetingServiceImpl" 
        /> -->

    <!-- 声明服务 spring -->
    <dubbo:service interface="com.ghg.dubbo.api.GreetingService"
        ref="greetingService" protocol="dubbo" validation="false" />
</beans>

github:https://github.com/ln0491/dubbo-demo

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容