本篇文章中需要三个角色,分别是服务的提供者,服务的消费者,还有一个是上一篇文章的主角——注册中心Eureka(使用单机版本即可,本篇的示例也会使用单机版本的Eureka)。
整体流程为:(了解源码可+求求: 1791743380)
- 先启动注册中心Eureka
- 启动服务的提供者将提供服务,并将服务注册到注册中心Eureka上
- 启动服务的消费者,在注册中心中找到服务并完成消费
1. 服务提供者
1. pom.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0</modelVersion>
- <parent>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-parent</artifactId>
- <version>2.1.6.RELEASE</version>
- <relativePath/> <!-- lookup parent from repository -->
- </parent>
- <groupId>com.springcloud</groupId>
- <artifactId>producer</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- <name>producer</name>
- <description>Demo project for Spring Boot</description>
- <properties>
- <java.version>1.8</java.version>
- <spring-cloud.version>Greenwich.SR1</spring-cloud.version>
- </properties>
- <dependencies>
- <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>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-test</artifactId>
- <scope>test</scope>
- </dependency>
- </dependencies>
- <dependencyManagement>
- <dependencies>
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-dependencies</artifactId>
- <version>${spring-cloud.version}</version>
- <type>pom</type>
- <scope>import</scope>
- </dependency>
- </dependencies>
- </dependencyManagement>
- <build>
- <plugins>
- <plugin>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-maven-plugin</artifactId>
- </plugin>
- </plugins>
- </build>
- </project>
2. 配置文件application.yml
- server:
- port: 8080
- spring:
- application:
- name: spring-cloud-producer
- eureka:
- client:
- service-url:
- defaultZone: http://localhost:8761/eureka/
3. 启动类ProducerApplication.java
增加@EnableEurekaClient,如果是其他注册中心可以使用注解@EnableDiscoveryClient来进行服务的注册
- package com.springcloud.producer;
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
- import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
- @SpringBootApplication
- @EnableEurekaClient
- public class ProducerApplication {
- public static void main(String[] args) {
- SpringApplication.run(ProducerApplication.class, args);
- }
- }
4. Controller
- package com.springcloud.producer.controller;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RequestParam;
- import org.springframework.web.bind.annotation.RestController;
- /**
- * Created with IntelliJ IDEA.
- *
- * @Date: 2019/7/2
- * @Time: 0:02
- * @email: inwsy@hotmail.com
- * Description:
- */
- @RestController
- public class HelloController {
- @RequestMapping("/hello")
- public String hello(@RequestParam String name) {
- return "hello "+name+",producer is ready";
- }
- }
先在可以先启动上一篇当中单机版的Eureka,再启动我们刚写好的producer服务提供者,启动成功后,访问链接http://localhost:8761/,可以看到我们的的服务提供者producer已经成功注册在注册中心上了。
至此,服务的提供者已经配置完成。
2. 服务消费者
1. pom.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0</modelVersion>
- <parent>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-parent</artifactId>
- <version>2.1.6.RELEASE</version>
- <relativePath/> <!-- lookup parent from repository -->
- </parent>
- <groupId>com.springcloud</groupId>
- <artifactId>consumers</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- <name>consumers</name>
- <description>Demo project for Spring Boot</description>
- <properties>
- <java.version>1.8</java.version>
- <spring-cloud.version>Greenwich.SR1</spring-cloud.version>
- </properties>
- <dependencies>
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
- </dependency>
- <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.boot</groupId>
- <artifactId>spring-boot-starter-test</artifactId>
- <scope>test</scope>
- </dependency>
- </dependencies>
- <dependencyManagement>
- <dependencies>
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-dependencies</artifactId>
- <version>${spring-cloud.version}</version>
- <type>pom</type>
- <scope>import</scope>
- </dependency>
- </dependencies>
- </dependencyManagement>
- <build>
- <plugins>
- <plugin>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-maven-plugin</artifactId>
- </plugin>
- </plugins>
- </build>
- </project>
spring-boot-starter-web: 这个包是通用的web开发包,里面包含了spring-web、spring-webmvc等包
spring-cloud-starter-openfeign: 这个包是springcloud对于Feign的封装,Feign是一个声明式的Web服务客户端。它支持Feign本身的注解、JAX-RS注解以及SpringMVC的注解。Spring Cloud集成Ribbon和Eureka以在使用Feign时提供负载均衡的http客户端。
2. 配置文件application.yml
- server:
- port: 8081
- spring:
- application:
- name: spring-cloud-consumers
- eureka:
- client:
- service-url:
- defaultZone: http://localhost:8761/eureka/
3. 启动类ConsumersApplication.java
同上,增加@EnableEurekaClient,如果是其他注册中心可以使用注解@EnableDiscoveryClient来进行服务的注测
- package com.springcloud.consumers;
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
- import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
- import org.springframework.cloud.openfeign.EnableFeignClients;
- @SpringBootApplication
- @EnableEurekaClient
- @EnableFeignClients
- public class ConsumersApplication {
- public static void main(String[] args) {
- SpringApplication.run(ConsumersApplication.class, args);
- }
- }
@EnableFeignClients: 这个注解是通知SpringBoot在启动的时候,扫描被 @FeignClient 修饰的类,@FeignClient这个注解在进行远程调用的时候会用到。
4. Feign远程调用
Feign是一个声明式Web Service客户端。使用Feign能让编写Web Service客户端更加简单, 它的使用方法是定义一个接口,然后在上面添加注解,同时也支持JAX-RS标准的注解。Feign也支持可拔插式的编码器和解码器。Spring Cloud对Feign进行了封装,使其支持了Spring MVC标准注解和HttpMessageConverters。Feign可以与Eureka和Ribbon组合使用以支持负载均衡。
创建一个remote接口
- package com.springcloud.consumers.remote;
- import org.springframework.cloud.openfeign.FeignClient;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RequestParam;
- /**
- * @Author: shiyao.wei
- * @Date: 2019/7/2 11:14
- * @Version: 1.0
- * @Desc:
- */
- @FeignClient(name= "spring-cloud-producer")
- public interface HelloRemote {
- @RequestMapping(value = "/hello")
- String hello(@RequestParam(value = "name") String name);
- }
- name:远程服务名,及spring.application.name配置的名称
- 此类中的方法和远程服务中contoller中的方法名和参数需保持一致
5. web层调用远程接口 Controller
- package com.springcloud.consumers.controller;
- import com.springcloud.consumers.remote.HelloRemote;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.web.bind.annotation.PathVariable;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RestController;
- /**
- * @Author: shiyao.wei
- * @Date: 2019/7/2 11:25
- * @Version: 1.0
- * @Desc:
- */
- @RestController
- public class HelloController {
- @Autowired
- HelloRemote helloRemote;
- @RequestMapping("/hello/{name}")
- public String index(@PathVariable("name") String name) {
- return helloRemote.hello(name);
- }
- }
现在,一个最简单的服务注册和调用的例子就完成了。
3. 测试
简单调用
顺次启动eureka、producer、consumer三个项目
启动成功后,先在浏览器输入http://localhost:8080/hello?name=springcloud
可以看到页面显示:hello springcloud,producer is ready
证明我们的producer已经正常启动,提供的服务也正常
接下来,我们测试服务消费者,在浏览器中输入:http://localhost:8081/hello/spring
可以看到页面显示:hello spring,producer is ready
说明客户端已经成功的通过feign调用了远程服务hello,并且将结果返回到了浏览器。
负载均衡
将上面的producer复制一份,修改名称为producer2,修改pom.xml中的\\为producer2,修改其中的Controller:
- package com.springcloud.producer.controller;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RequestParam;
- import org.springframework.web.bind.annotation.RestController;
- /**
- * Created with IntelliJ IDEA.
- *
- * @Date: 2019/7/2
- * @Time: 0:02
- * @email: inwsy@hotmail.com
- * Description:
- */
- @RestController
- public class HelloController {
- @RequestMapping("/hello")
- public String hello(@RequestParam String name) {
- return "hello "+name+",producer2 is ready";
- }
- }
修改application.yml配置文件启动端口为8082
启动我们刚复制好的producer2,这时可以看一下注册中心Eureka,我们现在已经有两个producer服务了。
这时我们再去访问:http://localhost:8081/hello/spring
第一次返回结果:hello spring,producer is ready
第二次返回结果:hello spring,producer2 is ready
连续刷新页面,两个结果会交替出现,说明注册中心提供了服务负载均衡功能。将服务数提高到N个,会发现测试结果一样,请求会自动轮询到每个服务端来处理。
相关推荐
在微服务架构中,Spring Cloud Alibaba 是一个强大的工具集,它提供了多种服务治理功能,如服务注册与发现、配置中心、熔断器等。本文将深入探讨如何在 Spring Cloud Alibaba 环境下,整合 Feign 实现服务间的 ...
Feign的设计灵感来源于Netflix的Feign库,它的主要目的是简化微服务之间的通信,使得开发者可以像定义接口一样调用远程服务,从而降低了服务消费者与提供者的耦合度。 在传统的Web Service(WebService)中,我们...
Spring Cloud Feign 是一个基于 annotations 的声明式 RESTful 客户端,它提供了对服务间调用的一种简单而高效的解决方案。在本文中,我们将基于 Spring Cloud Feign来实现服务间的相互调用。 首先,让我们来创建一...
Spring Cloud Feign 是一个基于 Java 的声明式 RESTful 客户端,提供了一种简单、可靠的方式来调用远程服务。在本文中,我们将介绍如何使用 Spring Cloud Feign 实现远程调用服务传输文件的方法。 Feign 介绍 ...
在给定的标题和描述中,我们看到了几个关键组件:Eureka、Zuul、Ribbon、Hystrix 和 Feign,这些都是Spring Cloud生态中的重要组成部分。下面将详细阐述这些组件及其在实际应用中的作用。 1. **Eureka**:它是...
在SpringCloud框架中,Feign是一个声明式的Web服务客户端,它使得编写Web服务客户端变得简单。Feign的设计目标是让微服务之间的调用更加优雅,它通过注解和接口的方式,使得客户端调用服务就像调用本地方法一样简单...
通过以上步骤和代码示例,可以清晰地看到Feign在Spring Cloud微服务架构中扮演的角色,以及如何实现远程服务调用和负载均衡。Feign的使用极大地简化了HTTP客户端的编程模型,使得开发者能够更加聚焦于业务逻辑的实现...
Spring Cloud Feign是一个基于Netflix的Feign组件,提供了一个简洁的方式来构建RESTful风格的微服务接口。Feign组件提供了一个统一的接口调用方式,使得微服务之间的调用变得更加简洁和高效。在微服务架构中, token...
在本项目"spring-cloud-user-feign.zip"中,我们探讨的是如何在Spring Cloud框架下实现服务间的调用,特别是利用Feign客户端进行微服务通信。这个压缩包包含了四个关键组件,分别是: 1. spring-cloud-gateways:这...
总结来说,这个项目是 SpringCloud 微服务架构的一个基本实践,包含了服务注册与发现、服务调用、客户端负载均衡、熔断保护以及 API 网关等功能。对于初学者,这是一个很好的起点,可以深入理解 SpringCloud 如何...
Feign是Spring Cloud提供的一种声明式Web服务客户端,它使得构建与使用Web服务客户端变得更加简单。 首先,让我们了解Spring Cloud的核心概念。Spring Cloud是一个为开发者提供快速构建分布式系统工具的框架,它...
在本教程中,我们将深入探讨如何使用Spring Cloud的2021.0.3版本,集成Nacos作为服务注册中心,并实现Feign客户端调用。Spring Cloud是一个强大的框架,用于构建分布式系统,如微服务架构,而Nacos是阿里巴巴开源的...
在本篇学习笔记中,我们将深入探讨如何在Spring Cloud框架下构建一个Feign客户端,用于在微服务架构中实现服务间的远程调用。Feign是一个声明式的Web服务客户端,它使得编写HTTP客户端变得简单,而Spring Cloud对...
这个项目下的 "spring-cloud-study.zip" 文件,看起来是一个学习资源,涵盖了 Spring Cloud 的几个关键组件的实战教程,包括服务注册与发现、Feign 客户端和服务间调用以及 Hystrix 断路器的使用。 首先,让我们...
- **Eureka 整合**:结合 Spring Cloud Eureka,Feign 可以从服务注册中心发现并调用服务,实现微服务间的无感知通信。 2. **Feign 的工作原理** - **接口定义**:在 Spring Cloud Feign 中,你需要创建一个接口...
综上所述,这个项目利用SpringCloud的组件构建了一个具备服务发现、动态路由、负载均衡的微服务系统,通过Eureka管理服务注册,Zuul处理外部请求的路由,Feign简化了服务间调用,共同构建出高效、可扩展的微服务架构...
6. 服务提供者:在服务提供者的启动类上添加`@EnableFeignClients`注解,表示该服务可以被其他服务通过Feign调用。同时,服务提供者也需要注册到Nacos集群,以便服务消费者发现。 在实际开发中,我们还可以利用...
Spring Cloud作为Java领域的主流微服务框架,为企业级分布式应用提供了全面的解决方案,包括服务发现、配置中心、负载均衡、熔断机制等核心功能。在7天的学习过程中,你将逐步深入理解并熟练运用Spring Cloud的各项...
RS注解,SpringCloud又为Feign增加了对SpringMVC注解的支持,同时为了能够使用和Spring Web中默认使用的相同的httpMessageConverter,SpringCloud集成了Ribbon和Eureka,用来在使用Feign时能够为其提供一个负载均衡...
标题 "springcloud2-hystrix-feign-zuul.zip" 提示了我们这是一组关于Spring Cloud 2的实现,具体涉及Hystrix、Feign和Zuul组件的实践项目。Spring Cloud 是一个用于构建分布式系统的服务发现、配置管理和微服务连接...