dubbo分布式学习(一)---------项目搭建demo

首先部署zookeeper,开启(此处省略)

其次,部署dubbo-admin

  1.    下载dubbo-admin  直接解压      https://github.com/apache/incubator-dubbo-ops
  2.    dubbo-admin解压完成后,进入dubbo-admin目录,在该目录下打开cmd,执行mvn clean package命令(打包)
  3. 打包完成后会在/dubbo-admin/target目录下生成dubbo-admin-0.0.1-SNAPSHOT.jar
  4. 在cmd中使用java -jar dubbo-admin-0.0.1-SNAPSHOT.jar执行jar包。注意:在zookeeper开启的状态下执行,不然会报错
  5. 在浏览器中输入http://localhost:7001,至此dubbo-admin在windows部署成功

1.项目目录结构

(1)服务公共接口

UserAddress.java

package com.atguigu.gmall.bean;

import java.io.Serializable;

public class UserAdress implements Serializable {

    
    private static final long serialVersionUID = 1L;
    
    
    private Integer id;
    private String userAdress;//用户地址
    private String userId;//用户id
    private String consignee;//收货地址
    private String phoneNum;//电话号码
    private String isDefault;//是否为默认地址
    
    
    
    public UserAdress() {
        super();
        // TODO Auto-generated constructor stub
    }
    public UserAdress(Integer id, String userAdress, String userId, String consignee, String phoneNum,
            String isDefault) {
        super();
        this.id = id;
        this.userAdress = userAdress;
        this.userId = userId;
        this.consignee = consignee;
        this.phoneNum = phoneNum;
        this.isDefault = isDefault;
    }
    @Override
    public String toString() {
        return "UserAdress [id=" + id + ", userAdress=" + userAdress + ", userId=" + userId + ", consignee=" + consignee
                + ", phoneNum=" + phoneNum + ", isDefault=" + isDefault + "]";
    }
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getUserAdress() {
        return userAdress;
    }
    public void setUserAdress(String userAdress) {
        this.userAdress = userAdress;
    }
    public String getUserId() {
        return userId;
    }
    public void setUserId(String userId) {
        this.userId = userId;
    }
    public String getConsignee() {
        return consignee;
    }
    public void setConsignee(String consignee) {
        this.consignee = consignee;
    }
    public String getPhoneNum() {
        return phoneNum;
    }
    public void setPhoneNum(String phoneNum) {
        this.phoneNum = phoneNum;
    }
    public String getIsDefault() {
        return isDefault;
    }
    public void setIsDefault(String isDefault) {
        this.isDefault = isDefault;
    }

}

orderService.java

package com.atguigu.gmall.service;

public interface OrderService {

    /**
     * @TODO 初始化订单
     *  @date 2018年9月8日 下午11:04:07
     *  @author Jlq
     */
    void initOrder(String userId);
}

 

userService.java

package com.atguigu.gmall.service;

import java.util.List;

import com.atguigu.gmall.bean.UserAdress;

public interface UserService {
    
    public List<UserAdress> getUserAddressList(String userId);

}
 

这个pom.xml文件不作任何修改

(2)服务消费者

orderService.java

package com.atguigu.gmall.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.atguigu.gmall.bean.UserAdress;
import com.atguigu.gmall.service.OrderService;
import com.atguigu.gmall.service.UserService;

@Service
public class OrderServiceImpl implements OrderService {
    
    @Autowired
    UserService userService;

    public void initOrder(String userId) {
        // TODO Auto-generated method stub
        System.out.println("userId----"+userId);
        //1查询用户收获地址
        List<UserAdress> list = userService.getUserAddressList(userId);
        for (UserAdress userAdress : list) {
            System.out.println(userAdress.getUserAdress());
        }
    }

}
 

mainApplication.java

package com.atguigu.gmall;

import java.io.IOException;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.atguigu.gmall.service.OrderService;

public class MainApplication {
    
    public static void main(String[] args) throws IOException {
        @SuppressWarnings("resource")
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("consumer.xml");
        
        OrderService orderService = context.getBean(OrderService.class);
        
        orderService.initOrder("1");
        
        System.out.println("调用结束");
        System.in.read();
    }

}
consumer.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: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://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.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">
        
    <context:component-scan base-package="com.atguigu.gmall.service.impl"></context:component-scan>

    <dubbo:application name="order-service-consumer"></dubbo:application>
    
    <!-- 4.指定注册中心地址 -->
    <!-- <dubbo:registry address="zookeeper://127.0.0.1:2181"></dubbo:registry> -->
    <dubbo:registry protocol="zookeeper" address="127.0.0.1:2181"></dubbo:registry>
    
    <!-- 声明需要调用的远程接口,生成远程服务代理 -->
    <dubbo:reference interface="com.atguigu.gmall.service.UserService" id="userService"></dubbo:reference>
</beans>

pom.xml

 

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.atguigu.gmall</groupId>
  <artifactId>order-service-consumer</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  
    <dependencies>
      <dependency>
        <groupId>com.atguigu.amall</groupId>
        <artifactId>gmall-interface</artifactId>
        <version>0.0.1-SNAPSHOT</version>
      </dependency>
      
      <!-- 1.引入dubbo -->
      <!-- https://mvnrepository.com/artifact/com.alibaba/dubbo -->
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>dubbo</artifactId>
        <version>2.6.2</version>
    </dependency>
      
      <!-- 2.引入zookeeper客户端 -->
      <!-- https://mvnrepository.com/artifact/org.apache.curator/curator-framework -->
    <dependency>
        <groupId>org.apache.curator</groupId>
        <artifactId>curator-framework</artifactId>
        <version>2.12.0</version>
    </dependency>
  </dependencies>
</project>

 

(3)服务提供者

userServiceImpl.java

package com.atguigu.gmall.service.impl;

import java.util.Arrays;
import java.util.List;

import com.atguigu.gmall.bean.UserAdress;
import com.atguigu.gmall.service.UserService;
/**
 * @Description: 服务提供者
 * @author Jlq
 * @date: 2018年9月8日 下午10:58:26
 */
public class UserServiceImpl implements UserService {

    public List<UserAdress> getUserAddressList(String userId) {
        // TODO Auto-generated method stub
        
        UserAdress adress1 = new UserAdress(1, "username", "userid", "consignee", "consignee", "isDefault");
        UserAdress adress2 = new UserAdress(1, "username", "userid", "consignee", "consignee", "isDefault");
        return Arrays.asList(adress1,adress2);
    }

}
mainApplication.java

package com.atguigu.gmall;

import java.io.IOException;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MainApplication {

    public static void main(String[] args) throws IOException {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("provider.xml");
        
        context.start();
        
        System.in.read();
    }
}

provider.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: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://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">

    <!-- 3.指定当前服务 -->
    <dubbo:application name="user-service-provider"></dubbo:application>
    
    <!-- 4.指定注册中心地址 -->
    <!-- <dubbo:registry address="zookeeper://127.0.0.1:2181"></dubbo:registry> -->
    <dubbo:registry protocol="zookeeper" address="127.0.0.1:2181"></dubbo:registry>
    
    <!-- 5.指定通讯规则(通讯端口) -->
    <dubbo:protocol name="dubbo" port="20880"></dubbo:protocol>
    
    <!-- 6.暴露服务   ref指向服务真正的实现对象 -->
    <dubbo:service interface="com.atguigu.gmall.service.UserService" ref="userServiceImpl"></dubbo:service>
    
    <!-- 服务的实现 -->
    <bean id="userServiceImpl" class="com.atguigu.gmall.service.impl.UserServiceImpl"></bean>
</beans>


pom.xml

 

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.dubbo.gmall</groupId>
  <artifactId>user-service-provider</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  
  
  <dependencies>
      <dependency>
        <groupId>com.atguigu.amall</groupId>
        <artifactId>gmall-interface</artifactId>
        <version>0.0.1-SNAPSHOT</version>
      </dependency>
      
      <!-- 1.引入dubbo -->
      <!-- https://mvnrepository.com/artifact/com.alibaba/dubbo -->
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>dubbo</artifactId>
        <version>2.6.2</version>
    </dependency>
      
      <!-- 2.引入zookeeper客户端 -->
      <!-- https://mvnrepository.com/artifact/org.apache.curator/curator-framework -->
    <dependency>
        <groupId>org.apache.curator</groupId>
        <artifactId>curator-framework</artifactId>
        <version>2.12.0</version>
    </dependency>
      
  </dependencies>
</project>

项目构建之后,先执行服务提供者provider项目中的mainApplication.java,可以在dubbo管控台(dubbo-admin)中看到服务提供者,

其次执行服务消费者consumer项目,就可以看到服务消费者

over

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值