dubbo 实现简易分布式服务

2023-12-07 14:00:40 浏览数 (1)

dubbo 实现简易分布式服务

服务器需要搭建zookeeper环境

zookeeper端口2181

还需要有java环境

1.需求

某个电商系统,订单服务需要调用用户服务获取某个用户的所有地址;

我们现在 需要创建两个服务模块进行测试

模块

功能

订单服务web模块

创建订单等

用户服务service模块

查询用户地址等

测试预期结果

订单服务web模块在A服务器,用户服务模块在B服务器,A可以远程调用B的功能。

2.工程架构

3.创建项目

3.1 公共接口层 ego-interface

简单maven项目即可

1.修改pom.xml文件
代码语言:javascript复制
<dependencies>
    <dependency>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId>
      <version>${lombok.vsersion}</version>
    </dependency>
  </dependencies>
2.创建实体类
代码语言:javascript复制
@Data
@AllArgsConstructor
@NoArgsConstructor
public class UserAddress implements Serializable {
    private static final long serialVersionUID = 1L;
    private Integer id;
    private String userId;
    private String userAddress;
}
3.创建公共接口
UserService
代码语言:javascript复制
public interface UserService {
    /**
     * 查询用户的所有地址
     * @return
     */
    public List<UserAddress> queryAllAddress(Integer id);
}
OrderService
代码语言:javascript复制
public interface OrderService {
    /**
     * 根据用户id 查找订单
     * @param uid 用户id
     * @return 订单
     */
    public List<UserAddress> initOrder(Integer uid);
}

3.2 用户服务模块 ego-user-service-provider (服务提供者)

1.修改pom.xml文件
代码语言:javascript复制
<dependencies>
    <!-- 这里是公共接口层哦-->
    <dependency>
      <groupId>com.hgzy</groupId>
      <artifactId>03-ego-interface</artifactId>
      <version>1.0-SNAPSHOT</version>
    </dependency>

  <dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>dubbo</artifactId>
    <version>2.6.7</version>
  </dependency>

  <dependency>
    <groupId>com.101tec</groupId>
    <artifactId>zkclient</artifactId>
    <version>0.11</version>
  </dependency>
  <!-- curator-framework -->
  <dependency>
    <groupId>org.apache.curator</groupId>
    <artifactId>curator-framework</artifactId>
    <version>4.1.0</version>
  </dependency>
  <dependency>
    <groupId>io.netty</groupId>
    <artifactId>netty-all</artifactId>
    <version>4.1.32.Final</version>
  </dependency>
</dependencies>
2.创建服务实体类
代码语言:javascript复制
@Service
public class UserServiceImpl implements UserService {
    private static List<UserAddress> addresses=new ArrayList<UserAddress>();
    static {
        addresses.add(new UserAddress(1,"1","湖南省株洲市荷塘区湖南化工职业技术学院"));
        addresses.add(new UserAddress(2,"2","湖南省永州市祁阳县"));
    }

    @Override
    public List<UserAddress> queryAllAddress(Integer id) {
        return addresses;
    }
}
3.applicationContext.xml
代码语言:javascript复制
<?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:context="http://www.springframework.org/schema/context"
       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://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
		http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd
		http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
    <!-- 声明应用程序的名称 -->
    <dubbo:application name="ego-user-service-provider"/>
    <!--指定注册中心的地址 -->
    <dubbo:registry
            address="zookeeper://服务器IP地址:2181"/>
    <!--使用dubbo协议,将服务暴露在20880端口 -->
    <dubbo:protocol name="dubbo" port="20880"/>

<!--     声明要暴露的实现类的对象-->
 <bean id="userService"
    class="com.hgzy.service.UserServiceImpl"/>
  
    <!-- 进行服务暴露 -->
    <dubbo:service interface="com.hgzy.service.UserService"
                   ref="userServiceImpl"/>
</beans>
4.测试服务
TestProvider
代码语言:javascript复制
public class TestProvider {
    public static void main(String[] args) throws IOException {
        ApplicationContext ac=
                new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
        System.out.println("服务提供者启动成功");
        System.in.read();
    }
}

3.3 订单服务(服务消费者) ego-order-service-consumer

1.修改pom.xml文件
代码语言:javascript复制
<dependencies>
    <!-- 这里是公共接口层哦-->
    <dependency>
      <groupId>com.hgzy</groupId>
      <artifactId>03-ego-interface</artifactId>
      <version>1.0-SNAPSHOT</version>
    </dependency>

  <dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>dubbo</artifactId>
    <version>2.6.7</version>
  </dependency>

  <dependency>
    <groupId>com.101tec</groupId>
    <artifactId>zkclient</artifactId>
    <version>0.11</version>
  </dependency>
  <!-- curator-framework -->
  <dependency>
    <groupId>org.apache.curator</groupId>
    <artifactId>curator-framework</artifactId>
    <version>4.1.0</version>
  </dependency>
  <dependency>
    <groupId>io.netty</groupId>
    <artifactId>netty-all</artifactId>
    <version>4.1.32.Final</version>
  </dependency>
</dependencies>
2.创建服务实体类
代码语言:javascript复制
@Service
public class OrderServiceImpl implements OrderService {

    @Reference
    private UserService userService;

    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    @Override
    public List<UserAddress> initOrder(Integer uid) {
        return userService.queryAllAddress(uid);
    }
}
3.applicationContext.xml
代码语言:javascript复制
<?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:p="http://www.springframework.org/schema/p"
       xmlns:c="http://www.springframework.org/schema/c" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
       xmlns:context="http://www.springframework.org/schema/context"
       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 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!--    声明服务名称-->
    <dubbo:application name="02-ego-order-service-consumer"/>
<!--    指定注册中心地址-->
    <dubbo:registry
            address="zookeeper://服务器ip地址:2181"/>
<!--  生成远程调用对象-->
    <dubbo:reference
            id="userService" interface="com.hgzy.service.UserService"/>
   
<!--   创建订单对象-->
  <bean id="orderService" class="com.hgzy.service.OrderServiceImpl">
        <property name="userService" ref="userService"/>
<    </bean>
</beans>
测试类
代码语言:javascript复制
public class TestConsumer {
    public static void main(String[] args) throws IOException {
        ApplicationContext ac=
                new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
        OrderService orderService = (OrderService) ac.getBean(OrderService.class);
        for (UserAddress userAddress : orderService.initOrder(1)) {
            System.out.println(userAddress);
        }
        System.in.read();
    }
}

0 人点赞