1. 注册中心zookeeper
1.1 下载
apache-zookeeper-3.6.3-bin.tar.gz
1.2 解压&启动
windows直接双击bin/zkServer.cmd
启动即可,系统一定要有Java环境。
2. 服务提供者Provider
2.1 pom依赖
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<!-- Spring的版本 -->
<spring.version>4.3.2.RELEASE</spring.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- Spring单元测试 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
<scope>test</scope>
</dependency>
<!-- Spring基础依赖 -->
<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-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- dubbo -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.3</version>
</dependency>
<!-- zookeeper -->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.6.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.101tec/zkclient -->
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.10</version>
</dependency>
</dependencies>
2.2 spring-dubbo.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">
<!--配置应用名-->
<dubbo:application name="demo-provider"/>
<!--配置注册中心-->
<dubbo:registry address="zookeeper://localhost:2181"/>
<!--配置服务协议-->
<dubbo:protocol name="dubbo" port="20890"/>
<!--配置服务接口暴露-->
<dubbo:service interface="cool.gjh.service.DemoService" ref="demoService"/>
<!--配置服务实现类暴露-->
<bean id="demoService" class="cool.gjh.service.impl.DemoServiceImpl"/>
</beans>
2.3 DemoService接口和实现类
接口:
package cool.gjh.service;
/**
* Dubbo暴露的接口
* @author ACE_GJH
* @date 2021/5/17
*/
public interface DemoService {
/**
* 说你好
* @param name 名字
* @return
*/
String hello(String name);
}
实现类:
package cool.gjh.service.impl;
import cool.gjh.service.DemoService;
/**
* Dubbo暴露的接口实现类
* @author ACE_GJH
* @date 2021/5/17
*/
public class DemoServiceImpl implements DemoService {
@Override
public String hello(String name) {
return "Hello, " + name;
}
}
2.4 启动类
package cool.gjh;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.io.IOException;
/**
* @author ACE_GJH
* @date 2021/05/17
*/
public class DubboProvider
{
public static void main( String[] args ) throws IOException {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring-dubbo.xml");
System.in.read();
}
}
3. 服务订阅者Consumer
3.1 pom依赖
同服务提供者Provider一样
3.2 spring-dubbo.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">
<!--配置应用名-->
<dubbo:application name="demo-consumer"/>
<!--配置注册中心-->
<dubbo:registry address="zookeeper://localhost:2181"/>
<!--配置服务协议-->
<dubbo:protocol name="dubbo" port="20890"/>
<!--配置消费服务-->
<dubbo:reference interface="cool.gjh.service.DemoService" id="demoService"/>
</beans>
3.3 DemoService接口
同服务提供者Provider一样
3.4 启动类
package cool.gjh;
import cool.gjh.service.DemoService;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @author ACE_GJH
* @date 2021/05/17
*/
public class DubboConsumer
{
public static void main( String[] args )
{
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring-dubbo.xml");
DemoService demoService = (DemoService)context.getBean("demoService");
String result = demoService.hello("Dubbo");
System.out.println(result);
}
}