欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

dubbo rest 服务学习笔记(二)【原创】

程序员文章站 2022-06-13 19:30:35
...
采用dubbo客户端访问dubbo发布的rest服务,接上一篇。
1)启动类
package cn.gov.zjport.dubborest.test;

public class DemoConsumer {

public static void main(String[] args) {
    com.alibaba.dubbo.container.Main.main(args);
}

}
2)beans-consumer.xml
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:cache="http://www.springframework.org/schema/cache" xmlns:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
       http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-4.0.xsd
       http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
       http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.0.xsd">
	<!--service服务 -->
	<import resource="classpath:/test/spring/consumer/spring-business-service.xml" />
	<import resource="classpath:/test/spring/consumer/spring-dubbo-service.xml" />
</beans>

3.spring 配置文件:spring-business-service.xml
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:cache="http://www.springframework.org/schema/cache" xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
       http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-4.0.xsd
       http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
       http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.0.xsd">
	<bean class="cn.gov.zjport.dubborest.test.DemoStart" init-method="start">
		<property name="restDemoService" ref="restDemoService" />
	</bean>
</beans>

4.dubbo配置文件:spring-dubbo-service.xml
<?xml version="1.0" encoding="UTF-8"?>
<!--
 - Copyright 1999-2011 Alibaba Group.
 -  
 - Licensed under the Apache License, Version 2.0 (the "License");
 - you may not use this file except in compliance with the License.
 - You may obtain a copy of the License at
 -  
 -      http://www.apache.org/licenses/LICENSE-2.0
 -  
 - Unless required by applicable law or agreed to in writing, software
 - distributed under the License is distributed on an "AS IS" BASIS,
 - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 - See the License for the specific language governing permissions and
 - limitations under the License.
-->
<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-2.5.xsd
	http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

    <dubbo:application name="dubborest-test-consumer" owner="dubborest" organization="zjport"/>

    <dubbo:registry address="zookeeper://192.168.3.110:2181"/>

    <!--uncomment this if you want to test dubbo's monitor-->
    <!--<dubbo:monitor protocol="registry"/>-->

    <dubbo:reference id="restDemoService" interface="cn.gov.zjport.dubborest.service.rest.RestDemoService"/>
</beans>

5. 客户端访问类:DemoStart
package cn.gov.zjport.dubborest.test;

import cn.gov.zjport.dubborest.pojo.User;
import cn.gov.zjport.dubborest.service.rest.RestDemoService;

public class DemoStart {

	private RestDemoService restDemoService;
	
	public void start(){
		System.out.println(restDemoService.search("zhenggm"));
		
		User user=new User();
		user.setId(1L);
		user.setName("zhengxl");
		System.out.println(restDemoService.post(user));
		System.out.println(restDemoService.get("zhangsan"));
	}

	public RestDemoService getRestDemoService() {
		return restDemoService;
	}

	public void setRestDemoService(RestDemoService restDemoService) {
		this.restDemoService = restDemoService;
	}
	
	
}

6. 运行 DemoConsumer
在run configuration 里设置 vm参数
-Ddubbo.spring.config=classpath:test/beans-consumer.xml
然后运行,就能打印出访问结果。
相关标签: dubbo rest