(2) dubbo的入门使用

2018-12-08  本文已影响0人  Mrsunup

1.dubbo的架构

接下里里介绍一下没有注册中心的直连方式以及有注册中心的连接方式以及多注册多协议多版本的配置

2.没有注册中心的直连方式

服务端的情况
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.5.3</version>
        </dependency>
/**
 * IHello接口
 */
public interface IHello {
    String sayHello(String msg);
}

/**
 * @Project: dubbo
 * @description: 具体的接口的实现
 * @author: sunkang
 * @create: 2018-06-24 16:27
 * @ModificationHistory who      when       What
 **/
@Service
public class HelloImpl  implements IHello  {
    @Override
    public String sayHello(String msg) {
        return "hello world "+ msg;
    }
}
<?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="dubbo-provider"  owner="sunkang"/>
    <!--不需要注册中心-->
    <dubbo:registry  address="N/A"/>
    <!--发布协议-->
    <dubbo:protocol  name="dubbo" port="20880"/>
    <!--启动过程中会有看到地址: dubbo://192.168.44.1:20880/com.sunkang.dubbo.IHello-->
    <dubbo:service interface="com.sunkang.dubbo.IHello" ref="helloService1" ></dubbo:service>

    <bean id="helloService1" class="com.sunkang.dubbo.HelloImpl"></bean>
</beans>
/**
 * @Project: dubbo
 * @description:  服务端的启动
 * @author: sunkang
 * @create: 2018-06-24 16:34
 * @ModificationHistory who      when       What
 **/
public class bootStrap {
    public static void main(String[] args) throws IOException {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("dubbo-server-direct.xml");
        context.start();
        //线程阻塞
        System.in.read();
    }
}
2018-12-08 13:31:31,751 INFO [com.alibaba.dubbo.config.AbstractConfig] -  [DUBBO] Export dubbo service com.sunkang.dubbo.IHello to url 
dubbo://192.168.44.1:20880/com.sunkang.dubbo.IHello?anyhost=true&application=dubbo-provider&dubbo=2.5.3&interface=com.sunkang.dubbo.IHello&methods=sayHello&owner=sunkang&pid=10144&side=provider&timestamp=1544247091312, dubbo version: 2.5.3, current host: 127.0.0.1
消费端的情况
<?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="dubbo-client" owner="sunkang"/>
    <!--通过直接连接的方式 需要通过url直接连接  -->
    <dubbo:reference id="helloImpl" interface = "com.sunkang.dubbo.IHello"
                     url="dubbo://192.168.44.1:20880/com.sunkang.dubbo.IHello"/>
</beans>
/**
 * @Project: dubbo
 * @description:  消费端启动消费
 * @author: sunkang
 * @create: 2018-12-08 13:38
 * @ModificationHistory who      when       What
 **/
public class ClientBootStrap {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("dubbo-client-direct.xml");
        IHello hello = context.getBean("helloImpl",IHello.class);
        System.err.print(hello.sayHello("i am dubbo"));
    }
}
hello world i am dubbo

3.有注册中心的连接方式

        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.4.10</version>
        </dependency>
        <dependency>
            <groupId>com.101tec</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.10</version>
        </dependency>

需要把服务端的xml的配置的标签为dubbo:registry改动如下

    <dubbo:registry   address="zookeeper://192.168.44.129:2181"/>

可以登录zookeeper观看服务地址列表的变化:dubbo的信息是基于url进行驱动的,管理信息可以在url地址看到,可以提供者列表只有一条信息,并且该url协议为dubbo而且地址为192.168.44.1而且接口名称为com.sunkang.dubbo.IHello

[zk: localhost:2181(CONNECTED) 3] ls /dubbo/com.sunkang.dubbo.IHello/providers
[dubbo%3A%2F%2F192.168.44.1%3A20880%2Fcom.sunkang.dubbo.IHello%3Fanyhost%3Dtrue%26application%3Ddubbo-provider%26dubbo%3D2.5.3%26interface%3Dcom.sunkang.dubbo.IHello%26methods%3DsayHello%26owner%3Dsunkang%26pid%3D14628%26side%3Dprovider%26timestamp%3D1544250185704]
    <dubbo:reference id="helloImpl" interface = "com.sunkang.dubbo.IHello"/>

4.多注册中心和多协议和多版本的配置

这里不再讲述,可以参考官网的

多注册中心:https://dubbo.apache.org/zh-cn/docs/user/demos/multi-registry.html

  <?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://dubbo.apache.org/schema/dubbo"
    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="world"  />
    <!-- 多注册中心配置 -->
    <dubbo:registry id="chinaRegistry" address="10.20.141.150:9090" />
    <dubbo:registry id="intlRegistry" address="10.20.154.177:9010" default="false" />
    <!-- 向中文站注册中心注册 -->
    <dubbo:service interface="com.alibaba.hello.api.HelloService" version="1.0.0" ref="helloService" registry="chinaRegistry" />
    <!-- 向国际站注册中心注册 -->
    <dubbo:service interface="com.alibaba.hello.api.DemoService" version="1.0.0" ref="demoService" registry="intlRegistry" />
</beans>

多协议:https://dubbo.apache.org/zh-cn/docs/user/demos/multi-protocols.html

<?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://dubbo.apache.org/schema/dubbo"
    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="world"  />
    <dubbo:registry id="registry" address="10.20.141.150:9090" username="admin" password="hello1234" />
    <!-- 多协议配置 -->
    <dubbo:protocol name="dubbo" port="20880" />
    <dubbo:protocol name="rmi" port="1099" />
    <!-- 使用dubbo协议暴露服务 -->
    <dubbo:service interface="com.alibaba.hello.api.HelloService" version="1.0.0" ref="helloService" protocol="dubbo" />
    <!-- 使用rmi协议暴露服务 -->
    <dubbo:service interface="com.alibaba.hello.api.DemoService" version="1.0.0" ref="demoService" protocol="rmi" /> 
</beans>

多版本:https://dubbo.apache.org/zh-cn/docs/user/demos/multi-versions.html
这里的版本信息也会加入都url中,进行版本的识别

<dubbo:service interface="com.foo.BarService" version="1.0.0" />
上一篇 下一篇

猜你喜欢

热点阅读