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

 

目录

RestTemplate

OpenFeign

1.引入依赖open-feign

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

3.注入FeignClient启用

4验证



RestTemplate

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

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

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

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

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:

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
        <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

        <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>

  • 7
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Elaine202391

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值