SpringBoot之Dubbo

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

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

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

<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.写提供者(服务端)的方法

接口:

public interface Hello {
 public String getString(String name);
}

实现类: 

public class HelloImpl implements Hello {
    @Override
    public String getString(String name) {
        return name;
    }
}


3.提供者的spring配置

<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配置

<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

public class DemoApplicationTests {
    @Autowired
    Hello h;
    @Test
    public void test() {
        String hh = h.getString("dubbo 远程调用成功");
        System.out.println(hh);
    }
 }

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


 springboot集成dubbo 

@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();
        }
    } }

@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);
                }
            }
        }
    }
}

博客地址:http://my.oschina.net/wangnian


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值