Java-RPC通信--HSF框架

最近leader给了KingYiFan一个任务,就是对接某国企的业务,人家用的淘宝的HSF框架RPC通信 根本不用httpclient what??? RPC不是Dubbo底层协议吗?这怎么通讯呢?翻遍了整个百度没有我想要的。 有一个大佬人家自己封装了一个RPC通讯含监控中心(积分下载的)需要联系我哈。

file

最后还是去老老实实看官网吧。 以下知识是我昨天学的,如有错误请指出。

HSF提供了两种开发模式

HSF概述:

file

file

file

file

HSF框架有两种开发方式(Ali-tomcat、Pandora Boot):

file

我们从Ali-tomcat开始说起。。 Ali-Tomcat概述:

file

安装 Ali-Tomcat 和 Pandora 并配置开发环境

  1. 下载Ali-Tomcat :点我直接下载哦!

  2. 下载好了找个地方解压并保存(和Tomcat其实没什么区别,只是被阿里那帮大佬给封装了)存放目录(D:devappsEDAS aobao-tomcat-7.0.59)

file

3.下载 Pandora 容器。点我直接下载哦!

4.保存后将内容解压至上述保存的 Ali-Tomcat 的 deploy 目录(D:devappsEDAS aobao-tomcat-7.0.59deploy)下

file

5.配置开发环境: 5.1 Ecplise配置: 5.1.1 下载 Tomcat4E 插件,并解压至本地(如:D:devappsEDAS omcat4e)。

file 5.1.2 安装ecplise插件

file

返回 Install 对话框,单击 Select All,然后单击 Next。 后续还有几个步骤,按界面提示操作即可。安装完成后,Eclipse 需要重启,以使 Tomcant4E 插件生效。 (Tips:建议安装ecplise插件的时候断网。要不他就会远端拉取。远端要是在国内还好,要是在国外。呵呵呵呵。。。。。)

5.1.3 配置刚刚安装的插件

file

5.1.4 直接Run启动服务出现下图证明ecplise安装完成:

file

5.2 IDEA配置:

5.2.1 从菜单栏中选择 Run > Edit Configuration。在 Run/Debug Configuration 页面左侧的导航栏中选择 Defaults > Tomcat Server > Local。配置 AliTomcat。 在右侧页面单击 Server 页签,然后在 Application Server 区域单击 Configure在 Application Server 页面右上角单击 +,然后在 Tomcat Server 对话框中设置 Tomcat Home 和 Tomcat base directory 路径,单击 OK。将 Tomcat Home 的路径设置为本地解压后的 Ali-Tomcat 路径,Tomcat base directory 可以自动使用该路径,无需再设置。在 Application Server 区域的下拉菜单中,选择刚刚配置好的 Ali-Tomcat。在 VM Options 区域的文本框中,设置 JVM 启动参数指向 Pandora 的路径,如:-Dpandora.location=D:devappsEDAS aobao-tomcat-7.0.59deploy aobao-hsf.sar说明:D:devappsEDAS aobao-tomcat-7.0.59deploy aobao-hsf.sar需要替换为在本地安装 Pandora 的实际路径。单击 Apply 或 OK 完成配置(具体如下图)。

file

6.安装配置中心(俗称监控中心):本地正确配置环境变量 JAVA_HOME,指向一个 1.6 或 1.6 以上版本的 JDK。确认 8080 和 9600 端口未被使用。

6.1 下载配置中心:点我下载最新版的配置中心

6.2 解压配置中心压缩包 并保存(栗:D:devappsEDASedas-lite-configcenter)

file

6.3 启动配置环境中心: 6.3.1 windows 直接启动startup.bat文件

file

6.3.2 linux 请在当前目录下执行 sh startup.sh 命令。

file

7.修改host文件(为什么要修改host文件呢? 因为EDAS的hsf在阿里云是收费的 没有注册是不能使用的。我们直接让程序找本地) 对于需要使用轻量配置中心的开发机器,请在本地 DNS(hosts 文件)中,将 jmenv.tbsite.net 域名指向启动了 EDAS 配置中心的机器 IP。 hosts 文件的路径如下: Windows 操作系统:C:WindowsSystem32driversetchosts Unix 操作系统:/etc/hosts(具体如下图)

file

访问:http://jmenv.tbsite.net:8080/ (看到下图就是配置中心安装完成)

file

可以看下官网HSF提供的Demo点我下载哦! 解压下载的压缩包,可以看到carshop文件夹,里面包含 itemcenter-api,itemcenter 和 detail 三个 Maven 工程文件夹。 itemcenter-api:提供接口定义 itemcenter:生产者服务 detail:消费者服务

file

启动server服务 选择项目 选择端口

file

没有报错即可启动成功

file

访问监控中心 就可以看到刚刚服务。

file

启动client消费端(步骤跟server服务一样)

file

启动完成就开始消费了。

file

仔细看他们的Demo(只要监听到服务启动 就开始消费。并且写了一个线程 循环调用执行)

file

查询监控中心有服务调用者。这个时候HSF AliTomcat基本就没问题了。

我们来看下HSF 怎么写的。 我们看到这个图 依赖关系 itemcenter 依赖 itemcenter-api jar包 detail 依赖 itemcenter-api文件

file

server端 实现类 引入阿里的jar包

file

写实现类 然后在xml发布一个服务 (实现类实现api提供的接口)

file

提供给用户用的接口jar包工程(这个工程职业接口和实体类)

file

消费端(也是客户端 依赖api工程)

file

很简单的 HSF-Alitomcat demo就弄完了,剩下只需要写你的业务就好了。更多详细请查阅EDAS-HSF官网

再说一下HSF另一种开发方式:使用Pandora Boot开发

file

开发配置:

  1. 需要配置私服环境(我的maven没有在默认的。一般都在默认maven环境 ~/.m2/settings.xml 中,在 settings.xml 中加入如下配置:)

file

    <profiles>
        <profile>
            <id>nexus</id>
            <repositories>
                <repository>
                    <id>central</id>
                    <url>http://repo1.maven.org/maven2</url>
                    <releases>
                        <enabled>true</enabled>
                    </releases>
                    <snapshots>
                        <enabled>true</enabled>
                    </snapshots>
                </repository>
            </repositories>
            <pluginRepositories>
                <pluginRepository>
                    <id>central</id>
                    <url>http://repo1.maven.org/maven2</url>
                    <releases>
                        <enabled>true</enabled>
                    </releases>
                    <snapshots>
                        <enabled>true</enabled>
                    </snapshots>
                </pluginRepository>
            </pluginRepositories>
        </profile>
        <profile>
            <id>edas.oss.repo</id>
            <repositories>
                <repository>
                    <id>edas-oss-central</id>
                    <name>taobao mirror central</name>
                    <url>http://edas-public.oss-cn-hangzhou.aliyuncs.com/repository</url>
                    <snapshots>
                        <enabled>true</enabled>
                    </snapshots>
                    <releases>
                        <enabled>true</enabled>
                    </releases>
                </repository>
                </repositories>
            <pluginRepositories>
                <pluginRepository>
                    <id>edas-oss-plugin-central</id>
                    <url>http://edas-public.oss-cn-hangzhou.aliyuncs.com/repository</url>
                    <snapshots>
                        <enabled>true</enabled>
                    </snapshots>
                    <releases>
                        <enabled>true</enabled>
                    </releases>
                </pluginRepository>
            </pluginRepositories>
        </profile>
    </profiles>
    <activeProfiles>
        <activeProfile>nexus</activeProfile>
        <activeProfile>edas.oss.repo</activeProfile>
    </activeProfiles>

在命令行执行如下命令 mvn help:effective-settings 。

file

1.1. 无报错,表明 setting.xml 文件格式没问题。 1.2. profiles 中包含 edas.oss.repo 这个 profile,表明私服已经配置到 profiles 中。 1.3. 在 activeProfiles 中 包含 edas.oss.repo 属性,表明 edas.oss.repo 私服已激活。 下面证明成功

file

  1. 配置我刚上面说的监控中心

file

下载官方提供的DEMO:点我下载生产者服务哦!点我下载客户消费者哦!

来我们看看项目的结构(这次没有api jar包两个都是SpringBoot项目)

file

引入jar包:

file server服务者和第一种区别就是 用的注解更简单了呢。注册服务

file consumer 消费端 创建一个config 然后用@HSFConsumer 注入

file 只需要用一次注解 别的地方直接可以用Autowired 注解直接注入。

file

消费者 和生成者两个启动类必须加入一下代码哦: file

直接启动main方法: server端:

file

client:

file

看到监控中心如下图(证明就搭建成功了)

file

我们来调用一下server的服务

file 有木有有人会问我为什么是18082端口呢? file

可不可以调用用Ali-Tomcat服务呢。 先把这个jar包install

file

install打包报下图异常

file

解决方案如下图(也可以安装jdk的时候不装jre)

file 打包成功并且放到本地仓库

file

引入依赖 file

注入接口:

file

一定要和服务端分组名称 路径 和版本号一值。 file 调用接口 file 测试一下: 启动服务端:

file

启动客户端:

file

查询监控中心:

file

file 访问地址:http://127.0.0.1:18082/hsf-item

file 实现类方法:

file

后面还有异步调用方法(感兴趣可以自己学一下,具体看官方文档,可以与我互相交流哦!)

终。。。


鼓励作者写出更好的技术文档,就请我喝一瓶哇哈哈哈哈哈哈哈。。你们的赞助决定我更新的速度哦!

微信:

支付宝:


感谢一路支持我的人。。。。。

Love me and hold me
QQ:69673804(16年老号)
EMAIL:69673804@qq.com
友链交换
如果有兴趣和本博客交换友链的话,请按照下面的格式在评论区进行评论,我会尽快添加上你的链接。

网站名称:KingYiFan’S Blog
网站地址:http://blog.cnbuilder.cn
网站描述:年少是你未醒的梦话,风华是燃烬的彼岸花。
网站Logo/头像: [头像地址](https://blog.cnbuilder.cn/upload/2018/7/avatar20180720144536200.jpg)
原文地址:https://www.cnblogs.com/kingyifan/p/11721442.html