Dubbo、Spring、Zookeeper、集成基础案例(XML方式)
程序员文章站
2022-05-07 14:24:07
...
摘要:最近抽时间系统的学习了Dubbo的一些内容,趁有时间,整理下,顺便记录下,以防以后回顾。
一:运行环境
1>:JDK 1.8
2>:IDEA 2018.1
3>:Zookeeper 3.x
4>:Maven 3.2
5>:Dubbo 2.8.4
二:项目结构
三:创建服务提供者工程
<?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">
<parent>
<artifactId>micai-dubbo-chapter1</artifactId>
<groupId>com.micai.dubbo</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>micai-dubbo-provider</artifactId>
<name>micai-dubbo-provider</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<spring.version>4.2.1.RELEASE</spring.version>
<jdk.version>1.8</jdk.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.8.4</version>
</dependency>
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.3</version>
</dependency>
</dependencies>
<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.0.0</version>
</plugin>
<!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.20.1</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
package com.micai.dubbo.provider;
/**
* @Auther: zhaoxinguo
* @Date: 2018/9/10 13:33
* @Description: 定义服务接口
*/
public interface DemoService {
String sayHello(String name);
}
package com.micai.dubbo.provider;
/**
* @Auther: zhaoxinguo
* @Date: 2018/9/10 13:34
* @Description: 在服务提供方实现接口
*/
public class DemoServiceImpl implements DemoService {
@Override
public String sayHello(String name) {
return "Hello" + name;
}
}
package com.micai.dubbo;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.io.IOException;
/**
* @Auther: zhaoxinguo
* @Date: 2018/9/10 14:13
* @Description:
*/
public class Provider {
public static void main(String [] args) {
ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
classPathXmlApplicationContext.start();
try {
System.in.read();//按任意键退出
} catch (IOException e) {
e.printStackTrace();
}
}
}
<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-4.2.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!--提供方应用信息,用于计算依赖关系-->
<dubbo:application name="hello-world-app"/>
<!--使⽤zookeeper⼴播注册中⼼暴露服务地址-->
<dubbo:registry protocol="zookeeper" address="127.0.0.1:2181"/>
<!--用dubbo协议在20880端口暴露服务-->
<dubbo:protocol name="dubbo" port="20880"/>
<!--声明需要暴露的服务接口-->
<dubbo:service interface="com.micai.dubbo.provider.DemoService" ref="demoService"/>
<!--和本地bean一样实现服务-->
<bean id="demoService" class="com.micai.dubbo.provider.DemoServiceImpl"/>
</beans>
四:创建服务消费者工程
<?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">
<parent>
<artifactId>micai-dubbo-chapter1</artifactId>
<groupId>com.micai.dubbo</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>micai-dubbo-consumer</artifactId>
<name>micai-dubbo-consumer</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring.version>4.2.1.RELEASE</spring.version>
<jdk.version>1.8</jdk.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.8.4</version>
</dependency>
<!--zookeeper-->
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.3</version>
</dependency>
<dependency>
<groupId>com.micai.dubbo</groupId>
<artifactId>micai-dubbo-provider</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.0.0</version>
</plugin>
<!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.20.1</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
package com.micai.dubbo;
import com.micai.dubbo.provider.DemoService;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @Auther: zhaoxinguo
* @Date: 2018/9/11 14:36
* @Description:
*/
public class Consumer {
public static void main(String [] args) {
ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
classPathXmlApplicationContext.start();
DemoService demoService = (DemoService) classPathXmlApplicationContext.getBean("demoService");
String str = demoService.sayHello("赵新国");
System.out.println("调用结果" + str);
}
}
<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-4.2.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!--消费方应用名、用于计算依赖关系,不是匹配条件,不要与提供方一样-->
<dubbo:application name="consumer-of-helloworld-app"/>
<!--使用zookeeper广播注册中心暴露发现服务地址-->
<dubbo:registry protocol="zookeeper" address="127.0.0.1:2181"/>
<!--生成远程服务代理,可以和本地bean一样使用demoService-->
<dubbo:reference id="demoService" interface="com.micai.dubbo.provider.DemoService"/>
</beans>
五:运行结果
六:源码下载请扫描下面的QQ交流群获取