diff --git a/spring/spring-dubbo/README-副本.md b/spring/spring-dubbo/README-副本.md new file mode 100644 index 0000000..c57041d --- /dev/null +++ b/spring/spring-dubbo/README-副本.md @@ -0,0 +1,279 @@ +# spring 整合 dubbo(xml配置方式) + +## 一、 项目结构说明 + +1.1 按照dubbo 文档推荐的服务最佳实践,建议将服务接口、服务模型、服务异常等均放在 API 包中,所以项目采用maven多模块的构建方式,在spring-dubbo下构建三个子模块: + +1. dubbo-common 是公共模块,用于存放公共的接口和bean,被dubbo-provider和dubbo-provider在pom.xml中引用; +2. dubbo-provider 是服务的提供者,提供商品的查询服务; +3. dubbo-provider 是服务的消费者,调用provider提供的查询服务。 + +1.2 本项目dubbo的搭建采用zookeeper作为注册中心, 关于zookeeper的安装和基本操作可以参见我的手记[Zookeeper 基础命令与Java客户端](https://github.com/heibaiying/LearningNotes/blob/master/notes/%E4%B8%AD%E9%97%B4%E4%BB%B6/ZooKeeper/ZooKeeper%E9%9B%86%E7%BE%A4%E6%90%AD%E5%BB%BA%E4%B8%8EJava%E5%AE%A2%E6%88%B7%E7%AB%AF.md) + +
+ + + +## 二、项目依赖 + +**在父工程的项目中同一导入依赖dubbo依赖的的jar包** + +这里需要注意的是ZooKeeper 3.5.x 和 ZooKeeper 3.4.x 是存在不兼容的情况 详见官网解释[ZooKeeper Version Compatibility](https://curator.apache.org/zk-compatibility.html), zookeeper 3.5 目前是beta版本,所以zookeeper 我选择的版本是 zookeeper-3.4.9 作为服务端。但默认情况下 curator-framework自动引用的最新的3.5的版本客户端,会出现 KeeperException$UnimplementedException 异常 + +```xml + +com.alibaba +dubbo +2.6.2 + + + org.apache.curator + curator-framework + 4.0.0 + + + org.apache.zookeeper + zookeeper + + + + + org.apache.zookeeper + zookeeper + 3.4.13 + +``` + + + +## 三、公共模块(dubbo-common) + +- api 下为公共的调用接口; +- bean 下为公共的实体类。 + +
+ +## 四、 服务提供者(dubbo-provider) + +
+ +#### 4.1 productService是服务的提供者( 商品数据用模拟数据展示) + +注:这里实现的接口IProductService来源于公共模块 + +```java +/** + * @author : heibaiying + * @description : 产品提供接口实现类 + */ +@Service +public class ProductService implements IProductService { + + private static List productList = new ArrayList<>(); + + static { + for (int i = 0; i < 20; i++) { + productList.add(new Product(i, "产品" + i, i / 2 == 0, new Date(), 66.66f * i)); + } + } + + public Product queryProductById(int id) { + for (Product product : productList) { + if (product.getId() == id) { + return product; + } + } + return null; + } + + + public List queryAllProducts() { + return productList; + } +} +``` + +#### 4.2 在dubbo.xml暴露服务 + +```xml + + + + + + + + + + + + + + + + + + +``` + +## 五、服务消费者(dubbo-consumer) + +
+ +#### 1.在dubbo.xml调用远程的服务 + +```xml + + + + + + + + + + + + + + + + + + + + +``` + +#### 2.消费服务 + +```java +@Controller +@RequestMapping("sell") +public class SellController { + + @Autowired + private IProductService productService; + + @RequestMapping + public String productList(Model model) { + List products = productService.queryAllProducts(); + model.addAttribute("products", products); + return "products"; + } + + @RequestMapping("product/{id}") + public String productDetail(@PathVariable int id, Model model) { + Product product = productService.queryProductById(id); + model.addAttribute("product", product); + return "product"; + } +} +``` + +## 六、项目构建的说明 + +因为在项目中,consumer和provider模块均依赖公共模块,所以在构建consumer和provider项目前需要将common 模块安装到本地仓库,**依次**对**父工程**和**common模块**执行: + +```shell +mvn install -Dmaven.test.skip = true +``` + +consumer中 pom.xml如下 + +```xml + + + + spring-dubbo + com.heibaiying + 1.0-SNAPSHOT + + 4.0.0 + + dubbo-consumer + + + + com.heibaiying + dubbo-common + 1.0-SNAPSHOT + compile + + + + +``` + +provider中 pom.xml如下 + +```xml + + + + spring-dubbo + com.heibaiying + 1.0-SNAPSHOT + + 4.0.0 + + dubbo-provider + + + + com.heibaiying + dubbo-common + 1.0-SNAPSHOT + compile + + + + +``` + +## 七、关于dubbo新版本管理控制台的安装说明 + +安装: + +```sh +git clone https://github.com/apache/incubator-dubbo-ops.git /var/tmp/dubbo-ops +cd /var/tmp/dubbo-ops +mvn clean package +``` + +配置: + +```sh +配置文件为: +dubbo-admin-backend/src/main/resources/application.properties +主要的配置有 默认的配置就是127.0.0.1:2181: +dubbo.registry.address=zookeeper://127.0.0.1:2181 +``` + +启动: + +```sh +mvn --projects dubbo-admin-backend spring-boot:run +``` + +访问: + +``` +http://127.0.0.1:8080 +``` \ No newline at end of file