说到分布式,大家可能很快就能想到阿里巴巴的dubbo,zoookeeper集群,目前spring-cloud也是比较热门的,spring-cloud是基于spinrg-boot之上进行协作的,
<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.test</groupId>
<artifactId>dubbo_provider</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<!-- 此处为provider工程 依赖 dubbo-interface工程 的依赖配置 -->
<dependency>
<groupId>com.dubbo.test.interface</groupId>
<artifactId>dubbo_service_interface</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<!-- 配置Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>4.3.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>4.3.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.3.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>4.3.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>4.3.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-instrument</artifactId>
<version>4.3.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-instrument-tomcat</artifactId>
<version>4.3.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-messaging</artifactId>
<version>4.3.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>4.3.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-oxm</artifactId>
<version>4.3.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.3.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>4.3.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.3.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc-portlet</artifactId>
<version>4.3.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-websocket</artifactId>
<version>4.3.1.RELEASE</version>
</dependency>
<!-- end -->
<!-- dubbo -->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.8</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.4</version>
</dependency>
<dependency>
<groupId>org.javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.18.1-GA</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.6.1</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.6.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
<!-- end -->
</dependencies>
<!-- 项目发布仓库 -->
<!--<distributionManagement>
<repository>
<id>releases</id>
<name>Nexus Release Repository</name>
<url>http://localhost:8081/nexus/content/repositories/releases/</url>
</repository>
<snapshotRepository>
<id>snapshots</id>
<name>Nexus Snapshot Repository</name>
<url>http://localhost:8081/nexus/content/repositories/snapshots/</url>
</snapshotRepository>
</distributionManagement>-->
<!--注意:我这里把项目的发布仓库注释了,因为这里涉及到maven中settings.xml文件的配置以及nexus,只需要满足上面依赖的jar包存在就ok-->
</project>
接下来创建一个包,名为com.dubbo.test.provider,然后创建一个TestServiceImpl实现TestService接口,并且实现其接口类的方法。内容如下:
package com.dubbo.test.provider;
import com.dubbo.test.inter.service.TestService;
public class TestServiceImpl implements TestService{
/*
* @see com.dubbo.test.inter.service.TestService#getName(java.lang.String)
*/
public String getName(String name) {
return "hello " + name + ", success!!";
}
}
接着就是比较重要的一步,那就是spring整合dubbo的配置,主要配置接口的暴露,因为jar包都已经依赖进来了。创建spring-dubbo-provider.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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!-- 提供方应用信息,用于计算依赖关系 -->
<dubbo:application name="dubbo-service"/>
<!-- 使用zookeeper注册中心暴露服务地址 -->
<dubbo:registry address="zookeeper://localhost:2181"/>
<!-- 用dubbo协议在20880端口暴露服务 -->
<dubbo:protocol name="dubbo" port="20880" />
<!-- 声明需要暴露的服务接口 -->
<dubbo:service interface="com.dubbo.test.inter.service.TestService"
ref="testService" />
<bean id="testService" class="com.dubbo.test.provider.TestServiceImpl"/>
</beans>
到目前,dubbo的远程接口提供者已经实现了,接下来创建一个测试类,命名为:DubboTest,内容如下:
package com.test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class DubboTest {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring-dubbo-provider.xml");
context.start();
System.out.println("服务启动成功!!!" + context.getBean("testService"));
}
}
dubbo-provider测试方式: ①首先启动zookeeper服务 ②然后在运行DubboTest中的main方法,main方法启动后,TestService类中的接口就被暴露了 否则是肯定不能连上zookeeper的。
3、前两步已经把interface和provider创建好了,并且测试通过了,接下来我们来实现它的消费者。
创建一个maven工程名为: dubbo-consumer,该工程主要是远程调用dubbo-provider工程中暴露的接口来实现我们需要的业务开发。
首先pom文件的配置,这里我就不多说了,跟dubbo-provider工程中的pom.xml文件内容一模一样。
接下来就是spring整合dubbo,主要配置怎样使用到dubbo-provider工程中暴露的接口。创建一个spring-dubbo-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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!-- 提供方应用信息,用于计算依赖关系 -->
<dubbo:application name="dubbo_provider"/>
<!-- 使用zookeeper注册中心暴露服务地址 -->
<dubbo:registry address="zookeeper://localhost:2181"/>
<dubbo:consumer timeout="5000" />
<dubbo:reference id="testService" interface="com.dubbo.test.inter.service.TestService"/>
</beans>
最后就是测试了,创建一个DubboTest类,实现如下:
package com.test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.dubbo.test.inter.service.TestService;
public class DubboTest {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring-dubbo-consumer.xml");
context.start();
TestService testService = context.getBean(TestService.class);
System.out.println(testService.getName("xxx"));
System.out.println("dubbo success!!!!!");
}
}
到目前三个工程都已近完成了,接下来就是终极测试:
重点:
①启动zookeeper服务
②启动dubbo-provider工程中的main方法,暴露接口
③启动dubbo-consumer工程中的main方法,调用接口实现自己业务。
后续将会 继续把集成的知识点 和 其他的知识点补全(在web端查看暴露的服务并且设置)