springboot集成dubbo的方法
这篇"springboot集成dubbo的方法"文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇"springboot集成dubbo的方法"文章吧。
第一步 搭建zookeeper环境
在centos窗口中,执行如下命令,拉取镜像,并启动zookeeper容器
docker pull zookeeperdocker run -d -v /home/docker/zookeeperhost/zookeeperDataDir:/data -v /home/docker/zookeeperhost/zookeeperDataLogDir:/datalog -e ZOO_MY_ID=1 -e ZOO_SERVERS='server.1=125.77.116.145:2888:3888' -p 2182:2181 -p 2888:2888 -p 3888:3888 --name zookeeper --privileged zookeeper
注:
1.zookeeper默认连接端口是2181 但本文测试用例时由于被其他程序占走,故使用2182。
2.读者请自行创建映射目录zookeeperDataDir | zookeeperDataLogDir
第二步 springboot集成dubbo
1.项目目录机构
说明:
1.api目录:存放消费者与提供者调用的service接口
2.consumer目录:消费者目录 调用提供者远程提供的接口实现
3.provider目录:提供者目录 提供给消费者接口实现
读者请自行创建项目目录(创建空项目,然后在空项目中新建三个module)
项目案例说明:业务假设场景=》 产品购买消费金额(consumer)同时并返回所有消费的总金额(需要调用到provider项目中服务实现)。
2.代码编写
2.1 api目录
接口编写
2.1.1.在com.dubbo.api.service(读者请自行创建,下同,package创建将不一一赘述)包下创建CostService.java
package com.dubbo.api.service;public interface CostService { /** * 成本增加接口 * @param cost * @return */ Integer add(int cost);}
2.1.2.pom.xml
4.0.0 com.dubbo api 0.0.1-SNAPSHOT jar api Demo project for Spring Boot org.springframework.boot spring-boot-starter-parent 2.1.0.RELEASE UTF-8 UTF-8 1.8 org.springframework.boot spring-boot-starter spring-boot-starter-test test org.springframework.boot spring-boot-maven-plugin
2.2 consumer目录 web访问、接口调用以及dubbo配置编写
2.2.1.引入 dubbo-spring-boot-starter 以及 上述的api模块
4.0.0 com.dubbo api 0.0.1-SNAPSHOT jar api Demo project for Spring Boot org.springframework.boot spring-boot-starter-parent 2.1.0.RELEASE UTF-8 UTF-8 1.8 org.springframework.boot spring-boot-starter org.springframework.boot spring-boot-starter-test test org.springframework.boot spring-boot-maven-plugin
2.2.2.在resources目录下 创建application.yml,并编写dubbo配置
4.0.0 com.dubbo consumer 0.0.1-SNAPSHOT jar consumer Demo project for Spring Boot org.springframework.boot spring-boot-starter-parent 2.1.0.RELEASE UTF-8 UTF-8 1.8 org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test test com.dubbo api 0.0.1-SNAPSHOT compile com.alibaba.boot dubbo-spring-boot-starter 0.2.0 org.springframework.boot spring-boot-maven-plugin
2.2.3.使用 @EnableDubbo 注解开启dubbo
ConsumerApplication.java 启动类
package com.dubbo.consumer;import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication@EnableDubbopublic class ConsumerApplication { public static void main(String[] args) { SpringApplication.run(ConsumerApplication.class, args); }}
2.2.4.编写产品service接口 ProductService.java
package com.dubbo.consumer.service;public interface ProductService { /** * 获得总消费 * @param a * @return */ Integer getCost(int a);}
2.2.5.编写产品接口的实现,并调用远程服务CostService 。 ProductServiceImpl.java
package com.dubbo.consumer.service.impl;import com.alibaba.dubbo.config.annotation.Reference;import com.dubbo.api.service.CostService;import com.dubbo.consumer.service.ProductService;import org.springframework.stereotype.Service;/** * 产品service */@Servicepublic class ProductServiceImpl implements ProductService { /** * 使用dubbo的注解 com.alibaba.dubbo.config.annotation.Reference。进行远程调用service */ @Reference private CostService costService; @Override public Integer getCost(int a) { return costService.add(a); }}
2.2.6.编写访问类,ProductController.java
package com.dubbo.consumer.controller;import com.dubbo.consumer.service.ProductService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;/** * 产品controller */@RestControllerpublic class ProductController { @Autowired private ProductService productService; /** * 添加完 返回总共消费 * @param a * @return */ @RequestMapping("/add") public String getCost(int a){ return "该产品总共消费 :"+productService.getCost(a); }}
2.3 provider目录 api接口实现以及dubbo配置
2.3.1.引入 dubbo-spring-boot-starter 以及 上述的api模块
4.0.0 com.dubbo provider 0.0.1-SNAPSHOT jar provider Demo project for Spring Boot org.springframework.boot spring-boot-starter-parent 2.1.0.RELEASE UTF-8 UTF-8 1.8 org.springframework.boot spring-boot-starter-web spring-boot-starter-test test com.dubbo api 0.0.1-SNAPSHOT compile com.alibaba.boot dubbo-spring-boot-starter 0.2.0 org.springframework.boot spring-boot-maven-plugin
2.3.2.在resources目录下 创建application.yml,并编写dubbo配置
dubbo: application: name: dubbo-provider registry: address: 125.77.116.145:2182 # 读者请自行更改zookeeper地址 protocol: zookeeper check: false protocol: name: dubbo port: 30003 monitor: protocol: register consumer: check: false timeout: 3000server: port: 8061
2.3.3.使用 @EnableDubbo 注解开启dubbo
ConsumerApplication.java 启动类
package com.dubbo.provider;import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication@EnableDubbopublic class ProviderApplication { public static void main(String[] args) { SpringApplication.run(ProviderApplication.class, args); }}
2.3.3.编写CostService服务实现 CostServiceImpl.java
package com.dubbo.provider.service.impl;import com.alibaba.dubbo.config.annotation.Service;import com.dubbo.api.service.CostService;/** * 花费服务 */@Servicepublic class CostServiceImpl implements CostService { /** * 假设之前总花费了100 */ private final Integer totalCost = 1000; /** * 之前总和 加上 最近一笔 * @param cost * @return */ @Override public Integer add(int cost) { return totalCost + cost; }}
第三步 测试dubbo远程服务调用
编写第二步代码完成后 ,启动consumer项目,以及provider项目
在浏览器中访问 http://localhost:8062/add?a=100
出现如上结果即为调用成功
第四步 dubbo管理平台
1.本文将运维项目代码
2.这里需要修改一个配置D:learnplaceincubator-dubbo-opsdubbo-adminsrcmain esourcesapplication.properties
## Licensed to the Apache Software Foundation (ASF) under one or more# contributor license agreements. See the NOTICE file distributed with# this work for additional information regarding copyright ownership.# The ASF licenses this file to You under the Apache License, Version 2.0# (the "License"); you may not use this file except in compliance with# the License. You may obtain a copy of the License at## http://www.apache.org/licenses/LICENSE-2.0## Unless required by applicable law or agreed to in writing, software# distributed under the License is distributed on an "AS IS" BASIS,# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.# See the License for the specific language governing permissions and# limitations under the License.#server.port=8001#服务器端口 server.portspring.velocity.cache=falsespring.velocity.charset=UTF-8spring.velocity.layout-url=/templates/default.vmspring.messages.fallback-to-system-locale=falsespring.messages.basename=i18n/messagespring.root.password=rootspring.guest.password=guest#访问的密码配置 spring.root.password spring.guest.password#dubbo.registry.address=zookeeper://127.0.0.1:2181dubbo.registry.address=zookeeper://125.77.116.145:2182#zookeeper地址
3.在D:learnplaceincubator-dubbo-opsdubbo-admin目录下 ,进入cmd窗口执行
mvn claen package 打包项目,
4.然后进入D:learnplaceincubator-dubbo-opsdubbo-admin arget ,进入cmd窗口执行
java -jar dubbo-admin-0.0.1-SNAPSHOT.jar 运行项目
5.启动成功后 浏览器访问http://localhost:8001 输入账号:root / 密码:root 即可。
以上就是关于"springboot集成dubbo的方法"这篇文章的内容,相信大家都有了一定的了解,希望小编分享的内容对大家有帮助,若想了解更多相关的知识内容,请关注行业资讯频道。