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,但客户端越来越多时,,需要每 ...
随机推荐
- lua正则表达式如何匹配中文
function CheckChinese(s) local ret = {}; local f = '[%z\1-\127\194-\244][\128-\191]*'; local line, l ...
- springmvc+jsp 拦截器下如何设置欢迎页面
0.需求 如何让用户在浏览器地址栏键入[http://XXX.XXX.XX.XX:端口号/应用名/]以后自动跳转到系统的登录界面 1.web.xml 1.1 注意welcome-file-list的配 ...
- oracle创建HR示例数据库脚本hr_main.sql分享
需求描述: 今天一同事想要在测试库上进行SQL练习,帮忙安装下这个示例数据库,在此记录下. 操作过程: 1.上传hr_main.sql脚本(10g 11g都可用) 脚本下载链接: https://pa ...
- grid网格的流动grid-auto-flow属性
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- mysql的wait_timeout配置(此处处理方法是有问题的,不建议作为操作参考)
mysql数据库有一个wait_timeout的配置,默认值为28800(即8小时). 在默认配置不改变的情况下,如果连续8小时内都没有访问数据库的操作,再次访问mysql数据库的时候,mysql数据 ...
- linux-友好显示文件大小
4850905319b / 1024 /1024/1024 = 4.6G ls -lh
- iOS开发-修改UITableViewCell中image和title的位置和大小
最近在开发中遇到需要Cell中imageView和textLable位置和大小的情况,设计希望得到的结果如下图所示: 而TableViewCell默认样式,image是靠紧左边的,并且image和ti ...
- Git 学习笔记--Eclipse Git 插件安装
http://benjsicam.me/blog/how-to-setup-eclipse-git-plugin-egit-for-github-part-1-tutorial/ http://ben ...
- 使用 requests 发送 GET 请求
基本用法: import requests req = requests.get("http://www.baidu.com/") //发起GET请求 print(req.text ...
- Java中获取资源文件的方法总结
这里总结3中方法获取资源文件的 ServletContext Class ClassLoader 文件的位置 1. ServletContext public void doGet(HttpServl ...