• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

使用SpringBoot跨系统调用接口的方案

springboot 搞代码 4年前 (2022-01-09) 15次浏览 已收录 0个评论
文章目录[隐藏]

一、简介

项目开发中存在系统之间互调问题,又不想用dubbo,这里提供几种springboot方案:

1、使用Feign进行消费(推荐)

2、使用原始httpClient请求

3、使用RestTemplate方法

二、方案

方案一:使用Feign进行消费(推荐)

1、在maven中添加依赖

<dependency>
 <groupId>org.springframework.cloud</groupId>
 <artifactId>spring-cloud-starter-openfeign</artifactId>
 <version>2.2.2</version>
</dependency>

2、启动类上加上@EnableFeignClients

@EnableHystrix
@EnableDiscoveryClient
@EnableFeignClients(basePackages = {"com.aaa.aurora"})
@SpringBootApplication
@EnableTransactionManagement
@ComponentScan(basePackages = "com.aaa.aurora")
@ImportResource(locations= {"classpath:spring.xml","spring-security.xml"})
@MapperScan("com.aaa.aurora.mapper")
public class AuroraWebApplication {
 public static void main(String[] args) {
   SpringApplication.run(AuroraWebApplication.class, args);
  }
}

3、编写service接口

@FeignClient(url = "${pang<em style="color:transparent">本文来源gao.dai.ma.com搞@代*码#网</em>u.url}",name = "panguUrl")
public interface PanGuService {
 @RequestMapping(value = "/pangu/restful/check",method = RequestMethod.POST)
 JSONObject check(@RequestParam(name="queryEngine") String queryEngine, @RequestParam(name="querySql") String querySql, @RequestParam(name="jobNo") String jobNo);
}

其中:pangu.url是配置在application.properties中的ip及端口

pangu.url = 192.168.1.3:8080
/pangu/restful/check是要调的接口名

4、代码中调用

 @Autowired
 private PanGuService panGuService;
 
 JSONObject jsonObject = null;
 try {
   jsonObject = panGuService.auroraPriviledge(PRESTO_DRIVER, query.get("sql"), user.getWorkNo());
 } catch (Exception e) {
  throw new Exception("请求系统异常");
 }
 if (PANGU_FAIL.equals(jsonObject.get("code"))) {
  LOG.info(jsonObject.get("msg").toString());
  throw new BusinessException(jsonObject.get("msg").toString());
 }

方案二:使用原始httpClient请求

使用HttpClient发送请求、接收响应很简单,一般需要如下几步即可。

1. 创建HttpClient对象。

2. 创建请求方法的实例,并指定请求URL。如果需要发送GET请求,创建HttpGet对象;如果需要发送POST请求,创建HttpPost对象。

3. 如果需要发送请求参数,可调用HttpGet、HttpPost共同的setParams(HttpParams params)方法来添加请求参数;对于HttpPost对象而言,也可调用setEntity(HttpEntity entity)方法来设置请求参数。

4. 调用HttpClient对象的execute(HttpUriRequest request)发送请求,该方法返回一个HttpResponse。

5. 调用HttpResponse的getAllHeaders()、getHeaders(String name)等方法可获取服务器的响应头;调用HttpResponse的getEntity()方法可获取HttpEntity对象,该对象包装了服务器的响应内容。程序可通过该对象获取服务器的响应内容。

6. 释放连接。无论执行方法是否成功,都必须释放连接。

public JSONObject doPost(String queryEngine, String querySql, String jobNo) {
  JSONObject jsonObject = null;
  //1.创建httpClient对象
  CloseableHttpClient client = HttpClients.createDefault();
  //2.创建请求方法的实例,并指定请求URL
  String url = "http://192.168.1.11:8080";
  HttpPost post = new HttpPost(url);
  post.setHeader("Content-Type", "application/json;charset=utf8");
  //3.参数
  AuroraPriviledge auroraPriviledge = new AuroraPriviledge();
  auroraPriviledge.setQueryEngine(queryEngine);
  auroraPriviledge.setQuerySql(querySql);
  auroraPriviledge.setJobNo(jobNo);
  String jsonString = JSON.toJSONString(auroraPriviledge);
  StringEntity entity = new StringEntity(jsonString, "UTF-8");
  post.setEntity(entity);
  //4.调用execute,返回response
  CloseableHttpResponse response = null;
  try {
   response = client.execute(post);
   HttpEntity responseEntity = response.getEntity();
  } catch (IOException e) {
   e.printStackTrace();
  } catch (Exception e) {
   e.printStackTrace();
  } finally {
   try {
    if (client != null) {
     client.close();
    }
    if (response != null) {
     response.close();
    }
   } catch (IOException e) {
    e.printStackTrace();
   }
  }
  return jsonObject;
 }

方案三:使用RestTemplate方法


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:使用SpringBoot跨系统调用接口的方案

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址