eureka跨服务_SpringCloud微服务之跨服务调用后端接口

本文详细介绍了在SpringCloud微服务架构中,如何进行跨服务调用后端接口。内容包括:1) 从前端使用AJAX直接调用;2) 后端通过HttpURLConnection访问接口;3) 结合Eureka服务发现,使用RestTemplate进行调用。在微服务场景下,推荐使用第三种方法,因为无需维护服务IP信息,当服务地址变更时,系统能自动适应。
摘要由CSDN通过智能技术生成

SpringCloud微服务系列博客:

本文介绍如何跨服务调用后端接口,包含三种方式:

从前端使用AJAX调用后端接口

在后端使用HttpURLConnection访问接口

在后端配合Eureka发现服务,使用RestTemplate调用接口

说明:

方式1和方式2需要知道被访问的服务的ip地址

方式3需要保证调用服务的一方和被调用服务的一方都注册在同一个Eureka Server上

因此,如果是微服务体系架构,那么使用方式3是最好的选择。方式3可以不用自己去管理服务ip信息,这样在被调用服务的部署地址发生变化时,不需要修改自己的配置,而方式1和方式2都需要重新配置被调用服务的ip信息

以下均以从microapp调用microimage的服务接口为例:

1、使用AJAX调用后端接口

function loadImages() {

$.ajax({

url: "localhost:8089/images/getAll",

type: "GET",

data: {},

async: false,

timeout: 5000,

success: function (data) {

for (var i = 0; i < data.length; i++) {

var li = $('

');

li.addClass("box");

var a = $('');

a.attr("href", data[i].url);

a.addClass("magnifier");

var img = $('');

img.attr("alt", data[i].description);

img.attr("src", data[i].url);

img.attr("height", "270px");

img.attr("width", "370px");

img.appendTo(li);

img.appendTo(a);

a.appendTo(li);

li.appendTo($("#images"));

}

},

error: function (xhr, textStatus) {

}

})

}

其中url参数处要填写出被调用服务的ip:port/接口名,调用成功后接口返回值即为success函数中的data

2、使用HttpUrlConnection访问接口

3、使用Eureka发现服务并调用接口

在启动类中配置RestTemplate:

package com.deng.site;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

import org.springframework.cloud.client.loadbalancer.LoadBalanced;

import org.springframework.context.annotation.Bean;

import org.springframework.http.converter.StringHttpMessageConverter;

import org.springframework.web.client.RestTemplate;

import java.nio.charset.StandardCharsets;

@EnableDiscoveryClient

@SpringBootApplication

public class MicroAppApplication {

public static void main(String[] args) {

SpringApplication.run(MicroAppApplication.class, args);

}

@LoadBalanced

@Bean

RestTemplate restTemplate(){

RestTemplate restTemplate = new RestTemplate();

restTemplate.getMessageConverters().set(1, new StringHttpMessageConverter(StandardCharsets.UTF_8));

return restTemplate;

}

}

调用MICROIMAGE微服务接口:

package com.deng.site.service.impl;

import com.deng.site.service.ImageService;

import com.deng.site.vo.ImageVO;

import com.fasterxml.jackson.databind.JsonNode;

import com.fasterxml.jackson.databind.ObjectMapper;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.beans.factory.annotation.Value;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.PropertySource;

import org.springframework.stereotype.Service;

import org.springframework.web.client.RestTemplate;

import java.io.IOException;

import java.util.ArrayList;

import java.util.List;

@Service

@PropertySource(value = {"classpath:application.properties"}, encoding = "utf-8")

public class ImageServiceImpl implements ImageService {

@Autowired

private RestTemplate restTemplate;

@Value("${gateway.url}")

private String gateway;

@Override

public List getAllImages() {

List imageVOs = new ArrayList<>();

String url = "http://MICROIMAGE/images/getAll";

String result = restTemplate.getForObject(url, String.class);

ObjectMapper objectMapper = new ObjectMapper();

try {

JsonNode jsonRes = objectMapper.readTree(result);

for (int i = 0; i < jsonRes.size(); i++) {

JsonNode jsonNode = jsonRes.get(i);

String description = jsonNode.path("description").asText();

String imageUrl = gateway + "/microimage/images/" + jsonNode.path("fileName").asText() + "/get";

imageVOs.add(new ImageVO(description, imageUrl));

}

} catch (IOException e) {

e.printStackTrace();

}

return imageVOs;

}

}

URL的构成为:”https://”+注册在Eureka上的服务名称+调用的接口名(http://MICROIMAGE/images/getAll)

根据接口支持的http方法的不同,使用RestTemplate提供的不同方法,比如对get接口使用getForObject,post接口使用postForObject

使用ObjectMapper转换接口返回的json数据

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值