大街款商城项目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>
相关推荐
米丘9 分钟前
微前端之 Web Components 完全指南
微服务·html
行者全栈架构师21 分钟前
IDEA 中 Maven 项目的 15 个红色报错快速解决方法
java·后端
令人头秃的代码0_023 分钟前
mac(m5)平台编译openjdk
java
唐青枫1 天前
Java JDBC 实战指南:从 Connection 到事务和连接池
java
一个做软件开发的牛马1 天前
MyBatis-Plus 从零实战:完整搭建可运行 Demo,BaseMapper 零 SQL、Wrapper 条件构造、分页插件与代码生成器详解
java·后端
用户3721574261351 天前
Java 处理 PDF 图片:提取 PDF 中的图片,并压缩 PDF 图片体积
java
用户3721574261351 天前
Java 打印 Word 文档:从基础打印到高级设置
java
用户3521802454752 天前
当 Prompt 学会"热更新":Spring Boot × Nacos3 AI 实战
java·spring boot·ai编程
东坡白菜2 天前
破局全栈:一个前端开发的Java入门实战记录(1)
java·全栈
唐青枫2 天前
Java Tomcat 实战指南:从 Servlet 容器到 Spring Boot 部署
java