【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后重启即可。
【Java】dubbo框架
如果查找不到点击我帮你解决
安装好插件后点击 File>Settings>Other Settings依次点击:
【Java】dubbo框架
即可看到左侧出现zookeeper侧边栏
【Java】dubbo框架

构建模块

依次创建:

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);

}

【Java】dubbo框架
子模块中没有启动类,需要在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
【Java】dubbo框架
由于删除了相关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.

【Java】dubbo框架
我们只需在 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

子模块截图:
【Java】dubbo框架

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
【Java】dubbo框架
观察我们的zookeeper:
【Java】dubbo框架
在启动order服务:OrderToMember.java
【Java】dubbo框架
发现我们调用成功!
注:这里不再是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
【Java】dubbo框架
访问http://localhost:7001/
并输入账号:root,密码:root
【Java】dubbo框架
点击 服务治理>服务 可以看到我们已注册的服务
【Java】dubbo框架
点击服务:
【Java】dubbo框架
此外我们还可以看到日志,版本等信息!
至此,spring就完成了简单的dubbo集成,我们也可以用springboot来集成更简便!

以上是 【Java】dubbo框架 的全部内容, 来源链接: utcz.com/a/92375.html

回到顶部