使用OpenFeign+Eureka实现HTTP调用的简单示例

  1. 由于RPC调用需要使用注册中心,所以首先需要创建eureka服务,创建SpringBoot项目后导入spring-cloud-starter-netflix-eureka-server,注意SpringBoot和SpringCloud版本一致性,然后进行配置,启动类添加注解@EnableEurekaServer
xml 复制代码
<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
yaml 复制代码
server:
  port: 8761

spring:
  application:
    name: eureka

eureka:
  client:
    register-with-eureka: false # 不向Eureka注册自己
    fetch-registry: false       # 不从Eureka获取注册信息
  service-url:
    defaultZone: http://localhost:8761/eureka/
java 复制代码
@SpringBootApplication
@EnableEurekaServer
public class EurekaApplication {

	public static void main(String[] args) {
		SpringApplication.run(EurekaApplication.class, args);
	}

}
  1. 创建服务提供者SpringBoot应用,关键是导入spring-cloud-starter-netflix-eureka-client依赖,进行eureka的配置,编写controller接入层代码,启动类不需要加@EnableEurekaClient注解,因为在新版本中已经被移除
xml 复制代码
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
yaml 复制代码
server:
  port: 8082

spring:
  application:
    name: producer

eureka:
  client:
    register-with-eureka: true
    fetch-registry: true
  service-url:
    defaultZone: http://localhost:8761/eureka/
java 复制代码
@RestController
@RequestMapping("/api")
public class DataController {

    @GetMapping("/data")
    public String getData() {

        System.out.println("服务提供者被调用");
        return "Hello from Producer!";
    }
}
  1. 创建服务消费者SpringBoot应用,引入如下三个关键依赖,进行eureka配置,编写接口,编写接入层代码调用该接口,启动类需要加上@EnableFeignClients
xml 复制代码
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-web</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-netflix-eureka-client</artifactId>
</dependency>
yaml 复制代码
server:
  port: 8083

spring:
  application:
    name: consumer

eureka:
  client:
    register-with-eureka: true
    fetch-registry: true
  service-url:
    defaultZone: http://localhost:8761/eureka/
java 复制代码
@FeignClient(name = "producer") // 此处不需要写url = "http://localhost:8082",因为通过注册中心调用
public interface ProducerClient {

    @GetMapping("/api/data")
    String getData();
}
java 复制代码
@RestController
@RequestMapping("/consumer")
public class ConsumerController {

    @Autowired
    private ProducerClient producerClient;

    @GetMapping("/data")
    public String getDataFromProducer() {
        return producerClient.getData();
    }
}
java 复制代码
@SpringBootApplication
@EnableFeignClients
public class ConsumerApplication {

	public static void main(String[] args) {
		SpringApplication.run(ConsumerApplication.class, args);
	}

}
  1. 总结
  • 通过引入eureka注册中心后,如果服务提供者有多个节点,那么请求就会被发送到存活的节点上,实现了动态路由,避免因为固定写url但是该节点宕机导致调用失败的问题
相关推荐
凯凯爱前端20 分钟前
通俗易懂的 TLS 协商过程
http
道友老李2 小时前
【微服务架构】SpringSecurity核心源码剖析+jwt+OAuth(三):SpringSecurity请求流转的本质
微服务·云原生·架构
Sirius Wu3 小时前
Service Mesh 深度解析与 Istio+Envoy 实现方案
云原生·istio·service_mesh
无名之逆4 小时前
[特殊字符] 超轻高性能的 Rust HTTP 服务器 —— Hyperlane [特殊字符][特殊字符]
java·服务器·开发语言·前端·网络·http·rust
穷儒公羊4 小时前
第一部分——Docker篇 第三章 构建自定义镜像
java·运维·后端·学习·docker·云原生·容器
forestsea5 小时前
Docker 是什么? Docker 基本观念介绍与容器和虚拟机的比较
docker·云原生
人不走空7 小时前
Kubernetes核心架构:从组件协同到工作原理
云原生·架构·k8s
堕落年代8 小时前
HTTP请求当中若用户还没接收到返回数据就离线但服务器资源已经删除的情况
网络·网络协议·http
言小乔.17 小时前
202520 | 微服务
微服务·云原生·架构
霸道流氓气质18 小时前
Winform入门进阶企业级开发示例:http接口数据清洗转换、断线续传、mqtt数据传输实例详解(附代码资源下载)
http·c#·winform