dubbo--知识点

一、SOA:是一种面向服务的架构,是一种架构设计思想

比如有多个项目都访问一个数据库,比较不安全,或是代码重复量大,

可一把访问数据的部门设置成一种服务或是一个项目,所有的项目都通过这个服务访问数据库,增加安全性,减少代码重复量

项目和服务间和用http,或者webservice协议,或者是基于socket的RPC,但随着业务的越来越多,越来越服务,服务也会越来越多,越来越服务,

所以还需要服务治理,dobbo和springcloud就可以

实际上SOA只是一种架构设计模式,而SOAP、REST、RPC就是根据这种设计模式构建出来的规范,其中SOAP通俗理解就是http+xml的形式,REST就是http+json的形式,RPC是基于socket的形式。上文提到的CXF就是典型的SOAP/REST框架,dubbo就是典型的RPC框架,而SpringCloud就是遵守REST规范的生态系统
soa 基于soap协议, http +xml比较占带宽,soa缺少服务的治理

实现SOA架构是,常用服务

1.dubbo

2.webservice

3.dubbox   当当网

4.httpclient

二、RPC 远程过程调用协议

客户端调用远程服务端,只能调用,不知道具体实现,安全性比较高

三、DUBBO

1、Provider:服务提供方,服务端 提供服务

2、Consumer:消费方,客户端调用服务端,观察者模式,消息订阅,根据注册中心提供的服务端地址去调用服务端,调用 服务端代理对象

3、Container:Dubbo 容器,依赖于spring容器

4、Registry:注册中心,容器启动的时候把服务注册到注册中心,让客户端去调用

5、monitor:监听器,provider和consumer 每隔1分钟向monitor发送调用的统计信息

 6、注册中心

在防火墙配置文件中放行端口

vim /etc/sysconfig/iptables

重启防火墙

service iptables restart

7、dubbo 协议

dubbo 协议    nio服用单一长连接,大文件传输时可能会有问题

rmi 协议

hessian 协议 http协议

8、dubbo-admin.war   监测提供方,消费方等相关信息   放到tomcat中修改   dubbo.properties

中的  zookeeper服务器地址

放到tomcat

接口项目和实现类项目要分开建  ,接口的实现用dubbo,接口类   provider和 consumer共用,

consumer只有接口,没有接口的具体实现,否则就不是rpc了

9、provider 消息提供

<dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.6.6</version>
        </dependency>
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.4.10</version>
        </dependency>
        <dependency>
            <groupId>com.101tec</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.5</version>
        </dependency>
        <dependency>
            <groupId>io.netty</groupId>
            <artifactId>netty-all</artifactId>
            <version>4.1.32.Final</version>
        </dependency>
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-framework</artifactId>
            <version>2.8.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-recipes</artifactId>
            <version>2.8.0</version>
        </dependency>
  </dependencies>
<?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="provider" owner="dubbo-app">
       <!--  <dubbo:parameter key="qos.enable" value="true"/>
        <dubbo:parameter key="qos.accept.foreign.ip" value="false"/>
        <dubbo:parameter key="qos.port" value="55555"/> -->
    </dubbo:application>

    <dubbo:monitor protocol="registry"/>

    <!--dubbo这个服务所要暴露的服务地址所对应的注册中心-->
    <!--<dubbo:registry address="N/A"/>-->
    <!-- <dubbo:registry address="zookeeper://localhost:2181" check="false"/> -->
    
    <dubbo:registry protocol="zookeeper" address="192.168.233.100:2181"/>

    <!--当前服务发布所依赖的协议;webserovice、Thrift、Hessain、http-->
    <dubbo:protocol name="dubbo" port="20880"/>

    <!--服务发布的配置,需要暴露的服务接口-->
    <dubbo:service
            interface="com.tj.service.UserServiceI"
            ref="userService"/>

    <!--Bean bean定义-->
    <bean id="userService" class="com.tj.service.UserServiceImpl"/>

</beans>

配置service  不能用dubbo注解@service   因为与spring   aop冲突

public static void main(String[] args) throws IOException {
        
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("provider.xml");
        context.start();
        //默认加载/mete-inf/spring/*.xml
        //Main.main(args);
        System.in.read();

    }

10、consumer  消费者,消费者配置文件可以包扫买不会冲突

<?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="consumer" >
    </dubbo:application>
    
    <dubbo:registry protocol="zookeeper" address="192.168.233.100:2181"/>
    
    <dubbo:annotation package="com.consumer.service.impl"/>


</beans>
public class TestServiceImpl implements TestService{
    
    @Reference
    private UserService userService;

    @Override
    public void add(String name) {
        
    }

}

11.maven打包插件   让单独的jar可运行

maven-assembly-plugin

12、dubbo和springcloud 区别

dubbo 是rpc框架的实现

springcloud是微服务的一套解决方案框架

Dubbo底层是使用Netty这样的NIO框架,是基于TCP协议传输的,配合以Hession序列化完成RPC通信。而SpringCloud是基于Http协议+rest接口调用远程过程的通信,相对来说,Http请求会有更大的报文,占的带宽也会更多。但是REST相比RPC更为灵活,服务提供方和调用方的依赖只依靠一纸契约,不存在代码级别的强依赖,这在强调快速演化的微服务环境下,显得更为合适,至于注重通信速度还是方便灵活性,具体情况具体考虑。

原文地址:https://www.cnblogs.com/jentary/p/12300517.html