正常一个服务不会只做客户端或者只做服务端,一般的微服务都是服务与服务相互调用,那么,应该怎么配置呢?接着之前的dubbo入门之helloWorld,我们再改改配置,即可实现正常的微服务架构。与之前相比,我新增了一个既做客户端又做服务端的工程。

HelloServerClientServiceImpl.java:

package com.sawshaw.dubbo_server_client;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import com.sawshaw.dubbo_interface.HelloInterface;
import com.sawshaw.dubbo_interface.HelloServerClientInterface;
@Service
public class HelloServerClientServiceImpl implements HelloServerClientInterface{
@Autowired
private HelloInterface hello; public String sayHi(String name) {
System.out.println("hello server-client......client send name is "+name);
String result=hello.sayHello(name);
return "hello-server-client "+result;
} }

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">
<!-- 提供方应用信息,用于计算依赖关系 -->
<dubbo:application name="provider"/>
<!-- 使用zookeeper注册中心暴露服务地址,这个地址是启动zookeeper默认暴露的地址 -->
<dubbo:registry address="zookeeper://127.0.0.1:2181"/>
<!-- 用dubbo协议在随机端口暴露服务 端口自己设置,如果被占用了就换一个端口 -->
<dubbo:protocol name="dubbo" port="20881" />
<!-- 声明需要暴露的服务接口 -->
<dubbo:service interface="com.sawshaw.dubbo_interface.HelloServerClientInterface" ref="hiService"/>
<!-- 和本地bean一样实现服务 -->
<bean id="hiService" class="com.sawshaw.dubbo_server_client.HelloServerClientServiceImpl"/>
</beans>

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">
<!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 ,要实现启动不报错要改成false-->
<dubbo:application name="consumer" default="false"/>
<!-- 连接注册中心配置 -->
<dubbo:registry address="zookeeper://127.0.0.1:2181" protocol="zookeeper" check="false"/>
<!-- 生成远程服务代理,可以和本地bean一样使用helloService check="false" 启动时不检查依赖是否已经启动-->
<dubbo:reference id="helloService" interface="com.sawshaw.dubbo_interface.HelloInterface" check="false"/>
</beans>

  

  

client调用,HelloClient.java:

package com.sawshaw.dubbo_client;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.sawshaw.dubbo_interface.HelloInterface;
import com.sawshaw.dubbo_interface.HelloServerClientInterface; public class HelloClient{
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
HelloInterface hs = (HelloInterface) context.getBean("helloService");
String result = hs.sayHello("dubbo");
System.out.println( "client output...."+result);
HelloServerClientInterface hi = (HelloServerClientInterface) context.getBean("hiService");
String result1 = hi.sayHi("dubbo");
System.out.println( "client output...."+result1);
} }

 client调server-client,server-client调server,所以server-client既是客户端又是服务端。

启动时检查

Dubbo缺省会在启动时检查依赖的服务是否可用,不可用时会抛出异常,阻止Spring初始化完成,以便上线时,能及早发现问题,默认check="true"。要改成

  <!-- 生成远程服务代理,可以和本地bean一样使用helloService  check="false" 启动时不检查依赖是否已经启动-->
<dubbo:reference id="helloService" interface="com.sawshaw.dubbo_interface.HelloInterface" check="false"/>

 当一个应用既当提供者和消费者时,必定会分别配置应用的名称,这时启动应用时就会报错:

java.lang.IllegalStateException: Duplicate application configs: <dubbo:application name="XXX" id="XXX" /> and <dubbo:application name="XXXX" id="XXXX" />

 解决办法是,在其中一个应用里面加上default="false",例如:

  <dubbo:application name="consumer" default="false"/>

  

代码地址:链接:https://pan.baidu.com/s/1h7pNFCSprX51CwUv7vHPFw 密码:cu6r

dubbo入门之微服务客户端服务端配置的更多相关文章

  1. spring cloud+dotnet core搭建微服务架构:配置中心(四)

    前言 我们项目中有很多需要配置的地方,最常见的就是各种服务URL地址,这些地址针对不同的运行环境还不一样,不管和打包还是部署都麻烦,需要非常的小心.一般配置都是存储到配置文件里面,不管多小的配置变动, ...

  2. spring cloud+dotnet core搭建微服务架构:配置中心续(五)

    前言 上一章最后讲了,更新配置以后需要重启客户端才能生效,这在实际的场景中是不可取的.由于目前Steeltoe配置的重载只能由客户端发起,没有实现处理程序侦听服务器更改事件,所以还没办法实现彻底实现这 ...

  3. spring cloud+.net core搭建微服务架构:配置中心续(五)

    前言 上一章最后讲了,更新配置以后需要重启客户端才能生效,这在实际的场景中是不可取的.由于目前Steeltoe配置的重载只能由客户端发起,没有实现处理程序侦听服务器更改事件,所以还没办法实现彻底实现这 ...

  4. spring cloud+.net core搭建微服务架构:配置中心(四)

    前言 我们项目中有很多需要配置的地方,最常见的就是各种服务URL地址,这些地址针对不同的运行环境还不一样,不管和打包还是部署都麻烦,需要非常的小心.一般配置都是存储到配置文件里面,不管多小的配置变动, ...

  5. Dubbo Ecosystem - 从微服务框架到微服务生态

    从微服务框架到微服务生态,这是微服务发展的必然趋势,也是Dubbo社区满足开发者更高效的构建微服务体系期望的使命和担当. 近期,Apache Dubbo PPMC 望陶(社区昵称:ralf0131)做 ...

  6. 移动端(钉钉微服务)webpack配置需要移除hash来解决应用更新后白屏的问题

    钉钉微服务webpack配置调整方案 1: Vue CLI配置修改方法 a. 修改build下webpack.prod.config.js.去掉图中三处hash(.[chunkhash]): b. 修 ...

  7. dubbo系列一、dubbo背景介绍、微服务拆分

    一.背景 随着互联网的发展,网站应用的规模不断扩大,常规的垂直应用架构已无法应对,分布式服务架构以及流动计算架构势在必行,亟需一个治理系统确保架构有条不紊的演进. 二.传统应用到分布式应用的演进过程 ...

  8. 2流高手速成记(之七):基于Dubbo&Nacos的微服务简要实现

    本节内容会用到之前给大家讲过的这两篇: 2流高手速成记(之六):从SpringBoot到SpringCloudAlibaba 2流高手速成记(之三):SpringBoot整合mybatis/mybat ...

  9. 微服务SpringCloud之配置中心和消息总线

    在微服务SpringCloud之Spring Cloud Config配置中心SVN博客中每个client刷新配置信息时需要post请求/actuator/refresh,但客户端越来越多时,,需要每 ...

随机推荐

  1. 磁盘IO概念及优化入门知识

    在数据库优化和存储规划过程中,总会提到IO的一些重要概念,在这里就详细记录一下,对这个概念的熟悉程度也决定了对数据库与存储优化的理解程度,以下这些概念并非权威文档,权威程度肯定就不能说了. 读/写IO ...

  2. python unittest 2

    参考资料:http://pyunit.sourceforge.net/pyunit_cn.html :http://docs.python.org/2/library/unittest.html py ...

  3. node.js和socket.io实现im

    im——Instant Messaging 即时通讯 基本技术原理 (1)通过IM服务器登陆或注销 (2)用户A通过列表找到B,用户B获得消息并与之交谈 (3)通过IM服务器指引建立与B单独的通讯通道 ...

  4. Spring3+mybatis3在多数据源情况下找不到数据库驱动的问题

    解决问题的过程如下: 1.遇到问题和一般的解决方法和下面这个帖子的一样: http://www.oschina.net/question/188964_32305 2.我在按照1的做法配置了以后,依然 ...

  5. linux中date命令显示昨天的日期信息?以特定格式显示时间?

    需求描述: linux环境中,在使用date命令的时候,可以通过-d指定日期的字符串来显示日期 操作过程: 1.通过date显示昨天的日期 [root@redhat6 ~]# date -d 'yes ...

  6. 【Nodejs】npm cnpm 淘宝镜像

    一.通过命令配置 1. 命令 npm config set registry https://registry.npm.taobao.org 2. 验证命令 npm config get regist ...

  7. MongoDB 备份恢复

    备份: mongodump --host -u admin -p -o /tmp/alldb/ // 备份所有的库 mongodump --host -u admin -p -d mydb -o /t ...

  8. JAVA对URL的解码【转】

    前段时间做URL的中文转换,有些url是utf8的格式,有的是gb2312的格式,很难区分到底是utf8还是gb2312,找了好久,发现网上的一个牛人写的转换代码: package org.apach ...

  9. 在RDLC报表中对纸张的设置

    RDLC报表是存放成XML文件格式的,这一点你可以直接打开RDLC报表文件看一下,而且在使用时,通过ReportViewer来读取报表并与数据源进行合成,也就是说RDLC是定义了一个格式,那就不能通过 ...

  10. SVN的基本原理 配置自动更新WEB服务器

    SVN的基本原理 配置自动更新WEB服务器 最近有个小项目,需要用SVN来进行版本控制.项目组的同僚有8个人,大家都在本地开发,然后提交到服务器——服务器就是其中一台机器.专门安排一个测试员来进行项目 ...