Dubbo学习

分布式服务架构RPC

1.分布式基础理论
单体架构—>垂直应用架构—>分布式服务架构RPC(远程过程调用)
进程内通讯(单体)

特点

流动计算架构(调度中心,提高集群利用率)
决定RPC 框架的两个核心模块 (速度+序列化以及反序列化的效率)

Dubbo中角色分为

注册中心
服务提供者
服务消费者
Dubbo框架容器
监控中心

Init 初始化
Async 异步
Sync 同步
在这里插入图片描述

项目配置

项目启动前提:下载 zookpeeper 并启动
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

java -jar 启动 dubbo 管理界面
项目从github上下载而来
在这里插入图片描述
localhost:7001 打开
在这里插入图片描述
在这里插入图片描述
dubbo的监控中心
在这里插入图片描述
start.bat启动
访问地址
http://localhost:8080/services.html
在这里插入图片描述

项目模块

gmallinterface 公共实体+接口
在这里插入图片描述
UserAddress 实体类

import java.io.Serializable;

/**
 * 用户地址
 * @author lfy
 *
 */
public class UserAddress implements Serializable {
	
	private Integer id;
    private String userAddress; //用户地址
    private String userId; //用户id
    private String consignee; //收货人
    private String phoneNum; //电话号码
    private String isDefault; //是否为默认地址    Y-是     N-否
    
    public UserAddress() {
		super();
		// TODO Auto-generated constructor stub
	}
    
	public UserAddress(Integer id, String userAddress, String userId, String consignee, String phoneNum,
                       String isDefault) {
		super();
		this.id = id;
		this.userAddress = userAddress;
		this.userId = userId;
		this.consignee = consignee;
		this.phoneNum = phoneNum;
		this.isDefault = isDefault;
	}
	
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getUserAddress() {
		return userAddress;
	}
	public void setUserAddress(String userAddress) {
		this.userAddress = userAddress;
	}
	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 接口

public interface OrderService {
    public void intoOrder(String userId);

}

UserService 接口

public interface UserService {
	
	/**
	 * 按照用户id返回所有的收货地址
	 * @param userId
	 * @return
	 */
	public List<UserAddress> getUserAddressList(String userId);

}

pom依赖

<?xml version="1.0" encoding="UTF-8"?>
<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>gmall-interface</artifactId>
    <version>1.0-SNAPSHOT</version>

<dependencies>
    <dependency>
        <groupId>com.atguigu.gmall</groupId>
        <artifactId>gmall-interface</artifactId>
        <version>1.0-SNAPSHOT</version>
    </dependency>
    <!--引入Dubbo依赖-->
    <!-- https://mvnrepository.com/artifact/com.alibaba/dubbo -->
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>dubbo</artifactId>
        <version>2.6.2</version>
    </dependency>
    <!--注册中心使用的是 zookeeper ,需要引入操作zookeeper的客户端-->
    <dependency>
        <groupId>org.apache.curator</groupId>
        <artifactId>curator-framework</artifactId>
        <version>2.12.0</version>
    </dependency>
</dependencies>
</project>

orderserviceconsumer 服务消费者
在这里插入图片描述
OrderServiceImpl 实现类

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

import java.util.List;

/**
 * @program: orderserviceconsumer
 * @description:
 * @author: Long Ao Tian
 * @create: 2020-08-16 12:53
 * 1.将服务提供者注册到注册中心(暴露服务)
 *      1.导入Dubbo依赖,操作zookeeper的客户端(curator)
 *      2. 配置服务提供者
 * 2 让服务消费者去注册中心订阅服务提供则的地址
 **/
@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    UserService userService;
    @Override
    public void intoOrder(String userId) {
        System.out.println("用户id:"+userId);
        // 1.查询用户的收货地址
        List<UserAddress> userAddressList = userService.getUserAddressList(userId);
        for (UserAddress user:userAddressList
             ) {
            System.out.println(user.getUserAddress());
        }
    }
}

MainApplication 启动类

import com.atguigu.gmall.service.OrderService;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.io.IOException;

/**
 * @program: parent
 * @description:
 * @author: Long Ao Tian
 * @create: 2020-08-16 14:18
 **/
public class MainApplication {
    public static void main(String[] args) throws IOException {
        ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("consumer.xml");
        OrderService orderService = classPathXmlApplicationContext.getBean(OrderService.class);
        orderService.intoOrder("1");
        System.out.println("调用完成......");
        System.in.read();
    }

}

consumer.xml 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://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="orderserviceconsumer">
    </dubbo:application>
    <dubbo:registry address="zookeeper://127.0.0.1:2181"></dubbo:registry>

    <!--申明需要调用的远程服务接口,生成远程代理服务-->
    <dubbo:reference interface="com.atguigu.gmall.service.UserService" id="userService"></dubbo:reference>

    <!--连接监控中心-->
    <dubbo:monitor protocol="registry"></dubbo:monitor>
</beans>

pom依赖

<?xml version="1.0" encoding="UTF-8"?>
<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>con.atguigu.gmall</groupId>
    <artifactId>order-service-consumer</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>

        <dependency>
            <groupId>com.atguigu.gmall</groupId>
            <artifactId>gmall-interface</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>
</project>

userserviceprovider 服务提供者
在这里插入图片描述
UserServiceImpl 实现类

import com.atguigu.gmall.bean.UserAddress;
import com.atguigu.gmall.service.UserService;

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

/**
 * @program: userserviceprovider
 * @description:
 * @author: Long Ao Tian
 * @create: 2020-08-16 12:48
 **/
public class UserServiceImpl  implements UserService {

    @Override
    public List<UserAddress> getUserAddressList(String userId) {
        UserAddress userAddress1=new UserAddress(1,"北京市","1","李老师","oo","1");
        UserAddress userAddress2=new UserAddress(2,"北京市","1","李老师","oo","1");
        return Arrays.asList(userAddress1,userAddress2);
    }
}

MainApplication 服务提供者启动类

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.io.IOException;

/**
 * @program: parent
 * @description:
 * @author: Long Ao Tian
 * @create: 2020-08-16 14:05
 **/
public class MainApplication {
    public static void main(String[] args) throws IOException {
        ClassPathXmlApplicationContext ac=new ClassPathXmlApplicationContext("provider.xml");
        ac.start();
        System.in.read();// 按任意键退出
    }
}

provider.xml 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://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">
    <!--1.指定当前服务/应用的名称(同样的服务名字相同,不要和别的服务重名)-->
    <dubbo:application name="userserviceprovider"></dubbo:application>
    <!--2 指定注册中心的位置-->
    <dubbo:registry address="zookeeper://127.0.0.1:2181"></dubbo:registry>
    <!--3.指定通信规则 (通信协议 通信端口)-->
    <dubbo:protocol name="dubbo" port="20880"></dubbo:protocol>
    <!--暴露服务  ref 指向服务真正的实现-->
    <dubbo:service interface="com.atguigu.gmall.service.UserService" ref="UserServiceImpl"></dubbo:service>
    <!--服务的实现-->
    <bean id="UserServiceImpl" class="com.atguigu.gmall.service.impl.UserServiceImpl"></bean>

    <!--连接监控中心-->
    <dubbo:monitor protocol="registry"></dubbo:monitor>
</beans>

pom文件依赖

<?xml version="1.0" encoding="UTF-8"?>
<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>user-service-provider</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>com.atguigu.gmall</groupId>
            <artifactId>gmall-interface</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>
</project>

https://www.bilibili.com/video/BV1ns411c7jV?p=12
P12集

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值