dubbo历程之初识

背景介绍:只听过dubbo,知道是开发用的,具体干嘛用,怎么用,一概不知道,可以说对dubbo一无所知,就这样我的项目需要使用dubbo调用别人的服务接口,在此记录下,方便以后自己查看

一:配置文件内容,dubbo.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://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">

    <dubbo:application name="test" />

    <!--使用zookeeper注册中心暴露服务地址-->
    <dubbo:registry protocol="zookeeper" address="${dubbo.registry.address}" />

    <!--生成远程服务代理,可以像使用本地bean一样使用-->
    <dubbo:reference id="testService" interface="cn.com.test.dubbo.TestService" />
</beans>

本来这么配置,启动始终报错,无法注入引入的类testService,后来在最后一行添加了group属性,可以了

 <dubbo:reference id="testService" interface="cn.com.test.dubbo.testService" version="1.0.0" group="服务方告知你"/>

网上雷锋的说法是,服务提供者那里这个Service有多个实现,所以指定了group,这个时候服务消费方(即调用服务方),必须配上group,否则项目起不来。也很好理解,多个实现,确实不知道你要调用哪个,就这个让我一上午服务起不来,也不懂这些。

2:引入dubbo.xml

这个项目spring-boot项目,通过main方法启动服务,

@SpringBootApplication
@EnableAsync
@MapperScan(basePackages = "com.test.mytest.mapper")
@EnableCaching
@RemoteApplicationEventScan
@EnableEurekaClient
@ImportResource("classpath:dubbo.xml")
public class TestManagerApplication {

    public static void main(String[] args) {
        SpringApplication.run(TestManagerApplication.class, args);
    }

}

三:调用服务

然后就直接可以在controller层进行调用了

@Autowired

TestService  testService;

通过注解,获取服务实例,进行调用你需要的方法就可以了。

四:引入maven jar包

差点漏了重要环节;就是pom中添加依赖,引入需要的jar包,需要服务方提供给你

完毕,此处仅是小白初次通过dubbo调用服务的过程。我知道里面的水很深,我还在岸边观望,慢慢下水



猜你喜欢

转载自blog.csdn.net/chenpuzhen/article/details/79899552