dubbo依赖服务

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
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,800评论 19 139
  • 0 准备 安装注册中心:Zookeeper、Dubbox自带的dubbo-registry-simple;安装Du...
    七寸知架构阅读 14,736评论 0 88
  • 查看索引 · Table表的名称。· Non_unique如果索引不能包括重复词,则为0。如果可以,则为1。· K...
    光剑书架上的书阅读 4,943评论 0 3
  • 今天去了31团幼儿园,下午干了半天,很熟悉的感觉,每个孩子生来都是天使,希望自己在今后的工作中不要去做那个折断天使...
    鹃儿阅读 715评论 0 0
  • 有言传 春秋秦穆公有女弄玉 弄玉笛响 箫史以箫相和 知音相遇 倾盖如故 携手眷侣 乘凤凰翔于天 凤翔由此而得名 人...
    公子段_阅读 1,527评论 0 0