dubbo整合springboot

SpringBoot与dubbo整合的三种方式

1、导入dubbo-starter,在application.properties配置属性,使用@Service【暴露服务】使用@Reference【引用服务】

2、保留dubbo xml配置文件:
导入dubbo-starter,使用@ImportResource导入dubbo的配置文件即可

3、使用注解API的方式:
将每一个组件手动创建到容器中,让dubbo来扫描其他的组件

一、导入相关依赖

在项目中(提供者、消费者)导入通用接口:

<dependency>
  <groupId>com.jess.gmall</groupId>
  <artifactId>gmall-interface</artifactId>
  <version>0.0.1-SNAPSHOT</version>
</dependency>

dubbo依赖

<dependency>
	<groupId>com.alibaba.boot</groupId>
	<artifactId>dubbo-spring-boot-starter</artifactId>
	<version>0.2.0</version>
</dependency>

注意start的版本:
在这里插入图片描述

二、配置

在application.properties文件中配置:

提供者配置:
dubbo.application.name=gmall-provider
dubbo.registry.protocol=zookeeper
dubbo.registry.address=127.0.0.1:2181
dubbo.scan.base-package=com.jess.gmall
dubbo.protocol.name=dubbo
dubbo.protocol.port=20880
dubbo.monitor.protocol=registry #从注册中心自动去找

消费者配置:
server.port=8081 #默认tomcat端口号8080,但是监控中心也在8080端口,所以将tomcat端口号改了

dubbo.application.name=gmall-consumer
dubbo.registry.protocol=zookeeper
dubbo.registry.address=127.0.0.1:2181
dubbo.scan.base-package=com.jess.gmall
dubbo.protocol.name=dubbo
dubbo.monitor.protocol=registry

application.name:服务名,同样的服务名字相同,不能跟别的服务重名
registry.protocol :指定注册中心协议
registry.address :注册中心的地址加端口号
protocol.name :分布式固定是dubbo,不要改。
base-package 注解方式要扫描的包

dubbo注解: @Service(暴露服务)、@Reference(远程引用)
【如果没有在配置中写dubbo.scan.base-package,还需要使用@EnableDubbo注解】

三、代码实现

provider:

/*
* 在要暴露的服务上加上注解service,这个service是dubbo包下的与springboot的service不同
*/

@com.alibaba.dubbo.config.annotation.Service
@Service
public class UserServiceImpl implements UserService {

	@HystrixCommand
	@Override
	public List<UserAddress> getUserAddressList(String userId) {
	
		System.out.println("UserServiceImpl..3.....");
		UserAddress address1 = new UserAddress(1, "四川省成都市", "1", "Jess", "12354685122", "Y");
		UserAddress address2 = new UserAddress(2, "四川省南充市", "1", "Eunice", "115234812396", "N");

		if(Math.random()>0.5) {
			throw new RuntimeException();
		}
		return Arrays.asList(address1,address2);
	}
}
/*
* @EnableDubbo:开启基于注解的dubbo功能
* @EnableHystrix:开启服务容错
*/

@EnableDubbo(scanBasePackages="com.jess.gmall")
@EnableHystrix 
@SpringBootApplication
public class BootUserServiceProviderApplication {

	public static void main(String[] args) {
		SpringApplication.run(BootUserServiceProviderApplication.class, args);
	}
}

consumer:

@Controller
public class OrderController {
	
	@Autowired
	OrderService orderService;
	
	//初始化订单
	@ResponseBody
	@RequestMapping("/initOrder")
	public List<UserAddress> initOrder(@RequestParam("uid")String userId) {
		return orderService.initOrder(userId);
	}
}
/*
* @Reference:从注册中心远程调用UserService 
*/

@Service
public class OrderServiceImpl implements OrderService {

	//@Autowired
	@Reference
	UserService userService;
	
	@HystrixCommand(fallbackMethod="hello")
	@Override
	public List<UserAddress> initOrder(String userId) {
		
		System.out.println("用户id:"+userId);
		//1、查询用户的收货地址
		List<UserAddress> addressList = userService.getUserAddressList(userId);
		return addressList;
	}
		
	public List<UserAddress> hello(String userId) {
	
		return Arrays.asList(new UserAddress(10, "测试地址", "1", "测试", "测试", "Y"));
	}
}
@EnableDubbo
@EnableHystrix
@SpringBootApplication
public class BootOrderServiceConsumerApplication {

	public static void main(String[] args) {
		SpringApplication.run(BootOrderServiceConsumerApplication.class, args);
	}
}

访问 localhost:8081/initOrder?uid=1 即可查出用户地址信息

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值