学习目录在上一篇中有了,这篇我们来写一个本地的单服务的demo,下面我们看看需要什么!!!
1、zookeeper的安装:
(1)、zookeeper 的安装很简单,下载架包解压缩,解压缩后的目录下有个conf目录也就是zookeeper的配置文件在该目录下,该目录下有一个zoo_sample.cfg 文件,复制一个修改为zoo.cfg,像我们这里是单服务的所以这里的配置只需要修改dataDir的目录就可以了其他的可以不用修改,dataDir的目录可以自己随便定义!!!
2、服务提供者
创建一个maven 工程,配置spring ,dubbo,zookeeper等相关的jar包,下面来具体看看!
(1)、目录结构
(2)、我们配置pom.xml下载我们需要的jar包,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.test</groupId>
- <artifactId>dubboser</artifactId>
- <version>0.0.1</version>
- <packaging>jar</packaging>
- <name>dubboserver</name>
- <url>http://maven.apache.org</url>
- <properties>
- <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
- <spring.version>3.1.4.RELEASE</spring.version>
- <slf4j.version>1.6.6</slf4j.version>
- </properties>
- <dependencies>
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <version>3.8.1</version>
- <scope>test</scope>
- </dependency>
- <!-- Spring -->
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-aop</artifactId>
- <version>${spring.version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-asm</artifactId>
- <version>${spring.version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-core</artifactId>
- <version>${spring.version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-beans</artifactId>
- <version>${spring.version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-context</artifactId>
- <version>${spring.version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-expression</artifactId>
- <version>${spring.version}</version>
- </dependency>
- <!-- spring end -->
- <!-- log -->
- <dependency>
- <groupId>log4j</groupId>
- <artifactId>log4j</artifactId>
- <version>1.2.16</version>
- </dependency>
- <dependency>
- <groupId>org.slf4j</groupId>
- <artifactId>slf4j-api</artifactId>
- <version>${slf4j.version}</version>
- </dependency>
- <dependency>
- <groupId>org.slf4j</groupId>
- <artifactId>slf4j-log4j12</artifactId>
- <version>${slf4j.version}</version>
- </dependency>
- <!-- dubbo -->
- <dependency>
- <groupId>com.alibaba</groupId>
- <artifactId>dubbo</artifactId>
- <version>2.5.3</version>
- </dependency>
- <!-- zkclient -->
- <dependency>
- <groupId>com.github.sgroschupf</groupId>
- <artifactId>zkclient</artifactId>
- <version>0.1</version>
- </dependency>
- <!-- zookeeper -->
- <dependency>
- <groupId>org.apache.zookeeper</groupId>
- <artifactId>zookeeper</artifactId>
- <version>3.4.5</version>
- </dependency>
- </dependencies>
- <build>
- <finalName>dubbo-demo</finalName>
- <plugins>
- <!-- 非多个资源配置 start-->
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-compiler-plugin</artifactId>
- <version>2.1</version>
- <configuration>
- <source>1.5</source>
- <target>1.5</target>
- <encoding>UTF-8</encoding>
- <failOnError>false</failOnError>
- </configuration>
- </plugin>
- <!-- 非多个资源配置 end-->
- </plugins>
- </build>
- </project>
(3)、写服务接口,服务接口的实现
- package com.test.dubboser;
- public interface ServiceDemo {
- public String say(String str);
- }
- package com.test.dubboser;
- public class ServiceImp implements ServiceDemo{
- public String say(String str) {
- System.err.println("server: "+str);
- return "hello: "+str;
- }
- }
(4)、向注册中心注册服务(将接口暴露出去),这里的注册中心是zookeeper
下面我们来看看,服务的配置也就是applicationProvider.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
- ">
- <!-- 提供方应用信息,用于计算依赖关系,这个和client没必要一致 -->
- <dubbo:application name="hello-world-app-my" />
- <!-- 使用multicast广播注册中心暴露服务地址 -->
- <!-- <dubbo:registry address="multicast://192.168.0.101:2181" /> -->
- <!-- 使用zookeeper广播注册中心暴露服务地址 -->
- <dubbo:registry protocol="zookeeper" address="192.168.0.101:2181"/>
- <!--和上面的配置是一样的效果 -->
- <!-- <dubbo:registry address="zookeeper://192.168.0.101:2181" /> -->
- <!-- 本机 伪集群 测试 -->
- <!-- <dubbo:registry protocol="zookeeper" address="192.9.145.19:2181,192.9.145.19:2182,192.9.145.19:2183" /> -->
- <!-- 用dubbo协议在20880端口暴露服务 -->
- <dubbo:protocol name="dubbo" port="20880" />
- <!-- 声明需要暴露的服务接口 -->
- <dubbo:service interface="com.test.dubboser.ServiceDemo"
- ref="demoService" />
- <!-- 和本地bean一样实现服务 -->
- <bean id="demoService" class="com.test.dubboser.ServiceImp"/>
- </beans>
(5)、所有的准备都好了,下面我们启动服务提供者(提前启动zookeeper)
- package com.test.dubboser;
- import java.io.IOException;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- public class Main {
- public static void main(String[] args) throws IOException {
- ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "applicationProvider.xml" });
- context.start();
- System.out.println("按任意键退出");
- System.in.read();
- }
- }
ok这样我们的服务提供者就好了,是不是很乱,我感觉也有点,所以之后会有个自己的小总结……
我们的服务提供者好了,我们来看看我们的服务消费者,也就是调用我们这里的服务的程序……
愿意了解框架技术或者源码的朋友直接求求交流分享技术:2042849237
分布式的一些解决方案,有愿意了解的朋友可以找我们团队探讨
更多详细源码参考来源:http://minglisoft.cn/technology
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/31452580/viewspace-2147017/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/31452580/viewspace-2147017/