dubbo xml 工程搭建

整体链路调用的流程:

 

① 消费者通过 Interface 进行方法调用,统一交由消费者端的 Proxy 通过 ProxyFactory 来进行代理对象的创建,使用到了 jdk javassist 技术

② 交给 Filter 这个模块,做一个统一的过滤请求,在 SPI 案例中涉及过

③ 接下来会进入最主要的 Invoker 调用逻辑

     通过 Directory 去配置中读取信息,最终通过 list 方法获取所有的 Invoker

     通过 Cluster 模块根据选择的具体路由规则来选取 Invoke 列表

     通过 LoadBalance模块,根据负载均衡策略,选择一个具体的 Invoker 来处理我们的请求

      如果执行中出现错误,并且 Consumer 配置类重试机制,则会重新尝试执行

④  继续经过 Filter 进行执行功能的前后封装 Invoker 选择具体的执行协议

⑤  客户端进行编码和序列化,然后发送数据

⑥   到达 Consumer 中的 Server 在这里进行 反编码 和 反序列化的接收数据

⑦   使用 Exporter 选择执行器

⑧   交给 Filter 进行一个提供者端的过滤,到达 Invoker 执行器

⑨   通过 Invoker 调用接口的具体实现,然后返回

1、新建主工程,pom 文件如下:

<properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <dubbo.version>2.7.5</dubbo.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.apache.dubbo</groupId>
                <artifactId>dubbo</artifactId>
                <version>${dubbo.version}</version>
            </dependency>
            <dependency>
                <groupId>org.apache.dubbo</groupId>
                <artifactId>dubbo-common</artifactId>
                <version>${dubbo.version}</version>
            </dependency>
            <dependency>
                <groupId>org.apache.dubbo</groupId>
                <artifactId>dubbo-registry-zookeeper</artifactId>
                <version>${dubbo.version}</version>
            </dependency>
            <dependency>
                <groupId>org.apache.dubbo</groupId>
                <artifactId>dubbo-registry-nacos</artifactId>
                <version>${dubbo.version}</version>
            </dependency>
            <dependency>
                <groupId>org.apache.dubbo</groupId>
                <artifactId>dubbo-rpc-dubbo</artifactId>
                <version>${dubbo.version}</version>
            </dependency>
            <dependency>
                <groupId>org.apache.dubbo</groupId>
                <artifactId>dubbo-remoting-netty4</artifactId>
                <version>${dubbo.version}</version>
            </dependency>
            <dependency>
                <groupId>org.apache.dubbo</groupId>
                <artifactId>dubbo-serialization-hessian2</artifactId>
                <version>${dubbo.version}</version>
            </dependency>
            <dependency>
                <groupId>org.apache.dubbo</groupId>
                <artifactId>dubbo-config-spring</artifactId>
                <version>${dubbo.version}</version>
            </dependency>
        </dependencies>

    </dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.5</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.7.5</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.47</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

2、新建子模块,公共api,主要是接口

public interface HelloService {
    String sayHello(String name);
}

3、新建子模块生产者,pom 文件如下

<dependencies>
        <dependency>
            <groupId>org.example</groupId>
            <artifactId>service-api</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-registry-zookeeper</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-rpc-dubbo</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-remoting-netty4</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-serialization-hessian2</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-config-spring</artifactId>
        </dependency>
    </dependencies>

新建生产者实现类

public class HelloServiceImpl implements HelloService {
    @Override
    public String sayHello(String name) {
        try {
            TimeUnit.SECONDS.sleep(5);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "hello:" + name;
    }
}

配置文件:dubbo-provider.xml

<?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.xsd
       http://dubbo.apache.org/schema/dubbo
       http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
    <!--提供应用方信息,用于计算依赖关系-->
       <dubbo:application name="service-provider"/>
    <!--使用zookeeper注册中心暴露服务地址-->
       <dubbo:registry address="zookeeper://101.132.167.18:2181?timeout=30000" id="r1"/>
    <!--使用dubbo协议在20882端口暴露服务,也可以使用 rmi 协议暴露-->
    <dubbo:protocol name="dubbo" port="20882"/>
    <!--声明需要暴露的服务接口-->
    <dubbo:service interface="com.lagou.service.HelloService" ref="helloService"/>
    <!--和本地bean一样实现服务-->
    <bean id="helloService" class="com.lagou.service.impl.HelloServiceImpl"/>

</beans>

配置文件:log4j.properties

log4j.rootCategory=INFO,CONSOLE

log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%d{HH:mm:ss.SSS} [%t] %-5p %c.%M\(%F:%L\) - %m%n

启动类:

public class ProviderApplication {
    public static void main(String[] args) throws IOException {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:dubbo-provider.xml");
        context.start();
        System.in.read();
    }
}

 4、新建子模块消费者,pom 文件如下:

<dependencies>
        <dependency>
            <groupId>org.example</groupId>
            <artifactId>service-api</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-registry-zookeeper</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-rpc-dubbo</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-remoting-netty4</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-serialization-hessian2</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-config-spring</artifactId>
        </dependency>
    </dependencies>

客户端调用类:

public class ConsumerApplication {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:dubbo-consumer.xml");
        HelloService helloService = (HelloService) context.getBean("helloService");
        String result = helloService.sayHello("world");
        System.out.println("--------------" + result);
    }
}



public class HelloServiceMock implements HelloService{
    @Override
    public String sayHello(String name) {
        return "hello mock";
    }
}

配置文件:dubbo-consumer.xml

<?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.xsd
       http://dubbo.apache.org/schema/dubbo
       http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
    <!--提供应用方信息,用于计算依赖关系-->
    <dubbo:application name="service-consumer">
        <dubbo:parameter key="qos.enable" value="true"/>
        <dubbo:parameter key="qos.port" value="33333"/>
        <!--是否可以远程调用,默认是true-->
        <dubbo:parameter key="qos.accept.foreign.ip" value="false"/>
    </dubbo:application>
    <!--设置超时时间,超时以后走mock 本地类,如果接口上也配置超时时间,以consumer配置时间为准-->
    <dubbo:consumer timeout="2000" check="false" retries="3"/>
    <!--使用zookeeper注册中心暴露服务地址-->
       <dubbo:registry address="zookeeper://101.132.167.18:2181?timeout=30000"/>
    <!--生成远程服务代理,可以和本地bean一样使用demoService-->
    <dubbo:reference id="helloService" interface="com.lagou.service.HelloService" mock="true" timeout="2000"/>

</beans>

配置文件:log4j.properties

log4j.rootCategory=ERROR,CONSOLE

log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%d{HH:mm:ss.SSS} [%t] %-5p %c.%M\(%F:%L\) - %m%n

启动类:

public class ConsumerApplication {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:dubbo-consumer.xml");
        HelloService helloService = (HelloService) context.getBean("helloService");
        String result = helloService.sayHello("world");
        System.out.println("--------------" + result);
    }
}

备注:当生产者和消费者都设置了超时时间,以消费者超时时间为准

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值