首页 > 编程语言 > 详细

SpringCloud,服务的提供与Feign调用流程

时间:2020-06-15 18:50:45      阅读:52      评论:0      收藏:0      [点我收藏+]

本篇文章中需要三个角色,分别是服务的提供者,服务的消费者,还有一个是注册中心Eureka(使用单机版本即可,本篇的示例也会使用单机版本的Eureka)。

整体流程为:

  1. 先启动注册中心Eureka
  2. 启动服务的提供者将提供服务,并将服务注册到注册中心Eureka上
  3. 启动服务的消费者,在注册中心中找到服务并完成消费

1. 服务提供者

1. pom.xm

Java代码  技术分享图片
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  3.     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  
  4.     <modelVersion>4.0.0</modelVersion>  
  5.     <parent>  
  6.         <groupId>org.springframework.boot</groupId>  
  7.         <artifactId>spring-boot-starter-parent</artifactId>  
  8.         <version>2.1.6.RELEASE</version>  
  9.         <relativePath/> <!-- lookup parent from repository -->  
  10.     </parent>  
  11.     <groupId>com.springcloud</groupId>  
  12.     <artifactId>producer</artifactId>  
  13.     <version>0.0.1-SNAPSHOT</version>  
  14.     <name>producer</name>  
  15.     <description>Demo project for Spring Boot</description>  
  16.   
  17.     <properties>  
  18.         <java.version>1.8</java.version>  
  19.         <spring-cloud.version>Greenwich.SR1</spring-cloud.version>  
  20.     </properties>  
  21.   
  22.     <dependencies>  
  23.         <dependency>  
  24.             <groupId>org.springframework.boot</groupId>  
  25.             <artifactId>spring-boot-starter-web</artifactId>  
  26.         </dependency>  
  27.         <dependency>  
  28.             <groupId>org.springframework.cloud</groupId>  
  29.             <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>  
  30.         </dependency>  
  31.   
  32.         <dependency>  
  33.             <groupId>org.springframework.boot</groupId>  
  34.             <artifactId>spring-boot-starter-test</artifactId>  
  35.             <scope>test</scope>  
  36.         </dependency>  
  37.     </dependencies>  
  38.   
  39.     <dependencyManagement>  
  40.         <dependencies>  
  41.             <dependency>  
  42.                 <groupId>org.springframework.cloud</groupId>  
  43.                 <artifactId>spring-cloud-dependencies</artifactId>  
  44.                 <version>${spring-cloud.version}</version>  
  45.                 <type>pom</type>  
  46.                 <scope>import</scope>  
  47.             </dependency>  
  48.         </dependencies>  
  49.     </dependencyManagement>  
  50.   
  51.     <build>  
  52.         <plugins>  
  53.             <plugin>  
  54.                 <groupId>org.springframework.boot</groupId>  
  55.                 <artifactId>spring-boot-maven-plugin</artifactId>  
  56.             </plugin>  
  57.         </plugins>  
  58.     </build>  
  59.   
  60. </project>  

 

2. 配置文件application.yml

Java代码  技术分享图片
  1. server:  
  2.   port: 8080  
  3. spring:  
  4.   application:  
  5.     name: spring-cloud-producer  
  6. eureka:  
  7.   client:  
  8.     service-url:  
  9.       defaultZone: http://localhost:8761/eureka/  

 

3. 启动类ProducerApplication.java

增加@EnableEurekaClient,如果是其他注册中心可以使用注解@EnableDiscoveryClient来进行服务的注册

Java代码  技术分享图片
  1. package com.springcloud.producer;  
  2.   
  3. import org.springframework.boot.SpringApplication;  
  4. import org.springframework.boot.autoconfigure.SpringBootApplication;  
  5. import org.springframework.cloud.netflix.eureka.EnableEurekaClient;  
  6.   
  7. @SpringBootApplication  
  8. @EnableEurekaClient  
  9. public class ProducerApplication {  
  10.   
  11.     public static void main(String[] args) {  
  12.         SpringApplication.run(ProducerApplication.class, args);  
  13.     }  
  14.   
  15. }  

 

4. Controller

Java代码  技术分享图片
  1. package com.springcloud.producer.controller;  
  2.   
  3. import org.springframework.web.bind.annotation.RequestMapping;  
  4. import org.springframework.web.bind.annotation.RequestParam;  
  5. import org.springframework.web.bind.annotation.RestController;  
  6.   
  7. /** 
  8.  * Created with IntelliJ IDEA. 
  9.  * 
  10.  * @Date: 2019/7/2 
  11.  * @Time: 0:02 
  12.  * @email: inwsy@hotmail.com 
  13.  * Description: 
  14.  */  
  15. @RestController  
  16. public class HelloController {  
  17.     @RequestMapping("/hello")  
  18.     public String hello(@RequestParam String name) {  
  19.         return "hello "+name+",producer is ready";  
  20.     }  
  21. }  

 

先在可以先启动上一篇当中单机版的Eureka,再启动我们刚写好的producer服务提供者,启动成功后,访问链接http://localhost:8761/,可以看到我们的的服务提供者producer已经成功注册在注册中心上了。

 

技术分享图片
 

 

至此,服务的提供者已经配置完成。

2. 服务消费者

1. pom.xml

Java代码  技术分享图片
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  3.     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  
  4.     <modelVersion>4.0.0</modelVersion>  
  5.     <parent>  
  6.         <groupId>org.springframework.boot</groupId>  
  7.         <artifactId>spring-boot-starter-parent</artifactId>  
  8.         <version>2.1.6.RELEASE</version>  
  9.         <relativePath/> <!-- lookup parent from repository -->  
  10.     </parent>  
  11.     <groupId>com.springcloud</groupId>  
  12.     <artifactId>consumers</artifactId>  
  13.     <version>0.0.1-SNAPSHOT</version>  
  14.     <name>consumers</name>  
  15.     <description>Demo project for Spring Boot</description>  
  16.   
  17.     <properties>  
  18.         <java.version>1.8</java.version>  
  19.         <spring-cloud.version>Greenwich.SR1</spring-cloud.version>  
  20.     </properties>  
  21.   
  22.     <dependencies>  
  23.         <dependency>  
  24.             <groupId>org.springframework.cloud</groupId>  
  25.             <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>  
  26.         </dependency>  
  27.         <dependency>  
  28.             <groupId>org.springframework.boot</groupId>  
  29.             <artifactId>spring-boot-starter-web</artifactId>  
  30.         </dependency>  
  31.         <dependency>  
  32.             <groupId>org.springframework.cloud</groupId>  
  33.             <artifactId>spring-cloud-starter-openfeign</artifactId>  
  34.         </dependency>  
  35.   
  36.         <dependency>  
  37.             <groupId>org.springframework.boot</groupId>  
  38.             <artifactId>spring-boot-starter-test</artifactId>  
  39.             <scope>test</scope>  
  40.         </dependency>  
  41.     </dependencies>  
  42.   
  43.     <dependencyManagement>  
  44.         <dependencies>  
  45.             <dependency>  
  46.                 <groupId>org.springframework.cloud</groupId>  
  47.                 <artifactId>spring-cloud-dependencies</artifactId>  
  48.                 <version>${spring-cloud.version}</version>  
  49.                 <type>pom</type>  
  50.                 <scope>import</scope>  
  51.             </dependency>  
  52.         </dependencies>  
  53.     </dependencyManagement>  
  54.   
  55.     <build>  
  56.         <plugins>  
  57.             <plugin>  
  58.                 <groupId>org.springframework.boot</groupId>  
  59.                 <artifactId>spring-boot-maven-plugin</artifactId>  
  60.             </plugin>  
  61.         </plugins>  
  62.     </build>  
  63.   
  64. </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

Java代码  技术分享图片
  1. server:  
  2.   port: 8081  
  3. spring:  
  4.   application:  
  5.     name: spring-cloud-consumers  
  6. eureka:  
  7.   client:  
  8.     service-url:  
  9.       defaultZone: http://localhost:8761/eureka/  

 

3. 启动类ConsumersApplication.java

同上,增加@EnableEurekaClient,如果是其他注册中心可以使用注解@EnableDiscoveryClient来进行服务的注册

Java代码  技术分享图片
  1. package com.springcloud.consumers;  
  2.   
  3. import org.springframework.boot.SpringApplication;  
  4. import org.springframework.boot.autoconfigure.SpringBootApplication;  
  5. import org.springframework.cloud.netflix.eureka.EnableEurekaClient;  
  6. import org.springframework.cloud.openfeign.EnableFeignClients;  
  7.   
  8. @SpringBootApplication  
  9. @EnableEurekaClient  
  10. @EnableFeignClients  
  11. public class ConsumersApplication {  
  12.   
  13.     public static void main(String[] args) {  
  14.         SpringApplication.run(ConsumersApplication.class, args);  
  15.     }  
  16.   
  17. }  

 

@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接口

Java代码  技术分享图片
  1. package com.springcloud.consumers.remote;  
  2.   
  3. import org.springframework.cloud.openfeign.FeignClient;  
  4. import org.springframework.web.bind.annotation.RequestMapping;  
  5. import org.springframework.web.bind.annotation.RequestParam;  
  6.   
  7. /** 
  8.  * @Author: shiyao.wei 
  9.  * @Date: 2019/7/2 11:14 
  10.  * @Version: 1.0 
  11.  * @Desc: 
  12.  */  
  13. @FeignClient(name= "spring-cloud-producer")  
  14. public interface HelloRemote {  
  15.     @RequestMapping(value = "/hello")  
  16.     String hello(@RequestParam(value = "name") String name);  
  17. }  

 

  • name:远程服务名,及spring.application.name配置的名称
  • 此类中的方法和远程服务中contoller中的方法名和参数需保持一致

5. web层调用远程接口 Controller

Java代码  技术分享图片
  1. package com.springcloud.consumers.controller;  
  2.   
  3. import com.springcloud.consumers.remote.HelloRemote;  
  4. import org.springframework.beans.factory.annotation.Autowired;  
  5. import org.springframework.web.bind.annotation.PathVariable;  
  6. import org.springframework.web.bind.annotation.RequestMapping;  
  7. import org.springframework.web.bind.annotation.RestController;  
  8.   
  9. /** 
  10.  * @Author: shiyao.wei 
  11.  * @Date: 2019/7/2 11:25 
  12.  * @Version: 1.0 
  13.  * @Desc: 
  14.  */  
  15. @RestController  
  16. public class HelloController {  
  17.     @Autowired  
  18.     HelloRemote helloRemote;  
  19.   
  20.     @RequestMapping("/hello/{name}")  
  21.     public String index(@PathVariable("name") String name) {  
  22.         return helloRemote.hello(name);  
  23.     }  
  24. }  

 

现在,一个最简单的服务注册和调用的例子就完成了。

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:

Java代码  技术分享图片
  1. package com.springcloud.producer.controller;  
  2.   
  3. import org.springframework.web.bind.annotation.RequestMapping;  
  4. import org.springframework.web.bind.annotation.RequestParam;  
  5. import org.springframework.web.bind.annotation.RestController;  
  6.   
  7. /** 
  8.  * Created with IntelliJ IDEA. 
  9.  * 
  10.  * @Date: 2019/7/2 
  11.  * @Time: 0:02 
  12.  * @email: inwsy@hotmail.com 
  13.  * Description: 
  14.  */  
  15. @RestController  
  16. public class HelloController {  
  17.     @RequestMapping("/hello")  
  18.     public String hello(@RequestParam String name) {  
  19.         return "hello "+name+",producer2 is ready";  
  20.     }  
  21. }  

 

修改application.yml配置文件启动端口为8082

启动我们刚复制好的producer2,这时可以看一下注册中心Eureka,我们现在已经有两个producer服务了。

 

技术分享图片
 

 

这时我们再去访问

第一次返回结果:hello spring,producer is ready

第二次返回结果:hello spring,producer2 is ready

连续刷新页面,两个结果会交替出现,说明注册中心提供了服务负载均衡功能。将服务数提高到N个,会发现测试结果一样,请求会自动轮询到每个服务端来处理。

SpringCloud,服务的提供与Feign调用流程

原文:https://www.cnblogs.com/ynog/p/13132324.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!