Dubbo——入门案例

Posted 芝諾de乌龟

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Dubbo——入门案例相关的知识,希望对你有一定的参考价值。

1. pmo:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.jcx.dubbo.demo</groupId>
    <artifactId>dubbo-demo</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>
    <modules>
        <module>dubbo-api</module>
        <module>dubbo-consumer</module>
        <module>dubbo-provider</module>
    </modules>

    <dependencies>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.6.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-framework</artifactId>
            <version>2.12.0</version>
        </dependency>
    </dependencies>

</project>

2. dubbo-api

service:

package com.jcx.dubbo.demo.service;

public interface IDemoService {

    String sayHello(String userName);

}

3. dubbo-provider:

service.impl

package com.jcx.dubbo.demo.service.impl;

import com.jcx.dubbo.demo.service.IDemoService;

public class DemoServiceImpl implements IDemoService {

    public String sayHello(String userName) {
        return "Hello," + userName;
    }
    
}

ProviderApplication:

import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.io.IOException;

public class ProviderApplication {
    public static void main(String[] args) throws IOException {
        ClassPathXmlApplicationContext ioc = new ClassPathXmlApplicationContext("provider.xml");
        ioc.start();
        System.out.println("provider start...");
        System.in.read();
    }
}

provider.xml:

<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
       xmlns="http://www.springframework.org/schema/beans"
       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="dubbo-demo-provider"/>

    <!--指定注册中心-->
    <dubbo:registry protocol="zookeeper" address="127.0.0.1:2181"/>

    <!--指定通信规则(通信协议,通信端口)-->
    <dubbo:protocol name="dubbo" port="20880"/>

    <!--暴露服务 ref指向真正实现-->
    <dubbo:service interface="com.jcx.dubbo.demo.service.IDemoService" ref="demoServiceImpl"/>

    <!--服务实现-->
    <bean id="demoServiceImpl" class="com.jcx.dubbo.demo.service.impl.DemoServiceImpl"/>

</beans>

4. dubbo-consumer:

ConsumerApplication:

import com.jcx.dubbo.demo.service.IDemoService;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.io.IOException;

public class ConsumerApplication {

    public static void main(String[] args) throws IOException {
        ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("consumer.xml");
        applicationContext.start();
        IDemoService demoService = (IDemoService) applicationContext.getBean("demoService");
        String hello = demoService.sayHello("jcx");
        System.out.println(hello);
    }
}

consumer.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
       xmlns="http://www.springframework.org/schema/beans"
       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="dubbo-demo-consumer"></dubbo:application>

    <!--指定注册中心-->
    <dubbo:registry address="zookeeper://127.0.0.1:2181"></dubbo:registry>

    <!--生成远程服务代理接口  check:启动时是否检查-->
    <dubbo:reference id="demoService" interface="com.jcx.dubbo.demo.service.IDemoService" check="false"/>
</beans>

注:先启动提供者,后启动消费者

以上是关于Dubbo——入门案例的主要内容,如果未能解决你的问题,请参考以下文章

Dubbo——入门案例

Dubbo架构设计及入门案例

SpringBoot+Dubbo+Zookeeper入门案例

探花交友day00—探花交友前置Dubbo

dubbo 2.7 的 3种入门案例实战

SpringBoot+Dubbo+zookeeper 快速入门案例