dubbo 实战1
准备:先安装zookeeper,然后进入一下步骤
服务端
1.新建 maven项目
2.pom.xml 内容如下
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.demo</groupId>
<artifactId>test</artifactId>
<version>1.0-SNAPSHOT</version> <dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.2.6.RELEASE</version>
</dependency> <dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.3</version>
</dependency>
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.9</version>
</dependency>
</dependencies> </project>
3.在resource里新建 applicationContext.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
">
<!-- 具体的实现bean -->
<bean id="animalService"
class="com.dubbo.controller.Animal" />
<!-- 提供方应用信息,用于计算依赖关系 -->
<dubbo:application name="anyname_provider" />
<!-- 使用zookeeper注册中心暴露服务地址 -->
<dubbo:registry address="zookeeper://127.0.0.1:2181" />
<!-- 用dubbo协议在20880端口暴露服务 -->
<dubbo:protocol name="dubbo" port="20881" />
<!-- 声明需要暴露的服务接口 -->
<dubbo:service interface="com.dubbo.controller.IAnimal"
ref="animalService" />
</beans>
4.新建IAnimal接口
package com.dubbo.controller; public interface IAnimal { public String say();
}
5.IAnimal接口的实现
package com.dubbo.controller; import com.dubbo.controller.IAnimal; public class Animal implements IAnimal {
public String say(){ return "i am say 1";
}
}
6.启动该应用(会注册到zookeeper)
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Test1 {
public static void main(String[] args) throws Exception { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"applicationContext.xml"});
context.start(); System.out.println(" app run "); System.in.read(); // 按任意键退出
}
}
启动后显示“ app run ” 则表示启动成功
客户端
1.新建maven 项目 dubbo-controller
2.pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.dubbo</groupId>
<artifactId>dubbo-controller</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging> <name>dubbo-contoller</name>
<url>http://maven.apache.org</url> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!-- 设置 Spring 的版本 -->
<org.springframework.version>3.1.4.RELEASE</org.springframework.version>
</properties> <dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${org.springframework.version}</version>
</dependency> <!-- json start -->
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-core-asl</artifactId>
<version>1.9.12</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.12</version>
</dependency>
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.4</version>
<classifier>jdk15</classifier>
</dependency>
<!--json end --> <!-- dubbo start -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.3</version>
<exclusions>
<exclusion>
<artifactId>spring</artifactId>
<groupId>org.springframework</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.3.6</version>
<exclusions>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</version>
</dependency>
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
<!-- dubbo end --> <dependency>
<groupId>com.dubbo</groupId>
<artifactId>dubbo-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<finalName>dubbo-controller</finalName>
</build>
</project>
3.新建 applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd"> <import resource="dubbo-config.xml" />
<import resource="spring-mvc.xml" />
</beans>
4.新建dubbo-config.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"
xmlns:context="http://www.springframework.org/schema/context"
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 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <dubbo:application name="consumer_app" /> <dubbo:registry address="zookeeper://127.0.0.1:2181" />
<dubbo:consumer timeout="5000" /> <dubbo:reference id="demoService"
interface="Provider" /> <dubbo:reference id="animalService"
interface="com.dubbo.controller.IAnimal" /> </beans>
5.新建 spring-mvc.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd"> <context:component-scan base-package="com.dubbo" /> <mvc:annotation-driven /> </beans>
6.新建接口 IAnimal
package com.dubbo.controller; public interface IAnimal {
public String say();
}
7.新建MyController
package com.dubbo.controller; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody; import com.dubbo.service.TestService; /**
* controller
* @author hwy
*
*/
@Controller
public class MyController { @Autowired
private IAnimal testService; @RequestMapping(value = "/test")
@ResponseBody
public String testSay(@RequestParam(value = "name",defaultValue = "") String name){
StringBuffer sb = new StringBuffer();
sb.append("aaa123456789001");
sb.append("Dubbo: ").append(testService.say());
return sb.toString();
} }
8.web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1"> <display-name>dubbo-controller</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</init-param>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
在Tomcat里运行该应用
访问:http://127.0.0.1:8080/dubbo-controller/test?name=hwy
源码:https://pan.baidu.com/s/1miBt9Ja
dubbo 实战1的更多相关文章
- dubbo实战之一:准备和初体验
欢迎访问我的GitHub https://github.com/zq2599/blog_demos 内容:所有原创文章分类汇总及配套源码,涉及Java.Docker.Kubernetes.DevOPS ...
- dubbo实战之二:与SpringBoot集成
欢迎访问我的GitHub https://github.com/zq2599/blog_demos 内容:所有原创文章分类汇总及配套源码,涉及Java.Docker.Kubernetes.DevOPS ...
- dubbo实战之三:使用Zookeeper注册中心
欢迎访问我的GitHub https://github.com/zq2599/blog_demos 内容:所有原创文章分类汇总及配套源码,涉及Java.Docker.Kubernetes.DevOPS ...
- dubbo实战之四:管理控制台dubbo-admin
欢迎访问我的GitHub https://github.com/zq2599/blog_demos 内容:所有原创文章分类汇总及配套源码,涉及Java.Docker.Kubernetes.DevOPS ...
- SpringBoot-13 Dubbo实战
SpringBoot-13 Dubbo实战 前提: 已经准备好Dubbo-admin和Zookeeper 前置准备 1.创建项目 显示创建一个Empty Project,创建两个Module---&g ...
- [dubbo实战] dubbo+zookeeper伪集群搭建
zookeeper作为注册中心,服务器和客户端都要访问,如果有大量的并发,肯定会有等待.所以可以通过zookeeper集群解决. 一.为什么需要zookeeper呢? 大部分分布式应用需要一个主控.协 ...
- Dubbo实战快速入门 (转)
Dubbo是什么? Dubbo[]是一个分布式服务框架,致力于提供高性能和透明化的RPC远程服务调用方案,以及SOA服务治理方案. 其核心部分包含: 远程通讯: 提供对多种基于长连接的NIO框架抽象封 ...
- 【分布式系列之dubbo】SSM+Dubbo实战
对于传统的单一构架,也就是打成的war或者ear包部署在同一个Web容器里的构架,它虽然开发.测试.部署简单,但随着业务的不断发展,维护成本增加,可扩展性差,而且一台Tomcat的并发数在500左右, ...
- dubbo 实战
dubbo 官网:http://dubbo.apache.org/zh-cn/docs/user/quick-start.html dubbo-admin 下载 : https://github.co ...
- [dubbo实战] dubbo+zookeeper伪集群搭建 (转)
zookeeper作为注册中心,服务器和客户端都要访问,如果有大量的并发,肯定会有等待.所以可以通过zookeeper集群解决. 一.为什么需要zookeeper呢? 大部分分布式应用需要一 个主控. ...
随机推荐
- 关于 __int128
__int128 是 GCC 提供的扩展(extension),可以当作 128 位整数使用. 关于 __int128 和 __int128_t Normally, _t suffix means a ...
- hihoCoder #1157 建造金字塔
这道题我想了一天才想清楚做法.AC 了之后去看别人写的题解,都是三言两语意识流式描述,我并不能读懂.我觉得很自卑,为何人家解这道题如此轻松.不过,我还是决定把我的解法写下来,并且一定要写清楚. 思路 ...
- POJ——1321棋盘问题(DFS+回溯)
棋盘问题 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 33272 Accepted: 16456 Description 在一 ...
- RobotFramwork自定义库
这么长时间才知道有RobotFramwork这东西... 感叹之前都干啥去了,感叹公司为啥不用这货? 网上的安装文档都有,就不用自己在记录啦. 感觉在实际实用时,肯定要有自己定义的库啊,不能只用bui ...
- foj 2150 bfs
题意: 给定一个平面图 . 为空地(不着火) # 为草 开始可以选1-2个草堆点燃,每隔一秒会把上下左右的草引燃(开始时间为0秒) 问把所有草烧光的最少时间 #include<iostream& ...
- C语言指针与数组
C语言指针与数组 数组的下标应该从0还是1开始? 我提议的妥协方案是0.5,可惜他们未予认真考虑便一口回绝 -- Stan Kelly-Bootle 1. 数组并非指针 为什么很多人会认为指 ...
- Best Coder Lotus and Characters
Lotus and Characters 问题描述 Lotus有nn种字母,给出每种字母的价值以及每种字母的个数限制,她想构造一个任意长度的串. 定义串的价值为:第1位字母的价值*1+第2位字母的 ...
- 【Visual Studio】Visual Studio 2015快捷键设置问题 alt+ F8 (转)
具体修改方法如下: 工具-选项-环境-键盘-应用以下其他键盘映射方案,选择visual C++6, 然后编代码试试,嘿,我的alt+F8回来了(否则是ctrl + k, ctrl + f); 原文转自 ...
- Struts学习总结(一)
1,需要包下载地址: http://archive.apache.org/dist/struts/binaries/ 2.报错:org/apache/commons/lang3/StringUtils ...
- es6 const let
一.const 1.const 声明的是常量,一旦声明,值将是不可变的: 2.const也具有块级作用域: 3.不能变量提升(必须先声明后使用): 4.const 不可重复声明 5.const 指令指 ...