dubbo 实现简易分布式服务

dubbo 实现简易分布式服务

服务器需要搭建zookeeper环境

zookeeper端口2181

还需要有java环境

1.需求

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

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

模块功能
订单服务web模块创建订单等
用户服务service模块查询用户地址等

测试预期结果

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

2.工程架构

image-20220118114329482

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-aOAIZG4h-1642477640192)(C:\Users\jiejie\AppData\Roaming\Typora\typora-user-images\image-20220118114313932.png)]

3.创建项目

3.1 公共接口层 ego-interface

简单maven项目即可

image-20220118105604937

1.修改pom.xml文件
<dependencies>
    <dependency>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId>
      <version>${lombok.vsersion}</version>
    </dependency>
  </dependencies>
2.创建实体类
@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
public interface UserService {
    /**
     * 查询用户的所有地址
     * @return
     */
    public List<UserAddress> queryAllAddress(Integer id);
}

OrderService
public interface OrderService {
    /**
     * 根据用户id 查找订单
     * @param uid 用户id
     * @return 订单
     */
    public List<UserAddress> initOrder(Integer uid);
}

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

1.修改pom.xml文件
<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.创建服务实体类
@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
<?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
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文件
<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.创建服务实体类
@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
<?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>

测试类
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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值