下载并安装Zookeeper:http://www.apache.org/dist/zookeeper/
下载后解压缩到本地,进入【conf】文件夹,将文件【zoo_sample.cfg】改为【zoo.cfg】,因为Zookeeper 在启动时会找zoo.cfg这个文件作为默认配置文件。
进入【bin】文件夹,双击【zkServer.cmd】文件,启动Zookeeper。
下载的 Dubbo-2.5.8
https://github.com/apache/incubator-dubbo/releases
下载后解压出来如下:
编译打包
切换到dubbo-admin
目录下,执行 mvn clean compile
进行编译,编译成功后执行 mvn clean package
来进行打包,打包成功后,会在target
文件夹里生成war:
将war包放到tomcat里面部署启动,dubbo-admin\src\main\webapp\WEB-INF
文件夹下的dubbo.properties
文件里面配置。
zookeeper地址和登录dubbo的账号密码。
springboot 生产者
接口
package com.example.product.service;
import java.util.List;
public interface findUser {
/**
* 查询信息
* @return
*/
List<String> list();
}
实现类
package com.example.product.service.serviceImpl;
import com.alibaba.dubbo.config.annotation.Service;
import com.example.product.service.findUser;
import java.util.ArrayList;
import java.util.List;
@Service
public class findUserImpl implements findUser {
@Override
public List<String> list() {
List<String> list = new ArrayList<String>();
list.add("hello,duboo-provider");
return list;
}
}
pom依赖
<!-- Spring Boot Dubbo 依赖-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.6.1</version>
</dependency>
<dependency>
<groupId>com.alibaba.spring.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.0.0</version>
<exclusions>
<exclusion>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo-registry-zookeeper</artifactId>
<version>2.6.1</version>
<exclusions>
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
<exclusion>
<groupId>org.apache.log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
<exclusion>
<artifactId>log4j</artifactId>
<groupId>log4j</groupId>
</exclusion>
<exclusion>
<artifactId>slf4j-log4j12</artifactId>
<groupId>org.slf4j</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-test</artifactId>
<version>2.1.7.RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.1.9.RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
在resources下面创建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: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="provider-service" />
<!-- 注册中心服务地址 -->
<dubbo:registry id="zookeeper" protocol="zookeeper" address="zookeeper://127.0.0.1:2181" />
<!-- 用dubbo协议在20881,可以自定义 -->
<dubbo:protocol name="dubbo" port="20881" />
<!-- 生成远程服务代理 -->
<dubbo:service interface="com.example.product.service.findUser" ref="findService"/><!-- 接口位置 -->
<bean id="findService" class="com.example.product.service.serviceImpl.findUserImpl"></bean><!-- 接口实现类位置 -->
</beans>
最后在启动类上面引入dubbo-provider.xml
@ImportResource({"classpath:dubbo-provider.xml"})
启动项目,在dubbo页面可以看到生产者已经注册
下面创建消费者,消费者/生产者的service层包结构必须一致。消费者service层到接口为止。生产者则从serviceImpl开始,但是上级包结构都是一样的。无论生产者还是消费者,xml中interface配置的路径也是相同的。
消费者接口与生产者接口名称目录位置都是一样的,但消费者不需要写实现类ServiceImpl
package com.example.product.service;
import java.util.List;
@Service
public interface findUser {
/**
* 查询信息
* @return
*/
List<String> list();
}
在resources下面创建dubbo-customer.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="customer" />
<!-- 注册中心服务地址 -->
<dubbo:registry id="zookeeper" protocol="zookeeper" address="zookeeper://127.0.0.1:2181" />
<dubbo:reference interface="com.example.product.service.findUser" id="findService"/>
</beans>
在启动类上面引入dubbo-customer.xml
@ImportResource({"classpath:dubbo-customer.xml"})
编写测试类text
import com.example.product.service.findUser;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.ArrayList;
import java.util.List;
@RunWith(SpringRunner.class)
@SpringBootTest
public class text {
@Autowired
findUser finduserService;
@Test
public void getUser(){
List<String> list = new ArrayList<String>();
list=finduserService.list();
System.out.println(list.get(0));
}
}
运行结果,hello,dubbo-provider