• spring-cloud-starter-openfeign 的使用


    1.添加依赖

    1. org.springframework.cloud
    2. spring-cloud-starter-openfeign
    3. 3.1.3

    2.创建调用接口

    1. package com.wpitedatagathering.carctrl.gathering.service;
    2. import com.wpitedatagathering.carctrl.gathering.entity.VolmeasureScantask;
    3. import org.springframework.cloud.openfeign.FeignClient;
    4. import org.springframework.web.bind.annotation.GetMapping;
    5. import org.springframework.web.bind.annotation.PostMapping;
    6. import org.springframework.web.bind.annotation.RequestBody;
    7. import org.springframework.web.bind.annotation.RequestParam;
    8. import java.net.URI;
    9. @FeignClient(name="VolmeasureScanTaskFeignClient",url="EMPTY")
    10. public interface VolmeasureScanTaskFeignClient {
    11. //新增任务
    12. @PostMapping("dataSync/scanTask/add")
    13. String add(URI uri,@RequestBody VolmeasureScantask volmeasureScantask);
    14. //完成任务
    15. @GetMapping("/dataSync/scanTask/complete")
    16. String complete(URI uri,@RequestParam("taskID") String taskID);
    17. }

    3.创建实体类

    1. package com.wpitedatagathering.carctrl.gathering.entity;
    2. import com.baomidou.mybatisplus.annotation.TableId;
    3. import com.baomidou.mybatisplus.annotation.TableName;
    4. import java.io.Serializable;
    5. import java.util.Date;
    6. /**
    7. * 扫描任务
    8. * @TableName VolMeasure_ScanTask
    9. */
    10. @TableName(value = "dbo.VolMeasure_ScanTask")
    11. public class VolmeasureScantask implements Serializable {
    12. /**
    13. * 主键ID
    14. */
    15. @TableId(value="kid")
    16. private String kid;
    17. /**
    18. * 任务名称
    19. */
    20. private String taskname;
    21. /**
    22. * 添加时间
    23. */
    24. private Date adddate;
    25. /**
    26. *
    27. */
    28. private Integer isdel;
    29. /**
    30. *
    31. */
    32. private Date deldate;
    33. /**
    34. * 是否初始化完成
    35. */
    36. private Integer isinit;
    37. /**
    38. * 0 子表记录未插入完 1 子表记录插入完
    39. */
    40. private Byte iscomplete;
    41. /**
    42. *
    43. */
    44. private Integer isdetailinit;
    45. private static final long serialVersionUID = 1L;
    46. /**
    47. * 主键ID
    48. */
    49. public String getKid() {
    50. return kid;
    51. }
    52. /**
    53. * 主键ID
    54. */
    55. public void setKid(String kid) {
    56. this.kid = kid;
    57. }
    58. /**
    59. * 任务名称
    60. */
    61. public String getTaskname() {
    62. return taskname;
    63. }
    64. /**
    65. * 任务名称
    66. */
    67. public void setTaskname(String taskname) {
    68. this.taskname = taskname;
    69. }
    70. /**
    71. * 添加时间
    72. */
    73. public Date getAdddate() {
    74. return adddate;
    75. }
    76. /**
    77. * 添加时间
    78. */
    79. public void setAdddate(Date adddate) {
    80. this.adddate = adddate;
    81. }
    82. /**
    83. *
    84. */
    85. public Integer getIsdel() {
    86. return isdel;
    87. }
    88. /**
    89. *
    90. */
    91. public void setIsdel(Integer isdel) {
    92. this.isdel = isdel;
    93. }
    94. /**
    95. *
    96. */
    97. public Date getDeldate() {
    98. return deldate;
    99. }
    100. /**
    101. *
    102. */
    103. public void setDeldate(Date deldate) {
    104. this.deldate = deldate;
    105. }
    106. /**
    107. * 是否初始化完成
    108. */
    109. public Integer getIsinit() {
    110. return isinit;
    111. }
    112. /**
    113. * 是否初始化完成
    114. */
    115. public void setIsinit(Integer isinit) {
    116. this.isinit = isinit;
    117. }
    118. /**
    119. * 0 子表记录未插入完 1 子表记录插入完
    120. */
    121. public Byte getIscomplete() {
    122. return iscomplete;
    123. }
    124. /**
    125. * 0 子表记录未插入完 1 子表记录插入完
    126. */
    127. public void setIscomplete(Byte iscomplete) {
    128. this.iscomplete = iscomplete;
    129. }
    130. /**
    131. *
    132. */
    133. public Integer getIsdetailinit() {
    134. return isdetailinit;
    135. }
    136. /**
    137. *
    138. */
    139. public void setIsdetailinit(Integer isdetailinit) {
    140. this.isdetailinit = isdetailinit;
    141. }
    142. @Override
    143. public boolean equals(Object that) {
    144. if (this == that) {
    145. return true;
    146. }
    147. if (that == null) {
    148. return false;
    149. }
    150. if (getClass() != that.getClass()) {
    151. return false;
    152. }
    153. VolmeasureScantask other = (VolmeasureScantask) that;
    154. return (this.getKid() == null ? other.getKid() == null : this.getKid().equals(other.getKid()))
    155. && (this.getTaskname() == null ? other.getTaskname() == null : this.getTaskname().equals(other.getTaskname()))
    156. && (this.getAdddate() == null ? other.getAdddate() == null : this.getAdddate().equals(other.getAdddate()))
    157. && (this.getIsdel() == null ? other.getIsdel() == null : this.getIsdel().equals(other.getIsdel()))
    158. && (this.getDeldate() == null ? other.getDeldate() == null : this.getDeldate().equals(other.getDeldate()))
    159. && (this.getIsinit() == null ? other.getIsinit() == null : this.getIsinit().equals(other.getIsinit()))
    160. && (this.getIscomplete() == null ? other.getIscomplete() == null : this.getIscomplete().equals(other.getIscomplete()))
    161. && (this.getIsdetailinit() == null ? other.getIsdetailinit() == null : this.getIsdetailinit().equals(other.getIsdetailinit()));
    162. }
    163. @Override
    164. public int hashCode() {
    165. final int prime = 31;
    166. int result = 1;
    167. result = prime * result + ((getKid() == null) ? 0 : getKid().hashCode());
    168. result = prime * result + ((getTaskname() == null) ? 0 : getTaskname().hashCode());
    169. result = prime * result + ((getAdddate() == null) ? 0 : getAdddate().hashCode());
    170. result = prime * result + ((getIsdel() == null) ? 0 : getIsdel().hashCode());
    171. result = prime * result + ((getDeldate() == null) ? 0 : getDeldate().hashCode());
    172. result = prime * result + ((getIsinit() == null) ? 0 : getIsinit().hashCode());
    173. result = prime * result + ((getIscomplete() == null) ? 0 : getIscomplete().hashCode());
    174. result = prime * result + ((getIsdetailinit() == null) ? 0 : getIsdetailinit().hashCode());
    175. return result;
    176. }
    177. @Override
    178. public String toString() {
    179. StringBuilder sb = new StringBuilder();
    180. sb.append(getClass().getSimpleName());
    181. sb.append(" [");
    182. sb.append("Hash = ").append(hashCode());
    183. sb.append(", kid=").append(kid);
    184. sb.append(", taskname=").append(taskname);
    185. sb.append(", adddate=").append(adddate);
    186. sb.append(", isdel=").append(isdel);
    187. sb.append(", deldate=").append(deldate);
    188. sb.append(", isinit=").append(isinit);
    189. sb.append(", iscomplete=").append(iscomplete);
    190. sb.append(", isdetailinit=").append(isdetailinit);
    191. sb.append(", serialVersionUID=").append(serialVersionUID);
    192. sb.append("]");
    193. return sb.toString();
    194. }
    195. }

    4.调用接口

    1. package com.wpitedatagathering.carctrl.gathering.service.impl;
    2. import com.wpitedatagathering.carctrl.device.config.CarConfig;
    3. import com.wpitedatagathering.carctrl.gathering.entity.VolmeasureScantask;
    4. import com.wpitedatagathering.carctrl.gathering.service.VolmeasureScanTaskFeignClient;
    5. import com.wpitedatagathering.carctrl.gathering.service.VolmeasureScantaskService;
    6. import lombok.RequiredArgsConstructor;
    7. import org.springframework.stereotype.Service;
    8. import java.net.URI;
    9. @Service
    10. @RequiredArgsConstructor
    11. public class VolmeasureScantaskServiceImpl implements VolmeasureScantaskService {
    12. private final VolmeasureScanTaskFeignClient volmeasureScanTaskFeignClient;
    13. private final CarConfig carConfig;
    14. //新增
    15. @Override
    16. public String add(VolmeasureScantask volmeasureScantask){
    17. try{
    18. return volmeasureScanTaskFeignClient.add(new URI(carConfig.getDataSyncUri()),volmeasureScantask);
    19. }catch (Exception e){
    20. e.printStackTrace();
    21. return null;
    22. }
    23. }
    24. //完成
    25. @Override
    26. public String complete(String taskID) {
    27. try{
    28. return volmeasureScanTaskFeignClient.complete(new URI(carConfig.getDataSyncUri()), taskID);
    29. }catch (Exception e){
    30. e.printStackTrace();
    31. return null;
    32. }
    33. }
    34. }

    5.配置类:

    1. package com.wpitedatagathering.carctrl.device.config;
    2. import lombok.Data;
    3. import org.springframework.boot.context.properties.ConfigurationProperties;
    4. import org.springframework.context.annotation.Configuration;
    5. import java.util.List;
    6. import java.util.Map;
    7. //小车配置文件
    8. @Data
    9. @ConfigurationProperties(prefix = "cardevice")
    10. @Configuration
    11. public class CarConfig {
    12. //数据同步接口
    13. private String dataSyncUri;
    14. //小车任务起始点
    15. private Integer startNum;
    16. //分组总数
    17. private Integer groupNum;
    18. //小车预置点
    19. private Integer positionNum;
    20. //小车IP地址
    21. private String hostname;
    22. //小车端口
    23. private Integer port;
    24. //小车名称
    25. private String deviceName;
    26. //小车的指令集合列表
    27. private List cmdList;
    28. //小车的预置点
    29. private Map positionValue;
    30. }

    6.application.yml 配置信息

    1. # 小车 ip 端口 信息
    2. cardevice:
    3. dataSyncUri: http://127.0.0.1:8859 #数据采集同步接口配置
    4. deviceName: 智能轨道小车
    5. hostname: 192.168.8.207
    6. port: 4196
    7. startNum: 1
    8. positionNum: 12 #预置点总数
    9. groupNum: 6 #分组总数
    10. cmdList:
    11. - { "cmdDesc":"查询水平位置","cmd":"FF 01 00 EB 00 00 EC"}
    12. - { "cmdDesc":"蜂鸣","cmd":"FF 01 00 AB 00 01 AD"}
    13. - { "cmdDesc":"前进开","cmd":"FF 01 00 09 00 01 0B"}
    14. - { "cmdDesc":"前进关","cmd":"FF 01 00 0B 00 01 0D"}
    15. - { "cmdDesc":"后退开","cmd":"FF 01 00 09 00 02 0C"}
    16. - { "cmdDesc":"后退关","cmd":"FF 01 00 0B 00 02 0E"}
    17. # 预置点设置和调用(调用作用是让小车行走到指定点位置,到达位置小车会自己停下来) 没有配置的用方法实现
    18. - { "cmdDesc":"保存1","cmd":"FF 01 00 03 00 01 05"}
    19. - { "cmdDesc":"调用1","cmd":"FF 01 00 07 00 01 09"}
    20. - { "cmdDesc":"保存2","cmd":"FF 01 00 03 00 02 06"}
    21. - { "cmdDesc":"调用2","cmd":"FF 01 00 07 00 02 0A"}
    22. - { "cmdDesc":"保存3","cmd":"FF 01 00 03 00 03 07"}
    23. - { "cmdDesc":"调用3","cmd":"FF 01 00 07 00 03 0B"}
    24. - { "cmdDesc":"保存4","cmd":"FF 01 00 03 00 04 08"}
    25. - { "cmdDesc":"调用4","cmd":"FF 01 00 07 00 04 0C"}
    26. - { "cmdDesc":"保存5","cmd":"FF 01 00 03 00 05 09"}
    27. - { "cmdDesc":"调用5","cmd":"FF 01 00 07 00 05 0D"}

  • 相关阅读:
    Mysql第二篇---InnoDB数据存储结构
    微信小程序中下载xlsx文件
    (十三)强缓存和协商缓存的区别
    C#上位机——根据命令发送
    如何利用三极管实现电平转换
    硬件成本节省60%,四川华迪基于OceanBase的健康大数据数仓建设实践
    安装pandas==0.22.0时的问题
    ky10-server docker 离线安装包、离线安装
    JVM内存和垃圾回收-09.对象的实例化内存布局与访问定位
    功能测试想进阶,可以提供一点点思路和方向吗?
  • 原文地址:https://blog.csdn.net/Listest/article/details/127704846