1.文档参照
2.三个工程
2.1 公共接口工程
参考这篇文章:SpringBoot整合Dubbo的第一种方式
2.2 服务提供者
首先我们注释掉配置文件中的相关内容,只留下应用名(不留也可以)。
spring.application.name=boot-user-service-provider #dubbo.application.name=boot-user-service-provider # #dubbo.scan.base-packages=com.szh.service.impl # #dubbo.registry.address=127.0.0.1:2181 #dubbo.registry.protocol=zookeeper # #dubbo.protocol.name=dubbo #dubbo.protocol.port=20880 # #dubbo.monitor.protocol=registry
下面我们编写核心的xml配置文件(不仅让我回想起以前学spring的时候啊,哈哈哈)
<?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" xmlns:dubb="http://code.alibabatech.com/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 http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <!-- 1、指定当前服务/应用的名字(同样的服务名字相同,不要和别的服务同名) --> <dubbo:application name="boot-user-service-provider"></dubbo:application> <!-- 2、指定注册中心的位置 --> <!-- <dubb:registry address="zookeeper://127.0.0.1:2181"></dubb:registry>--> <dubbo:registry protocol="zookeeper" address="127.0.0.1:2181"></dubbo:registry> <!-- 3、指定通信规则(通信协议?通信端口) --> <dubbo:protocol name="dubbo" port="20880"></dubbo:protocol> <!-- 4、暴露服务,ref:指向服务的真正的实现对象 --> <dubbo:service interface="com.szh.gmall.service.UserService" ref="userServiceImpl" version="1.0.0"></dubbo:service> <!-- 服务的具体实现 --> <bean id="userServiceImpl" class="com.szh.service.impl.UserServiceImpl"></bean> <bean id="userServiceImpl2" class="com.szh.service.impl.UserServiceImpl2"></bean> </beans>
由于这里的dubbo配置都在上面的xml中实现了,所以下面的实现类中 @DubboService 注解就可以注释掉了。
package com.szh.service.impl; import com.szh.gmall.bean.UserAddress; import com.szh.gmall.service.UserService; import org.apache.dubbo.config.annotation.DubboService; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; import org.springframework.stereotype.Service; import java.util.Arrays; import java.util.List; import java.util.concurrent.TimeUnit; /** * 1.将服务提供者注册到注册中心 * 1) 引入dubbo依赖、zookeeper客户端依赖 * 2) 配置服务提供者 * 2.让服务消费者从注册中心订阅服务提供者的相关服务 */ @Service //@DubboService(interfaceClass = UserService.class, version = "1.0.0") public class UserServiceImpl implements UserService { //The default value of ${dubbo.application.name} is ${spring.application.name} @Value("${spring.application.name}") private String applicationName; @Override public List<UserAddress> getUserAddressList(String userId) { UserAddress userAddress1 = new UserAddress(1, "浙江省杭州市", "1", "张三", "123456", "Y"); UserAddress userAddress2 = new UserAddress(2, "湖北省武汉市", "1", "李四", "999999", "N"); try { TimeUnit.MILLISECONDS.sleep(2000); //测试timeout // TimeUnit.MILLISECONDS.sleep(4000); //测试重试次数 } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(applicationName + " old...."); return Arrays.asList(userAddress1, userAddress2); } }
package com.szh.service.impl; import com.szh.gmall.bean.UserAddress; import com.szh.gmall.service.UserService; import org.apache.dubbo.config.annotation.DubboService; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import java.util.Arrays; import java.util.List; import java.util.concurrent.TimeUnit; /** * 1.将服务提供者注册到注册中心 * 1) 引入dubbo依赖、zookeeper客户端依赖 * 2) 配置服务提供者 * 2.让服务消费者从注册中心订阅服务提供者的相关服务 */ @Service //@DubboService(interfaceClass = UserService.class, version = "2.0.0") public class UserServiceImpl2 implements UserService { //The default value of ${dubbo.application.name} is ${spring.application.name} @Value("${spring.application.name}") private String applicationName; @Override public List<UserAddress> getUserAddressList(String userId) { UserAddress userAddress1 = new UserAddress(1, "浙江省杭州市", "1", "张三", "123456", "Y"); UserAddress userAddress2 = new UserAddress(2, "湖北省武汉市", "1", "李四", "999999", "N"); try { TimeUnit.MILLISECONDS.sleep(2000); //测试timeout // TimeUnit.MILLISECONDS.sleep(4000); //测试重试次数 } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(applicationName + " new...."); return Arrays.asList(userAddress1, userAddress2); } }
主启动类上添加 @ImportResource注解,要加载类路径下的某个xml配置文件。
package com.szh; import org.apache.dubbo.config.spring.context.annotation.EnableDubbo; import org.apache.dubbo.config.spring.context.annotation.EnableDubboConfig; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.ImportResource; @SpringBootApplication //@EnableDubbo(scanBasePackages = "com.szh") @ImportResource(locations = "classpath:provider.xml") public class BootUserServiceProviderApplication { public static void main(String[] args) { SpringApplication.run(BootUserServiceProviderApplication.class, args); } }
2.3 服务消费者
首先我们注释掉配置文件中的相关内容,只留下应用名(不留也可以)、端口号最好留下,因为服务提供者启动之后会占用8080,这里将服务消费者声明在8081端口启动。
server.port=8081 spring.application.name=boot-order-service-consumer #dubbo.application.name=boot-order-service-consumer # #dubbo.registry.address=zookeeper://127.0.0.1:2181 #dubbo.monitor.protocol=registry
package com.szh.controller; import com.szh.gmall.bean.UserAddress; import com.szh.gmall.service.OrderService; import org.apache.dubbo.config.annotation.DubboReference; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; import java.util.List; /** * */ @RestController public class OrderController { @Autowired private OrderService orderService; @GetMapping(value = "/initOrder/{userId}") public List<UserAddress> initOrder(@PathVariable("userId") String userId) { return orderService.initOrder2(userId); } }
package com.szh.service.impl; import com.szh.gmall.bean.UserAddress; import com.szh.gmall.service.OrderService; import com.szh.gmall.service.UserService; import org.apache.dubbo.config.annotation.DubboReference; import org.apache.dubbo.config.annotation.Method; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; /** * */ @Service public class OrderServiceImpl implements OrderService { // @DubboReference(interfaceClass = UserService.class, //服务接口名 // version = "1.0.0", //服务版本,与服务提供者的版本一致 // check = false, //启动时检查提供者是否存在,true报错,false忽略 // timeout = 3000, //服务方法调用超时时间(毫秒) // methods = @Method(name = "getUserAddressList"), //精确到服务接口的某个方法 // retries = 3) //远程服务调用重试次数,不包括第一次调用,不需要重试请设为0 @Autowired private UserService userService; @Override public void initOrder(String userId) { } @Override public List<UserAddress> initOrder2(String userId) { System.out.println("用户id:" + userId); List<UserAddress> addressList = userService.getUserAddressList(userId); return addressList; } }
下面是核心的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" xmlns:dubb="http://code.alibabatech.com/schema/dubbo" xmlns:context="http://www.springframework.org/schema/context" 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 http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:component-scan base-package="com.szh"></context:component-scan> <dubbo:application name="boot-order-service-consumer"></dubbo:application> <dubbo:registry address="zookeeper://127.0.0.1:2181"></dubbo:registry> <!-- 声明需要调用的远程服务的接口,生成远程服务代理 --> <dubbo:reference interface="com.szh.gmall.service.UserService" id="userServiceImpl" version="1.0.0" check="false" timeout = "3000"> <dubbo:method name="getUserAddressList"></dubbo:method> </dubbo:reference> </beans>
package com.szh; import org.apache.dubbo.config.spring.context.annotation.EnableDubbo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.ImportResource; @SpringBootApplication //@EnableDubbo(scanBasePackages = "com.szh") @ImportResource(locations = "classpath:consumer.xml") public class BootOrderServiceConsumerApplication { public static void main(String[] args) { SpringApplication.run(BootOrderServiceConsumerApplication.class, args); } }
3.启动测试
启动服务提供者和消费者之前,要先将zookeeper开启,然后再将dubbo管控台打开。
我这里为了方便,就直接在windows下启动zookeeper了,dubbo管控台开不开无所谓。
参考 https://blog.csdn.net/weixin_43823808/article/details/124494499