lion:dubbo服務的提供方糜俗,即服務端
項目地址:https://github.com/BruceZhangXL/lion
wolf:dubbo服務的調用方,即客戶端
項目地址:https://github.com/BruceZhangXL/wolf
wolf項目也是基于spring boot搭建的咱台,結構和lion類似,下面我主要說下,對dubbo服務的調用,作為客戶端這一側啊胶,要做哪些配置。
1垛贤、在wolf-rpc模塊依賴服務端的一些接口jar包焰坪,主要是lion-domain和lion-export
2、在wolf-rpc中增加dubbo調用側的一些配置spring-dubbo.xml,spring-goods-consumer.xml
其中spring-dubbo.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="${server.name}"/>
<dubbo:protocol name="dubbo" port="${dubbo.port}" />
<dubbo:provider timeout="3000" threadpool="fixed" threads="1000" accepts="1000" />
<dubbo:registry id="registry" protocol="zookeeper" address="${zookeeper.address}" />
</beans>
spring-goods-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:reference id="helloService" interface="org.lion.export.HelloService" version="${dubbo.version}" timeout="${dubbo.timeout}"/>
</beans>
3、在service層使用這個helloService
@Service("itemService")
public class ItemServiceImpl implements ItemService{
@Resource
private ItemDraftMapper itemDraftMapper;
@Resource
private HelloService helloService;
使用@Resource注入該遠端服務(實際上此時注入的是遠端服務的一個代理類)
4善绎、增加測試controller
@Controller
@RequestMapping("dubbo")
public class DubboTestController {
@Resource
private ItemService itemService;
@RequestMapping("")
@ResponseBody
public HelloDomain sayHi(String name){
return itemService.sayHi(name);
}
}
5黔漂、修改wolf項目端口為8082,啟動項目后測試
6禀酱、看看duboo-admin上炬守,客戶端是否注入
下圖可以看到客戶端項目wolf已經可以看到了。