實(shí)例代碼
引入dubbo 依賴
<!-- dubbo -->
<!-- zeekeeper注冊(cè)中心 -->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.9</version>
</dependency>
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.10</version>
</dependency>
<!-- dubbo -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.3</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
</exclusion>
</exclusions>
</dependency>
服務(wù)
@Slf4j
public class DemoServiceImpl implements DemoService {
@Override
public String sayHello(String name) {
log.info("[sayHello]start: {}", name);
return "Hello " + name + " " + new Date();
}
}
xml 配置
<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">
<!--定義了提供方應(yīng)用信息,用于計(jì)算依賴關(guān)系笨农;在 dubbo-admin 或 dubbo-monitor 會(huì)顯示這個(gè)名字辫秧,方便辨識(shí)-->
<dubbo:application name="demotest-provider" owner="programmer" organization="dubbox"/>
<!--使用 zookeeper 注冊(cè)中心暴露服務(wù),注意要先開(kāi)啟 zookeeper-->
<dubbo:registry address="zookeeper://59.110.240.159:2181"/>
<!-- 用dubbo協(xié)議在20880端口暴露服務(wù) -->
<dubbo:protocol name="dubbo" port="20880"/>
<!--使用 dubbo 協(xié)議實(shí)現(xiàn)定義好的 api.PermissionService 接口-->
<dubbo:service interface="DemoService" ref="demoService" protocol="dubbo"/>
<!--具體實(shí)現(xiàn)該接口的 bean-->
<bean id="demoService" class="DemoServiceImpl"/>
</beans>
啟動(dòng)類
@SpringBootApplication
@ImportResource(value = "classpath*:META-INF/provider.xml")
public class DubboProviderApplication {
public static void main(String[] args) {
SpringApplication.run(DubboProviderApplication.class, args);
}
}
流程梳理
解析配置文件到 beanDefinitionMap