Spring Cloud OpenFeign入门示例

简介

Feign

Feign是一个声明性web服务客户端。让编写Web服务客户端变得非常容易,只需创建一个接口并在接口上添加注解即可。让http远程调用就像接口调用一样简单。(远程http调用的工具有很多,HttpClient、OKHttp、Spring Boot中的RestTemplate等 —— 我们在 Spring Cloud alibaba 使用Nacos服务发现 一文中就是使用的restTemplate的方式)

OpenFeign和Feign

Feign是由Netflix开源,但目前和ribbon一样停止更新了,所以spring 官方在feign的基础上推出了OpenFeign,目前spring Cloud基本都使用OpenFeign做http调用客户端。

入门示例

pom.xml


<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.0modelVersion>

    <groupId>org.examplegroupId>
    <artifactId>CloudConsumerartifactId>
    <version>1.0-SNAPSHOTversion>

    <properties>
        <build.name>CloudConsumerbuild.name>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
        <java.version>1.8java.version>

        <spring.cloud.version>2021.0.4spring.cloud.version>
        <spring.cloud.alibaba.version>2021.0.4.0spring.cloud.alibaba.version>
    properties>

    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>2.6.11version>
    parent>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloudgroupId>
                <artifactId>spring-cloud-dependenciesartifactId>
                <version>${spring.cloud.version}version>
                <type>pomtype>
                <scope>importscope>
            dependency>
            <dependency>
                <groupId>com.alibaba.cloudgroupId>
                <artifactId>spring-cloud-alibaba-dependenciesartifactId>
                <version>${spring.cloud.alibaba.version}version>
                <type>pomtype>
                <scope>importscope>
            dependency>
        dependencies>
    dependencyManagement>

    <dependencies>

        
        <dependency>
            <groupId>com.alibaba.cloudgroupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discoveryartifactId>
        dependency>

        
        <dependency>
            <groupId>com.alibaba.cloudgroupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-configartifactId>
        dependency>

        
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-loadbalancerartifactId>
        dependency>

        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-openfeignartifactId>
        dependency>

        
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-bootstrapartifactId>
        dependency>



        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>
        <dependency>
            <groupId>junitgroupId>
            <artifactId>junitartifactId>
            <scope>testscope>
        dependency>
        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>

        <dependency>
            <groupId>org.projectlombokgroupId>
            <artifactId>lombokartifactId>
        dependency>

    dependencies>


    <profiles>
        <profile>
            <id>devid>
            <properties>
                <profileActive>devprofileActive>
            properties>
            <activation>
                <activeByDefault>trueactiveByDefault>
            activation>
        profile>
        <profile>
            <id>testid>
            <properties>
                <profileActive>testprofileActive>
            properties>
        profile>
        <profile>
            <id>preid>
            <properties>
                <profileActive>preprofileActive>
            properties>
        profile>
        <profile>
            <id>prodid>
            <properties>
                <profileActive>prodprofileActive>
            properties>
        profile>
    profiles>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
            plugin>
        plugins>

        <finalName>${build.name}finalName>
        <resources>
            <resource>
                <directory>src/main/resourcesdirectory>
                <filtering>truefiltering>
                <includes>
                    <include>mapper/**/*.xmlinclude>
                    <include>application.propertiesinclude>
                    <include>application-${profileActive}.propertiesinclude>
                    <include>bootstrap.propertiesinclude>
                    <include>logback-spring.xmlinclude>
                includes>
            resource>
        resources>
    build>

project>

Application 启动类

package com.yyoo.cloud;

import com.yyoo.cloud.conf.MyNacosLoadBalancerConfiguration;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.loadbalancer.annotation.LoadBalancerClients;
import org.springframework.cloud.openfeign.EnableFeignClients;

@SpringBootApplication(scanBasePackages = {"com.yyoo"})
@EnableDiscoveryClient // 开启服务注册发现功能
@EnableFeignClients  // 开启OpenFeign功能
// 更改默认的负载均衡策略
@LoadBalancerClients(defaultConfiguration = MyNacosLoadBalancerConfiguration.class)
public class Application {

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

}

MyNacosLoadBalancerConfiguration类的代码在我们的前文 Spring Cloud LoadBalancer(负载均衡) 中定义

OpenFeign客户端代码MyClient

package com.yyoo.cloud.client;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;

@FeignClient("myCloud")
public interface MyClient {

    @RequestMapping("/myCloud/conf/getName")
    String getName();

    @RequestMapping("/myCloud/conf/getCommonConf")
    String getCommonConf();
}

该调用的是我们Spring Cloud alibaba 使用Nacos配置中心 一文中的Provider。所以本文中没有Provider的实现示例。

注:@FeignClient(“myCloud”)中的myCloud是我们Provider的ServiceID,@RequestMapping中的myCloud是Provider的应用上下文

@FeignClient 注解的 contextId 字段,默认情况下,@FeignClient 注解的接口,Bean的名称为value属性(或name属性的值),其对应为Provider的应用名称,当我们需要将同一个Provider的接口拆分为多个 Feign 接口时,contextId 就可作为 bean 的名称来区分不同的 FeignClient

注:@FeignClient注解的类上,不能使用@RequestMapping注解。(这是官网提示)
在这里插入图片描述
翻译过来
@FeignClient 接口不应该在服务器与客户端直接共享,且不再支持 @RequestMapping 注解作为类级别的注解,注解在 @FeignClient 注解的接口上

编写测试Controller

package com.yyoo.cloud.controller;

import com.yyoo.cloud.client.MyClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RequestMapping("openFeign")
@RestController
public class MyController1 {

    @Resource
    private MyClient myClient;

    @RequestMapping("getName")
    public String getName(){
        String rs = myClient.getName();
        System.out.println("OpenFeign:" + rs);
        return rs;
    }

    @RequestMapping("getCommonConf")
    public String getCommonConf(){
        String rs = myClient.getCommonConf();
        System.out.println("OpenFeign:" + rs);
        return rs;
    }

}

调用 http://127.0.0.1:8704/myCloud/openFeign/getCommonConf 接口,多测试几次

  1. 先判断OpenFeign调用是否成功,是否返回对应结果
  2. 启动多个Provider(相同ip可以使用不同端口启动),验证我们的负载均衡配置是否成功。

到此我们的OpenFeign入门就完成了,但其涉及到的问题还有很多,比如:失败重试、负载均衡的健康检查、请求的熔断降级等等一系列的问题,我们都将一一说明。

你可能感兴趣的:(人在江湖之Spring,Cloud,aibaba,spring,cloud,spring,boot,Feign,OpenFeign,负载均衡)