`

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

阅读更多
采用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学习笔记.pdf

    【Dubbo学习笔记】 Dubbo是一款高性能、轻量级的Java RPC框架,它旨在提供面向接口的远程方法调用、智能容错与负载均衡、服务自动注册与发现等核心功能,帮助开发者构建分布式服务架构。以下是对Dubbo基础知识、...

    dubbo学习例题代码资源

    - **协议(Protocol)**: 服务交互的通信协议,如Dubbo默认的dubbo协议,HTTP,REST等。 - **监控中心(Monitor)**: 统计服务的调用次数、调用时间、失败比例等,用于故障排查和性能优化。 2. **Zookeeper在...

    dubbo:dubbo学习笔记

    《Dubbo学习笔记详解》 Dubbo,作为阿里巴巴开源的一款高性能、轻量级的服务治理框架,已经在Java开发者中广受欢迎。它主要应用于分布式系统中的服务调用,提供服务注册、服务发现、负载均衡、容错处理等核心功能,...

    springcloud笔记.pdf

    【SpringCloud笔记】是关于微服务架构的学习资料,主要涵盖了SpringCloud的基本概念、核心组件以及实战技巧。在学习SpringCloud之前,需要具备一定的基础知识,包括IDEA、JDK8、Maven、SpringBoot以及Linux环境的...

    周阳SpringCloud课堂笔记

    # SpringCloud课堂笔记知识点解析 ## 一、微服务概述 ### 1.1 微服务定义 微服务架构作为一种新兴的设计模式,旨在通过将单个应用程序分解为多个小型、独立的服务来提升软件的可扩展性和灵活性。这些服务通常遵循...

    SpringCloud微服务笔记

    5. **Dubbo**:阿里巴巴开源的服务框架,支持高性能的 RPC 服务输出和输入,与 Spring 框架无缝集成。 #### 八、微服务架构 1. **定义**:微服务架构是一种将应用分解为一系列小服务的设计模式,每个服务独立运行...

    drp项目 开发 Java程序员

    12. **微服务架构**:理解微服务的概念,如何通过Spring Cloud或Dubbo实现服务拆分,提高系统的可扩展性和可维护性。 13. **DRP业务流程**:深入理解DRP业务流程,包括采购、销售、库存管理等,将业务逻辑有效地...

Global site tag (gtag.js) - Google Analytics