SSM整合Dubbo以及Dubbo的安装教程

Dubbo搭建(直连式)

1、创建服务的提供者(Provider)

① 在一个独立项目中创建接口,并实现

如:UserService

实现:UserServiceImpl

public class UserServiceImpl implements UserService {
    
    
    @Override
    public String getUsername() {
    
    
        return "hello world";
    }
}

② 导入依赖

Spring相关核心依赖

Dubbo核心依赖

<dependency>
    <groupId>org.apache.dubbo</groupId>
    <artifactId>dubbo</artifactId>
    <version>2.7.8</version>
</dependency>

③ 创建配置文件

创建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://code.alibabatech.com/schema/dubbo"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://code.alibabatech.com/schema/dubbo        http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

    <!-- 将本地实例ioc-->
    <bean id="userService" class="com.sofwin.sercice.impl.UserServiceImpl"/>

    <!--    用于配置当前用用信息 不管该应用是提供者还是消费者都需要配置

    -->
    <dubbo:application name="pro1"></dubbo:application>

    <!--    要将服务暴露到哪里 用于配置连接注册中心相关信息
        address:注册中心的地址  N/A:直连
      -->
    <dubbo:registry address="N/A"></dubbo:registry>

    <!--    将服务暴露出去 声明接口类型
    interface:实现的接口
    ref:该接口的实现类
    -->
    <dubbo:service interface="com.sofwin.sercice.UserService" ref="userService"/>


</beans>

注意以下几步:

需要dubbo的约束。

配置当前应用信息。

配置连接注册中心的相关信息。

将服务暴露出去。

④ 测试是否将服务暴露出去

创建服务启动类

public class APP {
    
    
    public static void main(String[] args) throws Exception{
    
    
        //加载配置文件
        ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
        //容器启动
        context.start();
        System.out.println("运行");

        //让线程处于读取的状态
        System.in.read();
    }
}

2、创建服务的消费者(Consumer)

① 创建与服务提供者接口和实现类相同的类和方法。

② 导入依赖

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
</dependency>
<dependency>
    <groupId>org.apache.dubbo</groupId>
    <artifactId>dubbo</artifactId>
</dependency>

③ 创建配置文件

<?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://code.alibabatech.com/schema/dubbo"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://code.alibabatech.com/schema/dubbo        http://code.alibabatech.com/schema/dubbo/dubbo.xsd">


    <!--    用于配置当前应用信息 不管该应用是提供者还是消费者都需要配置

    -->
    <dubbo:application name="con1"></dubbo:application>

    <!--    要将服务暴露到哪里 用于配置连接注册中心相关信息
        address:注册中心的地址  N/A:直连
      -->
    <dubbo:registry address="N/A"></dubbo:registry>

    <!--    调用远程服务
        interface:服务接口名,调用的接口
        id:远程调用生成的实体id
        url:点对点直连连接服务者地址,会绕过注册中心 协议名称://ip地址:端口号
    -->
    <dubbo:reference id="userService" interface="com.sofwin.service.UserService" url="dubbo://localhost:20880"></dubbo:reference>
</beans>

④ 创建模拟服务消费

public class App {
    
    
    public static void main(String[] args) {
    
    
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");

        UserService service=(UserService) context.getBean("userService");
        String username = service.getUsername();
        System.out.println(username);

    }
}

控制台输出:hello world 即搭建成功

Dubbo搭建(zookeeper)

使用zookeeper服务

1、安装zookeeper(Linux)

① 下载地址:https://zookeeper.apache.org/releases.html

② 将压缩包放入Linux系统中,将该文件移动到usr/local目录中

③ 通过命令tar xvf 文件名将压缩文件解压

2、配置zookeeper

① 依次进入解压后的zookeeper/conf目录

② 将该目录下的zoo_sample.cfg文件复制为zoo.cfg

参考命令:cp zoo_sample.cfg zoo.cfg

③ 在解压后的zookeeper根目录下创建文件夹data

参考命令:mkdir data

④ 将dataDir后的内容该为dataDir=/usr/local/解压后的zookeeper/data,保存

参考内容:dataDir=/usr/local/zookeeper/data

3、创建服务的提供者

在直连式的基础上只需要修改配置文件

<?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://code.alibabatech.com/schema/dubbo"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://code.alibabatech.com/schema/dubbo        http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

    <!-- 将本地实例ioc-->
    <bean id="userService" class="com.sofwin.service.impl.UserServiceImpl"/>

    <!--    用于配置当前应用信息 不管该应用是提供者还是消费者都需要配置

    -->
    <dubbo:application name="pro1"></dubbo:application>

    <!--    要将服务暴露到哪里 用于配置连接注册中心相关信息
       protocol:注册中心地址协议,这里使用zookeeper协议
      -->
    <dubbo:registry protocol="zookeeper" address="192.168.0.195:2181"></dubbo:registry>


	<!--    默认启动为1秒 timeout设置超时时间-->
    <dubbo:service interface="com.sofwin.service.UserService" ref="userService" timeout="10000"/>


</beans>

4、创建服务的消费者

在直连式的基础上只需要修改配置文件:

<?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://code.alibabatech.com/schema/dubbo"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://code.alibabatech.com/schema/dubbo        http://code.alibabatech.com/schema/dubbo/dubbo.xsd">


    <!--    用于配置当前应用信息 不管该应用是提供者还是消费者都需要配置

    -->
    <dubbo:application name="con1"></dubbo:application>

    <!--    要将服务暴露到哪里 用于配置连接注册中心相关信息
      -->
    <dubbo:registry address="192.168.0.195:2181" protocol="zookeeper"></dubbo:registry>

    <!--    调用远程服务
        interface:服务接口名,调用的接口
        id:远程调用生成的实体id
        url:点对点直连连接服务者地址,会绕过注册中心 协议名称://ip地址:端口号
    -->
    <dubbo:reference id="userService" interface="com.sofwin.service.UserService" check="false"></dubbo:reference>

</beans>

5、提供者服务启动和消费者调用服务基本与直连式相同。

猜你喜欢

转载自blog.csdn.net/Stand_Fast/article/details/112346183