美文网首页
dubbo依赖服务

dubbo依赖服务

作者: 先生_吕 | 来源:发表于2017-05-21 15:38 被阅读496次

    dubbo最亮点的功能就是解决跨项目的相互调用,最典型的案例模式即将service层注册成服务供controller模块调用即(controller -- service),但是在实际开发中,有可能service层还要依赖另一个service层才能实现功能,所以在这种情况下,一个service服务层是一个可以扮演双重角色的节点,它即是服务的提供者,又是服务的消费者(controller -- service1 -- service2 ... ...)

    2017-05-21_151026.png

    【案例】假设我们一个新的模块dependency要依赖helloWord案例的服务demo,即dependency服务service要依赖helloWord的service来完成实现,dependency的server既是服务也是消费
    【编码实现】

    【helloWord服务】
    查看helloWord案例

    【dependency服务】
    接口:dubbo.dependency.provider.DependencyService.java

    public interface DependencyService {
    
        public String dependency() throws Exception;
    }
    

    接口实现:dubbo.dependency.provider.impl.DependencyServiceImpl.java

    /**
     * 
     * @author lvfang
     * 这种方式符合常规的开发方式
     * 实际开发规则:
     *          服务方用注解方式
     *                  注入bean:org.springframework.stereotype.Service
     *                  提供服务:com.alibaba.dubbo.config.annotation.Service
     *                      interfaceClass:依赖的类
     *                      protocol:提供的服务,这里是Dubbo服务,还有其他服务
     *                      retries:失败重启次数,一把为0
     *          消费方用xml方式
     *                  注入bean
     */
    @Service("dependencyServiceImpl")//注入bean
    @com.alibaba.dubbo.config.annotation.Service(
            interfaceClass=DependencyService.class,
            protocol={"dubbo"},
            retries=0)
    public class DependencyServiceImpl implements DependencyService {
        
        @Autowired
        private SampleService sampleService;//这里要对另一个服务进行依赖
    
        @Override
        public String dependency() throws Exception {
            sampleService.sayHello("xiaojiang");
            System.out.println("假装模拟依赖调用了另一个已注册的服务");
            return "Dependency exec";
        }
    
    }
    

    服务测试:dubbo.dependency.test.Provider.java

    public class Provider {
    
        public static void main(String[] args) throws Exception {
            ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"dependency-provider.xml"});
            context.start();
            
            System.out.println("服务已启动");
            System.in.read();//为保证服务一直开着,利用输入流阻塞来模拟,开发中,这些应该打jar包在服务器上运行
        }
    }
    

    服务配置文件:dependency-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://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">
     
        <!-- 具体实现bean  扫描代替-->
        <dubbo:annotation package="dubbo.dependency"/>
       
        <!-- 提供方应用信息,用于计算依赖关系 -->
        <dubbo:application name="dependency-provider" />
     
        <!-- 使用zookeeper注册中心暴露服务地址 -->
        <dubbo:registry address="zookeeper://192.168.22.128:2181" />
     
        <!-- 用dubbo协议在20880端口暴露服务 -->
        <dubbo:protocol name="dubbo" port="20881" />
        
        <!-- 消费服务:这里这个provide服务依赖了另一个provide服务,check是用来检测依赖服务是否启动,默认为true -->
        <dubbo:reference id="sampleService" check="true" interface="dubbo.sample.provider.SampleService" />
     
    </beans>
    

    启动服务:
    注意,这里这个服务有依赖服务,并且xml中检查服务的配置属性 check="true",所以一定要保证依赖服务已启动,反之如果是false则启动不会报错

    启动依赖服务

    2017-05-21_152813.png

    启动服务

    2017-05-21_152852.png 2017-05-21_152941.png

    注意:以上服务以(服务用注解,消费用配置的方式完成)

    ![Uploading 2017-05-21_154059_680403.png . . .] 2017-05-21_154059.png

    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    【dependencyConsumer】消费服务
    接口:dubbo.dependency.provider.DependencyService.java

    public interface DependencyService {
    
        public String dependency() throws Exception;
    }
    

    消费测试类:Consumer.java

    public class Consumer {
    
        public static void main(String[] args) throws Exception {
            ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"dependency-consumer.xml"});
            context.start();
            
            DependencyService dependencyService = (DependencyService) context.getBean("dependencyService");//这里的id与consumer的配置对应
            
            System.out.println(dependencyService.dependency());
            
            System.in.read();
        }
    }
    

    xml配置:dependency-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://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="dependency-consumer"  />
     
        <!-- 使用zookeeper注册中心暴露发现服务地址 -->
        <dubbo:registry address="zookeeper://192.168.22.128:2181" />
     
        <!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
        <dubbo:reference id="dependencyService" interface="dubbo.dependency.provider.DependencyService" />
     
    </beans>
    

    启动测试

    2017-05-21_153607.png

    查看provider服务的控制台

    2017-05-21_153709.png

    相关文章

      网友评论

          本文标题:dubbo依赖服务

          本文链接:https://www.haomeiwen.com/subject/uzgmxxtx.html