Spring Cloud、Spring Cloud LoadBalancer、Nacos 和 OpenFeign整合

版本

  1. Spring Cloud 3.1.1
  2. spring cloud loadbalancer 3.1.1
  3. nacos 2021.1
  4. open feign 3.1.1

引入依赖

在项目的 pom.xml 文件中添加相关依赖:

xml 复制代码
<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-loadbalancer</artifactId>
</dependency>

配置Nacos

在 application.yml 中配置Nacos服务发现和Feign的相关设置:

yaml 复制代码
spring:
  application:
    name: service-consumer # 当前服务名
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8888 # Nacos服务地址
    loadbalancer:
      retry: true # 启用重试机制
feign:
  client:
    config:
      default:
        connect-timeout: 5000
        read-timeout: 5000

主应用类

在主应用类中启用Feign客户端和Nacos服务发现:

java 复制代码
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;

@SpringBootApplication
@EnableFeignClients
@EnableDiscoveryClient
public class ServiceConsumerApplication {
    public static void main(String[] args) {
        SpringApplication.run(ServiceConsumerApplication.class, args);
    }
}

创建Feign客户端接口

定义一个接口,用于调用另一个服务的API:

java 复制代码
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

@FeignClient(name = "service-provider") // 这里的name是被调用服务的名称
public interface ServiceProviderClient {

    @GetMapping("/api/hello")
    String sayHello(@RequestParam(name = "name") String name);
}

使用Feign客户端

在你的控制器或服务中使用Feign客户端进行服务调用:

java 复制代码
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ConsumerController {

    @Autowired
    private ServiceProviderClient serviceProviderClient;

    @GetMapping("/hello")
    public String hello(@RequestParam(name = "name") String name) {
        return serviceProviderClient.sayHello(name);
    }
}

配置Nacos服务注册和发现

确保在被调用的服务(例如service-provider)的 application.yml 中配置了Nacos服务注册:

yaml 复制代码
spring:
  application:
    name: service-provider
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8888 # 注册中心地址

在 service-provider 项目的主应用类中也需要启用服务发现:

java 复制代码
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@SpringBootApplication
@EnableDiscoveryClient
public class ServiceProviderApplication {
    public static void main(String[] args) {
        SpringApplication.run(ServiceProviderApplication.class, args);
    }
}

并在service-provider项目中实现对应的控制器方法:

java 复制代码
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ProviderController {

    @GetMapping("/api/hello")
    public String sayHello(@RequestParam(name = "name") String name) {
        return "Hello, " + name + "!";
    }
}

启动服务

启动Nacos Server后,依次启动service-provider和service-consumer服务。访问service-consumer服务的 /hello 接口,验证服务间调用是否成功。

例如,访问 http://localhost:8090/hello?name=World 应该会调用service-provider服务的API,并返回 "Hello, World!"。

通过上述步骤,你已经成功整合了Spring Cloud、Spring Cloud LoadBalancer、Nacos和OpenFeign,实现了微服务之间的调用。

相关推荐
草履虫建模18 分钟前
Postman - API 调试与开发工具 - 标准使用流程
java·测试工具·spring·json·测试用例·postman·集成学习
火车叨位去19491 小时前
映射阿里云OSS(对象存储服务)
java·spring
笑衬人心。3 小时前
初学Spring AI 笔记
人工智能·笔记·spring
StephenCurryFans3 小时前
Spring AI vs LangChain4j:Java AI开发框架完整对比指南 🚀
后端·spring
白仑色3 小时前
Spring Cloud 微服务(统一网关设计)
spring cloud·微服务·服务治理·统一配置管理·分布式配置中心
编程小白gogogo4 小时前
Spring学习笔记
笔记·学习·spring
harmful_sheep4 小时前
Spring 为何需要三级缓存解决循环依赖,而不是二级缓存
java·spring·缓存
崎岖Qiu6 小时前
【Spring篇08】:理解自动装配,从spring.factories到.imports剖析
java·spring boot·后端·spring·面试·java-ee
述雾学java8 小时前
Spring Cloud 服务追踪实战:使用 Zipkin 构建分布式链路追踪
分布式·spring·spring cloud·zipkin
MonkeyKing_sunyuhua12 小时前
Ehcache、Caffeine、Spring Cache、Redis、J2Cache、Memcached 和 Guava Cache 的主要区别
redis·spring·memcached