标签:
正常一个服务不会只做客户端或者只做服务端,一般的微服务都是服务与服务相互调用,那么,应该怎么配置呢?接着之前的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>
代码地址:链接:https://pan.baidu.com/s/19NKvcCTVtbgP9Sz14oStGw 密码:3xew
标签: 来源: https://www.cnblogs.com/JAYIT/p/9645728.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。