Dubbo工作原理:
1.下载zookeeper
2.安装zookeeper
1)windows下的安装
解压下载的压缩包
然后在conf文件夹下复制粘贴zoo_sample.cfg,把复制的文件名称修改为zoo.cfg
进入bin文件夹,双击zkServer.cmd
2)linux下的安装
https://www.cnblogs.com/expiator/p/9853378.html
3.创建provider springboot项目
1)provider 服务提供者
引入依赖
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-dependencies-zookeeper</artifactId>
<version>3.0.0</version>
<type>pom</type>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
</exclusions>
</dependency>
exclusion 排除slf4j包
写入配置文件
dubbo.application.name=provider // 应用名
dubbo.scan.base-packages=com.xukf.service // 扫描提供的接口
dubbo.registry.address=zookeeper://127.0.0.1:2181 // zookeeper注册中心地址
伪代码:
public interface TikctService {
public String hollew();
}
package com.xukf.service.impl;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Component;
import com.xukf.service.TikctService;
@DubboService
@Component
public class TiketServiceImpl implements TikctService {
@Override
public String hollew() {
return "HOLLE,WORD!";
}
}
4.查看dubbo-admin
1)下载:https://github.com/apache/incubator-dubbo/tree/2.5.x
2) 解压
3)打包:mvn clean package -Dmaven.test.skip=true // 跳过单元测试打包
4)把打出的war包放到tomcat中运行
5)查看结果
5.创建concumer springboot项目
1)provider 服务提供者
引入依赖
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-dependencies-zookeeper</artifactId>
<version>3.0.0</version>
<type>pom</type>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
</exclusions>
</dependency>
exclusion 排除slf4j包
写入配置文件
dubbo.application.name=consumer //应用名
dubbo.registry.address=zookeeper://127.0.0.1:2181 // 注册中心
伪代码:
package com.xukf.service;
import org.springframework.stereotype.Service;
@Service
public interface TikctService {
public String hollew();
}
package com.xukf.controller;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.xukf.service.TikctService;
@Controller
public class IndexController {
@DubboReference
TikctService tikctService;
@RequestMapping("/index")
@ResponseBody
public String index() {
return tikctService.hollew();
}
}
6.查看运行结果
备注:
provider项目中的端口8001
consumer项目中的端口8002
dubbo-admin中的zookeeper连接可以在dubbo-2.5.x\dubbo-admin\src\main\webapp\WEB-INF文件夹下的dubbo.properties中修改