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

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值