大街款商城项目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 小时前
Zookeeper实现分布式锁
java·分布式·后端·zookeeper·wpf
MarcoPage5 小时前
Python 字典推导式入门:一行构建键值对映射
java·linux·python
脸大是真的好~5 小时前
黑马JAVAWeb-11 请求参数为数组-XML自动封装-XML手动封装-增删改查-全局异常处理-单独异常分别处理
java
Hello.Reader8 小时前
Data Sink定义、参数与可落地示例
java·前端·网络
2401_837088509 小时前
stringRedisTemplate.opsForHash().entries
java·redis
lkbhua莱克瓦2411 小时前
Java基础——集合进阶3
java·开发语言·笔记
蓝-萧11 小时前
使用Docker构建Node.js应用的详细指南
java·后端
多喝开水少熬夜11 小时前
Trie树相关算法题java实现
java·开发语言·算法
lkbhua莱克瓦2412 小时前
Java基础——集合进阶用到的数据结构知识点1
java·数据结构·笔记·github
音符犹如代码12 小时前
Java并发List实战:CopyOnWriteArrayList原理与ArrayList常见面试题
java·开发语言·面试·list