SpringBoot整合Dubbo和Zookeeper升级版

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/J080624/article/details/83900551

分布式架构与Dubbo基础入门与实践一文中初步介绍了分布式架构并使用xml配置方式进行了Dubbo和Zookeeper实践。分布式应用简单入门及SpringBoot整合Dubbo+Zookeeper一文中使用SpringBoot整合了Dubbo和Zookeeper但是并未抽取公共API项目。本文是上述两个项目的升级版。

【1】创建SpringBoot项目

① 使用Spring starter pproject 分别创建provider和consumer

在这里插入图片描述


② 分别将普通项目中的实现拷贝到boot项目中

如这里将order-service-consumer项目中的实现类及包拷贝到boot-order-service-consumer项目中:

在这里插入图片描述


③ 分别为两个boot项目添加gmall-interface项目依赖

当然是要依赖公共API项目了:

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

④ 为消费者项目编写controller

如下所示,拦截页面请求:

@Controller
public class OrderController {
	
	@Autowired
	OrderService orderService;
	
	@ResponseBody
	@RequestMapping("/initOrder")
	public List<UserAddress> initOrder(@RequestParam("uid")String userId) {
		return orderService.initOrder(userId);
	}

}

现在项目创建好了,但是远程调用肯定是失败的,需要进行服务配置。


【2】配置服务提供者

① 导入dubbo-starter

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

注意starter版本适配:
在这里插入图片描述


② 配置application.properties

//application.name就是服务名,不能跟别的dubbo提供端重复
//registry.protocol 是指定注册中心协议
//registry.address 是注册中心的地址加端口号
dubbo.application.name=user-service-provider
dubbo.registry.address=127.0.0.1:2181
dubbo.registry.protocol=zookeeper

//protocol.name 是分布式固定是dubbo,不要改。
//protocol.port是其他服务与本服务通信的端口
dubbo.protocol.name=dubbo
dubbo.protocol.port=20881

dubbo.monitor.protocol=registry



此时还没有暴露服务,这个别着急,使用Dubbo的@Service注解。


③ Dubbo的@Service注解暴露服务

如下所示:

import org.springframework.stereotype.Component;

import com.alibaba.dubbo.config.annotation.Service;
import com.web.gmall.bean.UserAddress;
import com.web.gmall.service.UserService;

@Service
@Component
public class UserServiceImpl implements UserService {
	//...
}

④ 在主程序启动类中使用@EnableDubbo注解以开启基于注解的dubbo功能

如下所示:

@EnableDubbo//开启基于注解的dubbo功能
@SpringBootApplication
public class BootUserServiceProviderApplication {

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

⑤ 启动主程序,查看Dubbo Admin

多了一个服务器提供者:

在这里插入图片描述


【3】配置服务消费者

① 导入dubbo-starter

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

② 配置application.properties

server.port=8081

dubbo.application.name=boot-order-service-consumer
dubbo.registry.address=zookeeper://127.0.0.1:2181
dubbo.monitor.protocol=registry

这里还没有声明要调用哪个远程服务,同样使用注解实现。


③ 使用@Reference注解引用远程的服务

import com.alibaba.dubbo.config.annotation.Reference;

@Service
public class OrderServiceImpl implements OrderService {
	@Reference
	UserService userService;
	//...
}

④ 在主程序启动类中使用@EnableDubbo注解以开启基于注解的dubbo功能

@EnableDubbo
@SpringBootApplication
public class BootOrderServiceConsumerApplication {

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

⑤ 启动主程序,查看Dubbo Admin

多了一个服务消费者:

在这里插入图片描述


⑥ 由于消费者是一个web工程,使用浏览器发送请求进行测试

在这里插入图片描述

多刷新浏览器(多次请求),在Monitor管理界面中可以查看请求与响应信息监控:
在这里插入图片描述
在这里插入图片描述

至此,五个项目如下所示(项目下载地址):

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/J080624/article/details/83900551