dubbo入门之微服务客户端服务端配置
正常一个服务不会只做客户端或者只做服务端,一般的微服务都是服务与服务相互调用,那么,应该怎么配置呢?接着之前的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入门之微服务客户端服务端配置的更多相关文章
- spring cloud+dotnet core搭建微服务架构:配置中心(四)
前言 我们项目中有很多需要配置的地方,最常见的就是各种服务URL地址,这些地址针对不同的运行环境还不一样,不管和打包还是部署都麻烦,需要非常的小心.一般配置都是存储到配置文件里面,不管多小的配置变动, ...
- spring cloud+dotnet core搭建微服务架构:配置中心续(五)
前言 上一章最后讲了,更新配置以后需要重启客户端才能生效,这在实际的场景中是不可取的.由于目前Steeltoe配置的重载只能由客户端发起,没有实现处理程序侦听服务器更改事件,所以还没办法实现彻底实现这 ...
- spring cloud+.net core搭建微服务架构:配置中心续(五)
前言 上一章最后讲了,更新配置以后需要重启客户端才能生效,这在实际的场景中是不可取的.由于目前Steeltoe配置的重载只能由客户端发起,没有实现处理程序侦听服务器更改事件,所以还没办法实现彻底实现这 ...
- spring cloud+.net core搭建微服务架构:配置中心(四)
前言 我们项目中有很多需要配置的地方,最常见的就是各种服务URL地址,这些地址针对不同的运行环境还不一样,不管和打包还是部署都麻烦,需要非常的小心.一般配置都是存储到配置文件里面,不管多小的配置变动, ...
- Dubbo Ecosystem - 从微服务框架到微服务生态
从微服务框架到微服务生态,这是微服务发展的必然趋势,也是Dubbo社区满足开发者更高效的构建微服务体系期望的使命和担当. 近期,Apache Dubbo PPMC 望陶(社区昵称:ralf0131)做 ...
- 移动端(钉钉微服务)webpack配置需要移除hash来解决应用更新后白屏的问题
钉钉微服务webpack配置调整方案 1: Vue CLI配置修改方法 a. 修改build下webpack.prod.config.js.去掉图中三处hash(.[chunkhash]): b. 修 ...
- dubbo系列一、dubbo背景介绍、微服务拆分
一.背景 随着互联网的发展,网站应用的规模不断扩大,常规的垂直应用架构已无法应对,分布式服务架构以及流动计算架构势在必行,亟需一个治理系统确保架构有条不紊的演进. 二.传统应用到分布式应用的演进过程 ...
- 2流高手速成记(之七):基于Dubbo&Nacos的微服务简要实现
本节内容会用到之前给大家讲过的这两篇: 2流高手速成记(之六):从SpringBoot到SpringCloudAlibaba 2流高手速成记(之三):SpringBoot整合mybatis/mybat ...
- 微服务SpringCloud之配置中心和消息总线
在微服务SpringCloud之Spring Cloud Config配置中心SVN博客中每个client刷新配置信息时需要post请求/actuator/refresh,但客户端越来越多时,,需要每 ...
随机推荐
- redhat enterprise edition 6.8:禁止ipv6后,nfs文件系统无法挂载:no such device
如题:谨记. 附注:如何禁止ipv6? 方法一 第一种方法是通过 /etc/sysctl.conf 文件对 /proc 进行永久修改. 换句话说,就是用文本编辑器打开 /etc/sysctl.conf ...
- 比SRCNN效果好的传统超分辨率算法汇总
1.基于深度协作表达的人脸图像超分辨率算法研究与应用_百度学术 采用一种深度协作表达算法框架,构造深度的多线性模型 分段拟合高低分辨率图像块之间的非线性关系,本文算法简洁高效,提供了一种新的深度学习模 ...
- Cannot call sendError() after the response has been committed - baiyangliu
当response提交后,不能调用sendError(),什么意思? 出现这个错误,一定是多次response导致的.可以这么理解,承载客户端和服务器进行Http交互的Socket连接已经关闭了,而你 ...
- SSL证书/TLS证书是什么
https://blog.csdn.net/donghaixiaolongwang/article/details/79193695 A. SSL协议与TLS是什么?它们的功能是什么? 答:SSL(S ...
- C#------如何深度克隆一个对象
普通版: public static object CloneObject( object obj ) { using ( MemoryStream memStream = new MemoryStr ...
- Linux+Redis实战教程_day01_Linux系统上安装tomcat
Linux系统上安装tomcat 安装tomcat 上传tomcat的安装文件 Alt+p 拖拽上传 创建tomcat的安装路径 mkdir -p /usr/local/tomcat 解压tomcat ...
- Go错误处理(二)
1,.error接口的定义 type error interface{ Error()string } 2.error的使用 func Foo(param int)(n int,err error){ ...
- BootStrap Table将时间戳更改为日期格式
一.使用BootStrap Table遇到的问题: 1.MyBatis从数据库中取出的时间格式如下:2017-12-04 21:43:19.0,时间后面多了一个点零. 2.从BootStrap Tab ...
- U3D 游戏引擎之游戏架构脚本该如何来写
这篇文章MOMO主要想大家说明一下我在Unity3D游戏开发中是如何写游戏脚本的,对于Unity3D这套游戏引擎来说入门极快,可是要想做好却非常的难.这篇文章的目的是让哪些已经上手Unity3D游戏引 ...
- Nginx 72万连接性能测试(一)
转自:http://my.oschina.net/chenzhuo/blog/150200?p=2#comments 根据系统内存64G估算单台tengine做反向代理最高支持72万连接.为了验证达到 ...