大街款商城项目03-微服务之间调用

目录

RestTemplate

OpenFeign

1.引入依赖open-feign

2.声明要调用的服务和接口

3.注入FeignClient启用

4验证


RestTemplate

在微服务架构中,使用RestTemplate是一种常见的方式进行服务间的HTTP通信。以下是一个简单的示例,演示如何使用RestTemplate进行微服务之间的RESTful调用。

首先,确保你的项目中引入了Spring Boot和RestTemplate的依赖。在pom.xml中添加如下依赖:

java 复制代码
<dependencies>
    <!-- Spring Boot Starter Web包含了RestTemplate -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

接下来,创建一个使用RestTemplate进行RESTful调用的服务类。以下是一个简单的示例,其中假设你有两个微服务,分别是ServiceAServiceB,它们之间通过RESTful调用:

java 复制代码
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
public class MicroserviceClient {

    private final RestTemplate restTemplate;

    @Autowired
    public MicroserviceClient(RestTemplate restTemplate) {
        this.restTemplate = restTemplate;
    }

    public String callServiceA() {
        String serviceAUrl = "http://service-a/api/data";
        ResponseEntity<String> response = restTemplate.getForEntity(serviceAUrl, String.class);
        return response.getBody();
    }

    public String callServiceB() {
        String serviceBUrl = "http://service-b/api/data";
        ResponseEntity<String> response = restTemplate.getForEntity(serviceBUrl, String.class);
        return response.getBody();
    }
}

最后,确保在你的Spring Boot应用程序主类(通常是带有@SpringBootApplication注解的类)中创建一个RestTemplate的Bean:

java 复制代码
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

@Configuration
public class AppConfig {

    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }
}

以上示例是一个简单的演示,实际中你可能需要更多的配置和异常处理。此外,现代的微服务架构中,通常会使用更先进的工具如Feign或者WebClient来简化和优化微服务之间的通信。

OpenFeign

OpenFeign是一个声明式的Web服务客户端,使得编写HTTP客户端变得更加简单。通过使用OpenFeign,你可以更轻松地声明和实现服务间的RESTful调用,而不必显式地创建RestTemplate实例。

还是使用项目进行说明,mall-product想要调用mall-order的获取订单列表接口。

1.引入依赖open-feign
XML 复制代码
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
2.声明要调用的服务和接口

在mall-product中声明OrderFeignService的order接口

3.注入FeignClient启用
4验证

调用http://localhost:10010/product/brand/order,返回order列表信息

如果运行报错:

No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalanc?

pom中需要添加spring-cloud-loadbalancer依赖,排除冲突的spring-cloud-starter-netflix-ribbon

XML 复制代码
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.cloud</groupId>
                    <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-loadbalancer</artifactId>
            <version>2.2.2.RELEASE</version>
        </dependency>
相关推荐
一个有温度的技术博主4 小时前
Redis系列八:Jedis连接池在java中的使用
java·redis·bootstrap
cyforkk4 小时前
Java 并发编程教科书级范例:深入解析 computeIfAbsent 与方法引用
java·开发语言
后青春期的诗go4 小时前
泛微OA-E9与第三方系统集成开发企业级实战记录(八)
java·接口·金蝶·泛微·oa·集成开发·对接
dreamxian5 小时前
苍穹外卖day09
java·spring boot·tomcat·log4j·maven
剑海风云5 小时前
JDK 26之安全增强
java·开发语言·安全·jdk26
左左右右左右摇晃5 小时前
Java并发——多线程
java·开发语言·jvm
23.5 小时前
【Java】字符串底层与常量池演变全解析
java·开发语言·jvm
极客先躯5 小时前
高级java每日一道面试题-2025年9月09日-数据处理篇[LangChain4j]-金融行业使用 LLM 有哪些合规要求?
java·金融·高级面试题·权限与访问控制·数据脱敏与隐私计算·模型可解释性工具·审计日志与监控
jing-ya5 小时前
day 59 图论part10
java·开发语言·数据结构·算法·图论
掘根5 小时前
【微服务即时通讯】用户管理子服务1
微服务·云原生·架构