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 文件中添加相关依赖:

<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的相关设置:

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服务发现:

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:

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客户端进行服务调用:

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服务注册:

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

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

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项目中实现对应的控制器方法:

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,实现了微服务之间的调用。

相关推荐

  1. SpringCloudAlibaba-整合openfeignloadbalence(三)

    2024-06-08 10:08:03       15 阅读
  2. Sentinel整合OpenFeign

    2024-06-08 10:08:03       33 阅读
  3. openfeign整合sentinel进行降级

    2024-06-08 10:08:03       14 阅读
  4. nacosopenFeign

    2024-06-08 10:08:03       20 阅读
  5. Feign OpenFeign 的区别???

    2024-06-08 10:08:03       13 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-06-08 10:08:03       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-06-08 10:08:03       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-06-08 10:08:03       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-06-08 10:08:03       18 阅读

热门阅读

  1. ubantu安装第三库到指定目录

    2024-06-08 10:08:03       9 阅读
  2. C#面:AJAX的底层实现原理

    2024-06-08 10:08:03       9 阅读
  3. 类的定义和对象的引用

    2024-06-08 10:08:03       8 阅读
  4. c++ 左右值与引用折叠

    2024-06-08 10:08:03       9 阅读
  5. 【例0808】create daxis using face 使用面创建基准轴

    2024-06-08 10:08:03       8 阅读
  6. 【Linux】GNU编译器基础

    2024-06-08 10:08:03       6 阅读
  7. 微信小程序按钮设计与交互:打造极致用户体验

    2024-06-08 10:08:03       7 阅读
  8. cocos入门6:向量简介

    2024-06-08 10:08:03       8 阅读
  9. 【学习笔记】Linux前置准备

    2024-06-08 10:08:03       8 阅读
  10. Ruby语言与Python:深度比较与独特魅力探索

    2024-06-08 10:08:03       8 阅读