前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >SpringBoot之Dubbo

SpringBoot之Dubbo

作者头像
王念博客
发布2019-07-25 18:02:56
4640
发布2019-07-25 18:02:56
举报
文章被收录于专栏:王念博客王念博客

DUBBO是一个分布式服务框架,致力于提供高性能和透明化的RPC远程服务调用方案,是阿里巴巴SOA服务化治理方案的核心框架,每天为2,000+个服务提供3,000,000,000+次访问量支持,并被广泛应用于阿里巴巴集团的各成员站点。

http://www.oschina.net/p/dubbo

1.导入依赖包(dubbo zookeeper zkclient)

代码语言:javascript
复制
<dependency>
 <groupId>com.alibaba</groupId>
 <artifactId>dubbo</artifactId>
 <version>2.5.3</version>
</dependency>
<dependency>
 <groupId>org.apache.zookeeper</groupId>
 <artifactId>zookeeper</artifactId>
 <version>3.4.6</version>
</dependency>
<dependency>
 <groupId>com.101tec</groupId>
 <artifactId>zkclient</artifactId>
 <version>0.7</version>
</dependency>

2.写提供者(服务端)的方法

接口:

代码语言:javascript
复制
public interface Hello {
 public String getString(String name);
}

实现类:

代码语言:javascript
复制
public class HelloImpl implements Hello {
    @Override
    public String getString(String name) {
        return name;
    }
}

3.提供者的spring配置

代码语言:javascript
复制
<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:dubbo="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="
     http://www.springframework.org/schema/context  http://www.springframework.org/schema/context/spring-context-4.0.xsd
     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
     http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
 <!-- dubbo程序名字 -->-->
    <dubbo:application name="dubbo"/>
 
    <!-- 使用zookeeper注册中心暴露服务地址 -->
    <dubbo:registry protocol="zookeeper" address="192.168.1.1:4181" username="用户名" password="密码"/>
    <!-- 用dubbo协议在20880端口暴露服务 -->
    
     <bean id="helloimpl" class="com.example.HelloImpl"/> <!--创建实现类的bean-->
     
     <dubbo:service interface="com.example.Hello" ref="helloimpl" id="hello"/> <!--将接口暴露给服务中心-->
</beans>

4.写消费者(客户端)接口

这里可以把服务端的接口复制过来就完事,如果你需要提供给其他们人使用,最好不是复制接口,而是打成jar接口包。

5.导入和服务端一样的依赖,配置spring配置

代码语言:javascript
复制
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
       xsi:schemaLocation="
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
     http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
 <!--名字可以和服务端不同-->
   <dubbo:application name="dubbo"/>
 
    <!-- 使用zookeeper注册中心暴露服务地址 -->
    <dubbo:registry protocol="zookeeper" address="192.168.1.1:4181" username="用户名" password="密码"/>
    <!-- 用dubbo协议在20880端口暴露服务 -->
     < dubbo : reference interface = "com.example.Hello"  id = "hello" />  <!--复制过来的接口或者jar里的接口 必须和服务端的接口一致  --> 
</beans>

6.客户端test

代码语言:javascript
复制
public class DemoApplicationTests {
    @Autowired
    Hello h;
    @Test
    public void test() {
        String hh = h.getString("dubbo 远程调用成功");
        System.out.println(hh);
    }
 }

最后说一下不用tomcat启动的方式

springboot集成dubbo

代码语言:javascript
复制
@SpringBootApplication
@ImportResource("classpath:/dubbo-provider.xml")//启动加在dubbo配置文件
public class ProviderApplication {
    
    public static void main(String[] args) {
        SpringApplication.run(ProviderApplication.class, args);
        try {
            System.out.println("启动成功,按任意键关闭");
            System.in.read();
        } catch (IOException e) {
            e.printStackTrace();
        }
    } }
代码语言:javascript
复制
@SpringBootApplication
public class DemoApplication {
    private static final Logger log = LoggerFactory.getLogger(DemoApplication.class);
    public static void main(String[] args) {
        try {
            ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:/applicationContext*.xml");
            context.start();
        } catch (Exception e) {
            log.error("== DubboProvider context start error:", e);
        }
        synchronized (DemoApplication.class) {
            while (true) {
                try {
                    DemoApplication.class.wait();
                } catch (InterruptedException e) {
                    log.error("== synchronized error:", e);
                }
            }
        }
    }
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
微服务引擎 TSE
微服务引擎(Tencent Cloud Service Engine)提供开箱即用的云上全场景微服务解决方案。支持开源增强的云原生注册配置中心(Zookeeper、Nacos 和 Apollo),北极星网格(腾讯自研并开源的 PolarisMesh)、云原生 API 网关(Kong)以及微服务应用托管的弹性微服务平台。微服务引擎完全兼容开源版本的使用方式,在功能、可用性和可运维性等多个方面进行增强。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档