前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >RPC通信--HSF框架

RPC通信--HSF框架

作者头像
猿码优创
发布2019-07-27 20:15:02
3.8K0
发布2019-07-27 20:15:02
举报
文章被收录于专栏:猿码优创猿码优创

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

file
file

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

HSF提供了两种开发模式

HSF概述:

file
file
file
file
file
file
file
file

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

file
file

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

file
file

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

  1. 下载Ali-Tomcat :点我直接下载哦!
  2. 下载好了找个地方解压并保存(和Tomcat其实没什么区别,只是被阿里那帮大佬给封装了)存放目录(D:\dev\apps\EDAS\taobao-tomcat-7.0.59)
file
file

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

4.保存后将内容解压至上述保存的 Ali-Tomcat 的 deploy 目录(D:\dev\apps\EDAS\taobao-tomcat-7.0.59\deploy)下

file
file

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

file
file

5.1.2 安装ecplise插件

file
file

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

5.1.3 配置刚刚安装的插件

file
file

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

file
file

(adsbygoogle =window.adsbygoogle ||[]).push({});

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:\dev\apps\EDAS\taobao-tomcat-7.0.59\deploy\taobao-hsf.sar说明:D:\dev\apps\EDAS\taobao-tomcat-7.0.59\deploy\taobao-hsf.sar需要替换为在本地安装 Pandora 的实际路径。单击 Apply 或 OK 完成配置(具体如下图)。

file
file

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

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

6.2 解压配置中心压缩包 并保存(栗:D:\dev\apps\EDAS\edas-lite-configcenter)

file
file

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

file
file

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

file
file

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

file
file

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

file
file

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

file
file

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

file
file

没有报错即可启动成功

file
file

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

file
file

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

file
file

启动完成就开始消费了。

file
file

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

file
file

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

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

file
file

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

file
file

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

file
file

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

file
file

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

file
file

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

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

file
file

开发配置:

  1. 需要配置私服环境(我的maven没有在默认的。一般都在默认maven环境 ~/.m2/settings.xml 中,在 settings.xml 中加入如下配置:)
file
file
代码语言:javascript
复制
    <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
file

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

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

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

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

file
file

引入jar包:

file
file

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

file
file

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

file
file

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

file
file

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

file
file

直接启动main方法: server端:

file
file

client:

file
file

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

file
file

我们来调用一下server的服务

file
file

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

file
file

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

file
file

install打包报下图异常

file
file

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

file
file

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

file
file

引入依赖

file
file

注入接口:

file
file

一定要和服务端分组名称 路径 和版本号一值。

file
file

调用接口

file
file

测试一下: 启动服务端:

file
file

启动客户端:

file
file

查询监控中心:

file
file
file
file

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

file
file

实现类方法:

file
file

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

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-04-03,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
微服务引擎 TSE
微服务引擎(Tencent Cloud Service Engine)提供开箱即用的云上全场景微服务解决方案。支持开源增强的云原生注册配置中心(Zookeeper、Nacos 和 Apollo),北极星网格(腾讯自研并开源的 PolarisMesh)、云原生 API 网关(Kong)以及微服务应用托管的弹性微服务平台。微服务引擎完全兼容开源版本的使用方式,在功能、可用性和可运维性等多个方面进行增强。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档