分布式dubbo + zookeeper
程序员文章站
2022-05-07 14:30:36
...
步骤:
- 先新建三个Maven项目:
- 添加依赖包
- 遍写接口
- 编写服务
- 发布服务
|===配置服务名
|===配置注册中心
|===配置RPC的协议
|===发布服务 - 编写客户端
项目结构如下:
1.创建对应的目录结构:dubbo-demo-api
domain类:
package com.xinyi.shop_api.domain;
import java.io.Serializable;
public class HelloWorld implements Serializable{
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
service接口:
package com.xinyi.shop_api.service;
import com.xinyi.shop_api.domain.HelloWorld;
public interface IHelloWorldService {
HelloWorld hello( HelloWorld helloWorld );
}
pom:
<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.xinyi</groupId>
<artifactId>dubbo-demo-api</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>
2.创建对应的目录结构:dubbo-demo-server
serviceImpl类:
package com.xinyi.serviceImpl;
import com.xinyi.shop_api.domain.HelloWorld;
import com.xinyi.shop_api.service.IHelloWorldService;
public class HelloWorldServiceImpl implements IHelloWorldService{
public HelloWorld hello(HelloWorld helloWorld) {
helloWorld.setName("wency");
return helloWorld;
}
}
测试类模拟发布:
package com.xinyi.test;
import java.io.IOException;
import com.alibaba.dubbo.config.ApplicationConfig;
import com.alibaba.dubbo.config.ProtocolConfig;
import com.alibaba.dubbo.config.RegistryConfig;
import com.alibaba.dubbo.config.ServiceConfig;
import com.xinyi.serviceImpl.HelloWorldServiceImpl;
import com.xinyi.shop_api.service.IHelloWorldService;
public class HelloWorldServer {
IHelloWorldService helloWorldService = new HelloWorldServiceImpl();
public void hello(){
//1.当前应用配置
ApplicationConfig application = new ApplicationConfig();
//设置应用的名称
application.setName("HelloWorld");
//设置注册中心
RegistryConfig registry = new RegistryConfig();
//设置注册中心地址
registry.setAddress("zookeeper://127.0.0.1:2181");
//服务提供者协议
ProtocolConfig protocol = new ProtocolConfig();
//协议名
protocol.setName("dubbo");
//协议端口
protocol.setPort(20880);
//服务配置
ServiceConfig<IHelloWorldService> serviceConfig = new ServiceConfig<IHelloWorldService>();
//设置当前应用配置
serviceConfig.setApplication(application);
//设置当前注册中心配置
serviceConfig.setRegistry(registry);
//设置服务提供者配置
serviceConfig.setProtocol(protocol);
//设置服务的接口
serviceConfig.setInterface("com.xinyi.shop_api.service.IHelloWorldService");
//设置服务的具体实现类
serviceConfig.setRef(helloWorldService);
//发布服务
serviceConfig.export();
System.out.println("启动成功...");
try {
System.in.read();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args){
new HelloWorldServer().hello();
}
}
pom:
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.xinyi</groupId>
<artifactId>dubbo-demo-server</artifactId>
<packaging>war</packaging>
<version>1.0</version>
<name>dubbo-demo-server</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>com.xinyi</groupId>
<artifactId>dubbo-demo-api</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.1.6.RELEASE</version>
</dependency>
<!-- dubbo-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.3</version>
</dependency>
<!-- zookeeper -->
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.2</version>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.5</version>
</dependency>
</dependencies>
</project>
3.创建对应的目录结构:dubbo-demo-client
App类:
package com.xinyi.client;
import com.alibaba.dubbo.config.ApplicationConfig;
import com.alibaba.dubbo.config.ReferenceConfig;
import com.alibaba.dubbo.config.RegistryConfig;
import com.xinyi.shop_api.domain.HelloWorld;
import com.xinyi.shop_api.service.IHelloWorldService;
public class App {
public void hello(){
//配置服务名
ApplicationConfig applicationConfig = new ApplicationConfig();
applicationConfig.setName("helloWorldConsumer");
//配置注册中心
RegistryConfig registryConfig = new RegistryConfig();
registryConfig.setAddress("zookeeper://127.0.0.1:2181");
//引用远程服务
ReferenceConfig<IHelloWorldService> referenceConfig =new ReferenceConfig<IHelloWorldService>();
//设置服务配置
referenceConfig.setApplication(applicationConfig);
//设置注册中心配置
referenceConfig.setRegistry(registryConfig);
//设置接口
referenceConfig.setInterface("com.xinyi.shop_api.service.IHelloWorldService");
//获取服务
IHelloWorldService helloWorldService = referenceConfig.get();
HelloWorld helloWorld = new HelloWorld();
helloWorld = helloWorldService.hello(helloWorld );
System.out.println(helloWorld.getName());
}
public static void main(String[] args){
new App().hello();
}
}
pom:
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.xinyi</groupId>
<artifactId>dubbo-demo-client</artifactId>
<packaging>war</packaging>
<version>1.0</version>
<name>dubbo-demo-client</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>com.xinyi</groupId>
<artifactId>dubbo-demo-api</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.1.6.RELEASE</version>
</dependency>
<!-- dubbo-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.3</version>
</dependency>
<!-- zookeeper -->
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.2</version>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.5</version>
</dependency>
</dependencies>
</project>
使用zookeeper注册中心的配置:
- 下载zookeeper注册中心的jar包
- 修改conf文件夹里的zoo_sample.cfg文件,文件名改成zoo.cfg
- 修改zoo.cfg文件的以下配置:
#存在内存中的数据的快照位置:
dataDir=D:/zookeeper-3.4.5/data
#存储错误日志的位置:
dataLogDir=D:/zookeeper-3.4.5/log
#zookeeper的端口号:
clientPort=2181
- 双击bin目录下的zkServer.cmd文件
- 添加log4j.properties文件