Dubbo(使用注册中心)

Dubbo使用注册中心

直接改造上一篇文章创建的工程

1、复制之前的的项目,修改项目名和pom文件(这里只演示一个)

在这里插入图片描述

修改artifactId与工程名一致

在这里插入图片描述

2、删除选中的多余的文件

在这里插入图片描述

3、导入model(其余两个操作相同)

在这里插入图片描述

4、导入maven工程

一直next就行
在这里插入图片描述

5、修改服务提供者和消费者的pom文件

<dependency>
	<groupId>com.why</groupId>
	<artifactId>ch06-interface</artifactId>
	<version>1.0.0</version>
</dependency>

6、给服务提供者添加zookeeper依赖

<dependency>
	<groupId>org.apache.curator</groupId>
	<artifactId>curator-framework</artifactId>
	<version>4.1.0</version>
</dependency>

7、修改服务提供者的dubbo核心配置文件

<?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"
       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">

    <!--声明dubbo服务提供者名称:保证唯一-->
    <!--选择dubbo.apache.org的那个-->
    <dubbo:application name="ch06-StudentService-provide"/>
    <!--声明dubbo使用的协议和端口,dubbo官方推荐使用的协议为dubbo,端口号默认为20880-->
    <dubbo:protocol name="dubbo" port="20880"/>
    <!--指定注册中心的地址和端口号-->
    <dubbo:registry address="zookeeper://localhost:2181"/>
    <!--暴露服务(使用注册中心)-->
    <!--这里就不需要使用registry属性-->
    <dubbo:service interface="com.why.service.StudentService" ref="StudentService" />
    
    <bean id="StudentService" class="com.why.service.Imp.StudentServiceImp"/>
</beans>

8、给服务消费者添加zookeeper依赖

<dependency>
	<groupId>org.apache.curator</groupId>
	<artifactId>curator-framework</artifactId>
	<version>4.1.0</version>
</dependency>

9、修改服务消费者dubbo核心配置文件

<?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"
       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">

    <!--声明dubbo服务提供者名称:保证唯一-->
    <!--选择dubbo.apache.org的那个-->
    <dubbo:application name="ch06-StudentService-consumer"/>
    <!--指定注册中心-->
    <dubbo:registry address="zookeeper://localhost:2181"/>
    <!--引用远程服务接口-->
    <!--这里也不需要registry属性了-->
    <dubbo:reference id="userService" interface="com.why.service.StudentService" />
</beans>

10、打开注册中心

在这里插入图片描述

测试:

在这里插入图片描述

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
THE END
分享
二维码
< <上一篇
下一篇>>