Spring-cloud之声明式服务调用Feign

    xiaoxiao2024-03-09  130

    什么是Feign

        Spring cloud实现服务调用的方式有两种分别是ribbon和feign。Ribbon的调用比较麻烦而且不灵活可以参考博客https://blog.csdn.net/sinat_32366329/article/details/90383664 看下具体的调用过程。

        Feign是netflix开发的声明书模板化的HTTP客户端,可以帮助我们更加灵活和便捷的通过HTTPAPI调用服务。Feign是支持所有Spring MVC注解的,同时整合了ribbon做负载均衡。

    服务注册中心搭建

        由于之前已经介绍过注册中心/服务提供者和基于ribbon的消费者调用,不懂的可以看下面的博客:https://blog.csdn.net/sinat_32366329/article/details/90383664

    服务提供者

        我们在之前的服务提供者中创建一个Controller后添加一个方法如下:

    @ResponseBody @GetMapping(value = "/get") public String get(@RequestParam(value = "name") String name) { System.out.println("--------------------provider--------------------"); return "Hello " + name; }

    服务消费者

        这里为了大家更加简便的理解,所以贴全部代码出来,如果不理解的可以看看之前的博文系统的学习比较好。

    POM

        Pom这里的spring boot版本使用了2.1.5.RELEASE版本,如果有问题可以修改回之前的版本,但是消息POM的其他依赖就会有很大的区别。因为spring cloud在2.X版本和1.X版本对包名做了比较大的改动,所以需要小心。

    <?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.5.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>consumer</artifactId> <version>0.0.1-SNAPSHOT</version> <name>consumer</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.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>

    Application.yml

    server: port: 8971 spring: application: name: consumer eureka: client: service-url: default-zone: http://localhost:8761/eureka/

    服务端接口

        Feign是基于接口的方式实现对服务提供者的调用的,所以需要创建和服务提供者参数一致的接口,并且需要给接口配置对应的注解才可以实现。

    @FeignClient:注解的name属性是注册中心服务提供者的实例名称,可以通过这个名称找到对应的服务提供者调用对应的方法。

    @RequestParam:注解是Spring MVC的注解,前面说过feign支持Spring MVC注解,这里的作用是传参时候需要指定传递的参数,这个注解也支持传递Map对象。如果参数没有注解调用会出错。该注解是针对简单的参数类型,下面简单介绍一下复杂的参数使用什么注解。

    @RequestHeader:这个注解是将参数放到请求头

    @RequestBody:这个注解可以传递自定义类型的参数例如User.class这些类等。

    import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; @FeignClient(name = "PROVIDER-USER") public interface UserFeignClientInterface { @GetMapping(value = "/get") String get(@RequestParam(value = "name") String name); }

    启动类修改

    import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.openfeign.EnableFeignClients; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController; @Controller @EnableFeignClients @EnableDiscoveryClient @SpringBootApplication public class ConsumerApplication { @Autowired private UserFeignClientInterface userFeignClientInterface; public static void main(String[] args) { SpringApplication.run(ConsumerApplication.class, args); } @ResponseBody @GetMapping(value = "/get/{name}") public String get(@PathVariable String name) { System.out.println("--------------------consumer--------------------"); return this.userFeignClientInterface.get(name); } }

    启动

    启动注册中心启动服务提供者启动服务消费者访问服务消费者的get方法,看日志是否有正常访问到服务提供者中的方法

    微信公众号

        本人开通了个人公众号,以后所有在发表的博客都会同步到公众号,可以关注以便第一时间学习到新的技术知识。

     

    知春秋 认证博客专家 博客专家 Java高级研发 不忘初心,方得始终。初心易得,始终难守。
    最新回复(0)