1 创建提供者
利用idea搭建。
1-1 创建maven项目
需求分析:
hello的demo,消费者传输用户名,提供者分解用户名返回hello + name。
2 创建提供者
创建用户实体 (provider\src\main\java\entity\userentity.java)
package entity;
public class userentity {
private String name;
private String sex;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}
创建接口(provider\src\main\java\service\userService.java):
package service;
import entity.userentity;
public interface userService {
public String SayHello(userentity user) ;
}
创建接口实现类(provider\src\main\java\service\impl\userserviceimpl.java):
package service.impl;
import com.alibaba.dubbo.config.annotation.Service;
import entity.userentity;
import service.userService;
public class userserviceimpl implements userService {
public String SayHello(userentity user) {
return "您好" + user.getName() + "性别:" + user.getSex();
}
}
/*
* 1、将服务器提供者注册到注册中心(暴露服务)
* 1)引入dubbo依赖和zookeeper客户端(curator)
* 2、让服务消费者去注册中心订阅服务提供者的服务地址。
* */
//报错处理:
// 1、java.lang.ClassNotFoundException: io.netty.channel.EventLoopGroup
// 这是因为dubbo和zookeeper,zookeeper客户端版本冲突问题,需要一同更新版本
/*
2、java.lang.IllegalStateException: Failed to register dubbo://192.168.25.10:20880/service.userService?anyhost=true&application=user-service-provider&bean.name=service.userService&dubbo=2.0.2&generic=false&interface=service.userService&methods=SayHello&pid=14916&side=provider×tamp=1558431972456 to registry 192.168.25.128:2181, cause: Failed to register dubbo://192.168.25.10:20880/service.userService?anyhost=true&application=user-service-provider&bean.name=service.userService&dubbo=2.0.2&generic=false&interface=service.userService&methods=SayHello&pid=14916&side=provider×tamp=1558431972456 to zookeeper zookeeper://192.168.25.128:2181/com.alibaba.dubbo.registry.RegistryService?application=user-service-provider&dubbo=2.0.2&interface=com.alibaba.dubbo.registry.RegistryService&pid=14916×tamp=1558431972444, cause: KeeperErrorCode = Unimplemented for /dubbo/service.userService/providers/dubbo%3A%2F%2F192.168.25.10%3A20880%2Fservice
这是因为dubbo的版本太低了,而zookeeper和其客户端版本高,提升dubbo版本,现在apache.dubbo的版本高。
3、Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/curator/framework/recipes/cache/TreeCacheListener
* */
到此,provider的服务已经写好了。
下面我来配置消费者。
3 配置提供者
pom 文件中,添加maven依赖。
<?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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.dubbo</groupId>
<artifactId>provider</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!-- 引入dubbo -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.6.2</version>
</dependency>
<!-- 由于我们使用zookeeper作为注册中心,所以需要操作zookeeper
dubbo 2.6以前的版本引入zkclient操作zookeeper
dubbo 2.6及以后的版本引入curator操作zookeeper
下面两个zk客户端根据dubbo版本2选1即可
-->
<!--<dependency>-->
<!--<groupId>com.101tec</groupId>-->
<!--<artifactId>zkclient</artifactId>-->
<!--<version>0.10</version>-->
<!--</dependency>-->
<!-- curator-framework -->
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>2.12.0</version>
</dependency>
</dependencies>
</project>
在resources文件夹中添加配置文件。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:cache="http://www.springframework.org/schema/cache"
xmlns:tx="http://code.alibabatech.com/schema/dubbo" 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://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!--1、制定当前服务的名称,(同样的服务名字项目,不要和其他的服务同名)-->
<dubbo:application name="provider"></dubbo:application>
<!--2/ 制定注册中心的位置-->
<dubbo:registry address="zookeeper://192.168.25.128:2181"></dubbo:registry>
<!--<dubbo:registry protocol="zookeeper" address="192.168.25.128:2181" />-->
<!--3/指定通信规则-->
<dubbo:protocol name="dubbo" port="20880" />
<!--4/ 声明需要暴露的服务接口 -->
<dubbo:service interface="service.userService" ref="userserviceimpl" />
<!--5/和本地bean一样实现服务 -->
<bean id="userserviceimpl" class="service.impl.userserviceimpl" />
<!--注意:第四步和第五步中的ref和id相对应。 -->
</beans>
配置provider的启动项:
创建\provider\src\main\java\Provider.java
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.io.IOException;
public class Provider {
public static void main(String[] args) throws IOException {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"provider.xml"});
context.start();
System.in.read(); // 按任意键退出
}
}
到此配置结束,启动项目:
我们可以在dubbo监控中心看到:
provider项目结构如下:
4 配置消费者
同样创建一个maven项目:
将provider项目中entity和serve接口复制到本项目中,此时大部分工作已经结束了。
下面就是配置和调用了。
5 配置消费者
引入依赖。
<?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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.dubbo</groupId>
<artifactId>consumer</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.6.2</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>2.12.0</version>
</dependency>
</dependencies>
</project>
创建配置文件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:dubbo="http://dubbo.apache.org/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
<!-- 应用名 -->
<dubbo:application name="consumer12"></dubbo:application>
<!-- 指定注册中心地址 -->
<dubbo:registry address="zookeeper://192.168.25.128:2181" />
<!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
<dubbo:reference interface="service.userService" id="userservice"></dubbo:reference>
<!--注意:dubbo:reference中的interface ,使用provider中暴露的端口 interface -->
</beans>
创建启动程序:
package testmain;
import entity.userentity;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Service;
import service.userService;
import java.io.IOException;
@Service
public class main {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("consumer.xml");
context.start();
userentity userentity=new userentity();
userentity.setName("2");
userentity.setSex("22");
userService demoService = context.getBean(userService.class); // 获取远程服务代理
String hello = demoService.SayHello(userentity); // 执行远程方法
System.out.println( hello ); // 显示调用结果
try {
System.in.read();
} catch (IOException e) {
e.printStackTrace();
}
}
}
运行consumer程序:
到此,demo已经搭建成功。
源码下载:
https://download.csdn.net/download/aiming66/11192954