乐趣区

关于java:dubbo框架

环境筹备

  1. Centos8.0
  2. Zookeeper
  3. IDEA

当然也能够装置到 win10 上,不过本文是装置在 linux 上的。
参考文档:centos8 装置 zookeeper(单机形式)
注:zookeeper-3.5.6 版本全副替换 zookeeper-3.6.2 即可
我依照上述文章装置部署没遇到坑,就不再赘述。

Zookeeper 客户端插件

咱们间接在 IDEA 上点击 File>Settings>Plugins 而后搜寻 zookeeper,install 后重启即可。

如果查找不到点击我帮你解决
装置好插件后点击 File>Settings>Other Settings顺次点击:

即可看到左侧呈现 zookeeper 侧边栏

构建模块

顺次创立:

member 模块(只有 api, 并不提供实现):springboot_duboo_member_api
member Service(依赖 member 模块,api 的实现):

springboot_dubbo_member_service_api

order 模块(依赖 member 模块):

spring_dubbo_order_service

member 模块

member 接口:MemberService.java

package com.baba.wlb.member.service;

/**
 * @Author wulongbo
 * @Date 2020/12/31 11:48
 * @Version 1.0
 */
public interface MemberService {public String getUser(Long userId);
}


子模块中没有启动类,须要在 pom 中排除一下依赖:
pom.xml

<?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">
    <parent>
        <artifactId>springboot_emqx</artifactId>
        <groupId>com.baba.wlb</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>springboot_duboo_member_api</artifactId>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <skip>true</skip>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

member 实现类模块

member 实现类接口:MemberServiceImpl.java

package com.baba.wlb.member.service.impl;


import com.baba.wlb.member.service.MemberService;

/**
 * @Author wulongbo
 * @Date 2020/12/31 11:52
 * @Version 1.0
 */
public class MemberServiceImpl implements MemberService {public String getUser(Long userId) {System.out.println("order 服务调用 member 服务 userId:" + userId);
        return "order 服务调用 member 服务";
    }
}

spring-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://code.alibabatech.com/schema/dubbo
             http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

    <!-- 提供方利用信息,用于计算依赖关系 在 dubbo-admin 或 dubbo-monitor 会显示这个名字,不便辨识 -->
    <dubbo:application name="hello-world-app"/>
    <!-- 应用 zookeeper/multicast 播送注册核心裸露服务地址 -->
    <dubbo:registry address="zookeeper://39.102.56.91:2181"/>
    <!-- 用 dubbo 协定在 20880 端口裸露服务 -->
    <dubbo:protocol name="dubbo" port="20880"/>
    <!-- 申明须要裸露的服务接口 -->
    <dubbo:service interface="com.baba.wlb.member.service.MemberService" ref="memberService" protocol="dubbo"/>
    <!-- 具体实现该接口的 bean -->
    <bean id="memberService" class="com.baba.wlb.member.service.impl.MemberServiceImpl"/>

</beans>

application 启动类:AppMemberApplication.java

package com.baba.wlb.member.service.impl;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.io.IOException;

/**
 * @Author wulongbo
 * @Date 2020/12/31 14:44
 * @Version 1.0
 */
public class AppMemberApplication {public static void main(String[] args) throws IOException {ClassPathXmlApplicationContext applicationContext=new ClassPathXmlApplicationContext("spring-provider.xml");
        applicationContext.start();
        System.out.println("member 服务启动胜利...");
        System.in.read();// 放弃服务始终在运行}
}

外围 Maven 依赖:

<?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">
    <parent>
        <artifactId>springboot_emqx</artifactId>
        <groupId>com.baba.wlb</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>springboot_dubbo_member_service_api</artifactId>

    <dependencies>

        <dependency>
            <groupId>com.baba.wlb</groupId>
            <artifactId>springboot_duboo_member_api</artifactId>
            <version>1.0-SNAPSHOT</version>
            <scope>compile</scope>
        </dependency>

        <!--ZK 客户端工具 -->
        <dependency>
            <groupId>com.101tec</groupId>
            <artifactId>zkclient</artifactId>
<!--            <exclusions>-->
<!--                <exclusion>-->
<!--                    <groupId>org.slf4j</groupId>-->
<!--                    <artifactId>slf4j-log4j12</artifactId>-->
<!--                </exclusion>-->
<!--            </exclusions>-->
            <version>0.10</version>
        </dependency>

        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.2</version>
        </dependency>

        <!--dubbo 底层框架基于 netty 实现 -->
        <dependency>
            <groupId>org.jboss.netty</groupId>
            <artifactId>netty</artifactId>
            <version>3.2.5.Final</version>
        </dependency>

        <!--spring 框架组件 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.3.9.RELEASE</version>
        </dependency>

        <!--dubbo 框架 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.5.3</version>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.jboss.netty</groupId>
                    <artifactId>netty</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-log4j12</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

    </dependencies>

</project>

注:这里须要排除 slf4j 依赖,如果启动报错能够手动删除抵触 jar

因为删除了相干 jar 后,后盾启动会报错:

log4j:WARN No appenders could be found for logger (com.alibaba.dubbo.common.logger.LoggerFactory).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.


咱们只需在 resource目录下手动增加 log4j.properties即可:

log4j.rootLogger=DEBUG, Console
#Console
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n
log4j.logger.java.sql.ResultSet=INFO
log4j.logger.org.apache=INFO
log4j.logger.java.sql.Connection=DEBUG
log4j.logger.java.sql.Statement=DEBUG
log4j.logger.java.sql.PreparedStatement=DEBUG

子模块截图:

order 服务模块

application 启动类:OrderToMember.java

package com.baba.wlb.order;

import com.baba.wlb.member.service.MemberService;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @Author wulongbo
 * @Date 2020/12/31 15:56
 * @Version 1.0
 */
public class OrderToMember {public static void main(String[] args) {ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("consumer.xml");
        MemberService memberService = applicationContext.getBean(MemberService.class);
        String resultUser = memberService.getUser(1l);
        applicationContext.start();
        System.out.println("order 服务调用 member 服务返回后果:" + resultUser);
    }
}

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"
       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="order-consumer"/>

    <!--     应用 multicast 播送注册核心裸露发现服务地址 -->
    <!--     <dubbo:registry address="multicast://224.5.6.7:1234" /> -->

    <!-- 应用 zk 播送注册核心裸露服务地址 -->
    <dubbo:registry id="zk1" address="zookeeper://39.102.56.91:2181" protocol="zookeeper"/>

    <!-- 指定是用 zk-->
    <dubbo:consumer registry="zk1"/>

    <!-- 生成近程服务代理,能够和本地 bean 一样应用 demoService -->
    <dubbo:reference id="memberService" interface="com.baba.wlb.member.service.MemberService"/>

</beans>

pom 文件 依赖于 member 模块,copy member 服务的就 OK

<?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">
    <parent>
        <artifactId>springboot_emqx</artifactId>
        <groupId>com.baba.wlb</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>spring_dubbo_order_service</artifactId>

    <dependencies>

        <dependency>
            <groupId>com.baba.wlb</groupId>
            <artifactId>springboot_duboo_member_api</artifactId>
            <version>1.0-SNAPSHOT</version>
            <scope>compile</scope>
        </dependency>

        <!--ZK 客户端工具 -->
        <dependency>
            <groupId>com.101tec</groupId>
            <artifactId>zkclient</artifactId>
            <!--            <exclusions>-->
            <!--                <exclusion>-->
            <!--                    <groupId>org.slf4j</groupId>-->
            <!--                    <artifactId>slf4j-log4j12</artifactId>-->
            <!--                </exclusion>-->
            <!--            </exclusions>-->
            <version>0.10</version>
        </dependency>

        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.2</version>
        </dependency>

        <!--dubbo 底层框架基于 netty 实现 -->
        <dependency>
            <groupId>org.jboss.netty</groupId>
            <artifactId>netty</artifactId>
            <version>3.2.5.Final</version>
        </dependency>

        <!--spring 框架组件 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.3.9.RELEASE</version>
        </dependency>

        <!--dubbo 框架 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.5.3</version>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.jboss.netty</groupId>
                    <artifactId>netty</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-log4j12</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

    </dependencies>

</project>

启动服务

duboo 流程剖析:1.Provider 注册到注册核心 Registry(zookeeper)中 >2. 当节点发生变化的时候,注册核心通过事件告诉的形式告诉给 Consumer(tcp 长链接的形式),比照 Eureka 则是每隔一段时间定时去查,http 协定做心跳的形式 >3.Consumer 获取到 value 实在地址后采纳本地 RPC 近程调用技术进行拜访接口 >4. 每一次 Consumer 调用 Provider,监控核心 Monitor 都会做一次记录

启动 member 服务:AppMemberApplication.java

察看咱们的 zookeeper:

在启动 order 服务:OrderToMember.java

发现咱们调用胜利!
注:这里不再是 jar 包调用 jar 包的形式,因为这里咱们只是援用了接口,并没有援用实现,实现都是通过底层转换成 netty,dubbo 调用的过程

dubbo-admin 平台

底层实现连贯 zookeeper 信息,进行解析并转换成页面进行展现。

公共的资源 dubbo-admin 下载地址

咱们应用老版本,所以切换分支至 master,点击下载,将 zip 包解压之后,应用 IDEA 关上我的项目。
或者间接在此地址关上并下载 dubbo-admin
关上我的项目后:

首先执行 clean 操作,其次执行 install 操作
老版本:因为我 zookeeper 是装置在 linux 上的,所以须要批改 application.propertiesdubbo.properties 两个文件,如果是本地装置的能够跳过上面步骤。

能够依据本身状况批改一些参数

application.properties
# 设置端口
server.port=7001
spring.velocity.cache=false
spring.velocity.charset=UTF-8
spring.velocity.layout-url=/templates/default.vm
spring.messages.fallback-to-system-locale=false
spring.messages.basename=i18n/message
spring.root.password=root
spring.guest.password=guest
dubbo.registry.address=zookeeper://39.102.56.91:2181
dubbo.properties
dubbo.container=log4j,spring,registry,jetty-monitor
dubbo.application.name=simple-monitor
dubbo.application.owner=dubbo
#dubbo.registry.address=multicast://224.5.6.7:1234
dubbo.registry.address=zookeeper://39.102.56.91:2181
#dubbo.registry.address=redis://127.0.0.1:6379
#dubbo.registry.address=dubbo://127.0.0.1:9090
dubbo.protocol.port=7070
dubbo.jetty.port=8080
dubbo.jetty.directory=${user.home}/monitor
dubbo.charts.directory=${user.home}/monitor/charts
dubbo.statistics.directory=${user.home}/monitor/statistics
dubbo.log4j.file=logs/dubbo-monitor-simple.log
dubbo.log4j.level=WARN

启动 DubboAdminApplication.java

拜访 http://localhost:7001/
并输出账号:root, 明码:root

点击 服务治理 > 服务 能够看到咱们已注册的服务

点击服务:

此外咱们还能够看到日志,版本等信息!
至此,spring 就实现了简略的 dubbo 集成,咱们也能够用 springboot 来集成更简便!

退出移动版