简单Dubbo项目的实践

92 阅读2分钟

持续创作,加速成长!这是我参与「掘金日新计划 · 6 月更文挑战」的第14天,点击查看活动详情

dubbo学习

项目架构

image.png

项目采用dubbo最简单的架构,分为三个部分,分别是消费者(consumer),服务提供者(provider),和api.

1.其中api中提供了服务接口HelloService。

image.png

2.在服务提供者中实现了服务HelloServiceImpl

image.png

并在zookeeper注册中心注册服务:

3.consumer消费者端,通过向zookeeper订阅服务:

然后实现调用服务提供者(provider)提供的服务:

运行结果如下:

image.png

项目运行bug

image.png

这种问题是由于spring-boot与java的spring framework的的版本号没对应导致,具体版本号对应关系参考:www.cnblogs.com/badtree/art…

在修改.pom修改文件中相关依赖后,发现还是存在上面版本号的问题,发现在

dubbo-provider.iml文件中,下图第二项仍为5.2.2(下图已修改为5.0.4)版本导致,修改后无问题。

image.png

问题

  • 生成的.iml文件有什么用?

.iml文件是idea自动创建的模块文件,里面存储着module的配置信息,用来识别module。

  • @Resource与@Resources的作用于原理是什么?、

@Resource的官方解释:@Resource annotation, which is semantically defined to identify a specific target component by its unique name, with the declared type being irrelevant for the matching process.

就是说他是一个被定义为来用唯一名称来识别特定目标组件的予以,其种声明类型与匹配过程无关。

If no name is specified explicitly, the default name is derived from the field name or setter method. In case of a field, it takes the field name; in case of a setter method, it takes the bean property name.

翻译:如果没有明确指定名称,则默认名称是从字段名称或设置方法(get、set方法)派生的。 如果用在字段上,则采用字段名称; 如果用在在setter方法,它采用其属性名称(例如getProperty()方法,取property做为属性名称)。

Thus in the following example, the customerPreferenceDao field first looks for a bean named customerPreferenceDao, then falls back to a primary type match for the type CustomerPreferenceDao. The “context” field is injected based on the known resolvable dependency type ApplicationContext.

解释:customerPreferenceDao字段首先查找名字为customerPreferenceDao的bean,如果没找到该类,则以CustomerPreferenceDao类型进行匹配。

public class MovieRecommender {

@Resource

private CustomerPreferenceDao customerPreferenceDao;

@Resource

private ApplicationContext context;

public MovieRecommender() {

}

...

}

与@Autowired的主要区别在于@Resource默认ByName的方式进行自动注入,而@Autowired首先ByType进行寻找,然后在ByName方式匹配。