大街款商城项目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>
相关推荐
没有bug.的程序员1 小时前
分布式架构未来趋势:从云原生到智能边缘的演进之路
java·分布式·微服务·云原生·架构·分布式系统
毕业设计制作和分享3 小时前
springboot150基于springboot的贸易行业crm系统
java·vue.js·spring boot·后端·毕业设计·mybatis
小梁努力敲代码8 小时前
java数据结构--List的介绍
java·开发语言·数据结构
摸鱼的老谭8 小时前
构建Agent该选Python还是Java ?
java·python·agent
lang201509289 小时前
Spring Boot 官方文档精解:构建与依赖管理
java·spring boot·后端
夫唯不争,故无尤也9 小时前
Tomcat 启动后只显示 index.jsp,没有进入你的 Servlet 逻辑
java·servlet·tomcat
zz-zjx9 小时前
Tomcat核心组件全解析
java·tomcat
Deschen9 小时前
设计模式-外观模式
java·设计模式·外观模式
007php0079 小时前
百度面试题解析:微服务架构、Dubbo、Redis及其一致性问题(一)
redis·百度·docker·微服务·容器·职场和发展·架构
why技术10 小时前
从18w到1600w播放量,我的一点思考。
java·前端·后端