Dubbo入门01

一 Dubbo技术架构

Dubbo入门01_第1张图片
节点角色说明
Dubbo入门01_第2张图片

二 代码实现

2.1 服务端

提供给消费端,也就是被调用的一方

  1. 定义接口
/**
 * xml方式服务提供者接口
 */
public interface ProviderService {

    String SayHello(String word);
}
  1. 实现接口
/**
 * xml方式服务提供者实现类
 */
public class ProviderServiceImpl implements ProviderService{

    public String SayHello(String word) {
        return "hello "+word;
    }
}
  1. maven依赖
    <dependencies>
        <dependency>
            <groupId>junitgroupId>
            <artifactId>junitartifactId>
            <version>3.8.1version>
            <scope>testscope>
        dependency>
        
        <dependency>
            <groupId>com.alibabagroupId>
            <artifactId>dubboartifactId>
            <version>2.6.6version>
        dependency>
        <dependency>
            <groupId>org.apache.zookeepergroupId>
            <artifactId>zookeeperartifactId>
            <version>3.4.10version>
        dependency>
        <dependency>
            <groupId>com.101tecgroupId>
            <artifactId>zkclientartifactId>
            <version>0.5version>
        dependency>
        <dependency>
            <groupId>io.nettygroupId>
            <artifactId>netty-allartifactId>
            <version>4.1.32.Finalversion>
        dependency>
        <dependency>
            <groupId>org.apache.curatorgroupId>
            <artifactId>curator-frameworkartifactId>
            <version>2.8.0version>
        dependency>
        <dependency>
            <groupId>org.apache.curatorgroupId>
            <artifactId>curator-recipesartifactId>
            <version>2.8.0version>
        dependency>
    dependencies>
  1. 暴露接口(xml 配置方法)
    首先,我们在我们项目的 resource 目录下创建 META-INF.spring 包,然后再创建 provider.xml 文件

<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="provider" owner="dubbo">
        <dubbo:parameter key="qos.enable" value="true"/>
        <dubbo:parameter key="qos.accept.foreign.ip" value="false"/>
        <dubbo:parameter key="qos.port" value="55555"/>
    dubbo:application>

    <dubbo:monitor protocol="registry"/>

    
    
    <dubbo:registry address="N/A" />

    
    <dubbo:protocol name="dubbo" port="20880"/>

    
    <dubbo:service
            interface="com.dubbo.provider.service.ProviderService"
            ref="providerService"/>

    
    <bean id="providerService" class="com.dubbo.provider.service.ProviderServiceImpl"/>

beans>

在启动的时候如果没有log4j.properties配置会有警告,所以加上配置文件
Dubbo入门01_第3张图片

log4j.rootLogger = info,stdout,D
log4j.appender.stdout = org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target = System.out
log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern =  %d %t [%c{2}] %L [%-5p] %m%n
log4j.appender.D = org.apache.log4j.DailyRollingFileAppender
log4j.appender.D.File = ../logs/log.log
log4j.appender.D.Append = true
log4j.appender.D.Threshold = info
log4j.appender.D.DatePattern= '_'yyyy-MM-dd'.log'
log4j.appender.D.layout = org.apache.log4j.PatternLayout
log4j.appender.D.layout.ConversionPattern = %d %t [%c{2}] %L [%-5p] %m%n
  1. 启动类(xml方式启动)
/**
 * xml方式启动
 *
 */
public class ProviderApplication
{
    public static void main( String[] args ) throws IOException {
        //加载xml配置文件启动
        ClassPathXmlApplicationContext context =
                new ClassPathXmlApplicationContext("META-INF/spring/provider.xml");
        context.start();
        System.in.read(); // 按任意键退出
    }
}

Dubbo入门01_第4张图片

2.2 消费端

  1. xml配置
    Dubbo入门01_第5张图片

<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="consumer" owner="dubbo"/>

    
    
    <dubbo:registry address="N/A" />
    

    
    
    <dubbo:reference id="providerService"
                     interface="com.dubbo.provider.service.ProviderService"
                     url="dubbo://192.168.31.202:20880/com.dubbo.provider.service.ProviderService"/>

    

beans>

PS:URL需换成服务端暴露的URL
2. maven依赖
与服务端一致
3. 启动类

/**
 * xml的方式调用
 *
 */
public class ConsumerApplication
{
    public static void main( String[] args ) throws IOException {

        ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("consumer.xml");
        context.start();
        ProviderService providerService = (ProviderService) context.getBean("providerService");
        String str = providerService.SayHello("world!");
        System.out.println(str);
        System.in.read();

    }
}

Dubbo入门01_第6张图片
博文参考: https://segmentfault.com/a/1190000019896723.

你可能感兴趣的:(java,spring,intellij-idea,dubbo)