SpringCloud无废话入门03:Feign声明式服务调用

1.Feign概述

        在上一篇的HelloService这个类中,我们有这样一行代码:

        return restTemplate.getForObject("http://hello-service/hello",String.class);

        对于代码有一定洁癖的你来说,一定感觉到了,这个url应该是可以被配置的。既然说到配置,那我们首先想到的就是使用java注解的方式。Feign就是这样一个注解框架,它也是netflix为我们提供的,方便我们整合ribbon和hystrix(后面会学习)。

        使用Feign,我们能很方便的通过编写接口并插入注解,来定义和代理HTTP请求。Feign主要具备如下特性:

        支持Feign注解; 

        支持Ribbon负载均衡;

        支持HTTP编码器和解码器;

        提供了熔断器Hystrix;

2.Feign引入

        让我们修改ribbon这个项目,使之支持feign。

        首先,pom引入,

<?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/maven-v4_0_0.xsd">

    <parent>

        <artifactId>springcloud.parent</artifactId>

        <groupId>com.zuikc</groupId>

        <version>1.0-SNAPSHOT</version>

    </parent>

    <modelVersion>4.0.0</modelVersion>

    <packaging>war</packaging>

    <name>ribbon</name>

    <artifactId>ribbon</artifactId>

    <dependencyManagement>

        <dependencies>

            <dependency>

                <groupId>org.springframework.cloud</groupId>

                <artifactId>spring-cloud-dependencies</artifactId>

                <version>Greenwich.RELEASE</version>

                <type>pom</type>

                <scope>import</scope>

            </dependency>

        </dependencies>

    </dependencyManagement>

    <dependencies>

        <dependency>

            <groupId>org.springframework.boot</groupId>

            <artifactId>spring-boot-starter</artifactId>

        </dependency>

        <dependency>

            <groupId>org.springframework.cloud</groupId>

            <artifactId>spring-cloud-starter-eureka</artifactId>

            <version>1.4.6.RELEASE</version>

        </dependency>

        <dependency>

            <groupId>org.springframework.boot</groupId>

            <artifactId>spring-boot-starter-test</artifactId>

            <scope>test</scope>

        </dependency>

        <dependency>

            <groupId>org.springframework.boot</groupId>

            <artifactId>spring-boot-starter-web</artifactId>

        </dependency>

        <dependency>

            <groupId>org.springframework.cloud</groupId>

            <artifactId>spring-cloud-starter-feign</artifactId>

            <version>1.4.6.RELEASE</version>

        </dependency>

    </dependencies>

</project>

        注意pom中的粗体部分。引入Feign依赖,会自动引入Hystrix依赖。

        appcation.yml并不需要变动。

3.@EnableFeignClients

        修改ServiceRibbonApplication,加入注解@EnableFeignClients,这一步很重要,说明项目对于feign的支持,

package com.zuikc;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

import org.springframework.cloud.client.loadbalancer.LoadBalanced;

import org.springframework.cloud.openfeign.EnableFeignClients;

import org.springframework.context.annotation.Bean;

import org.springframework.web.client.RestTemplate;

/**

 * @ClassName ServiceRibbonApplication

 * @Description 我们提供咨询和培训服务,关于本文有任何困惑,请关注并联系“码农星球”

 * @Author 码农星球

 **/

@SpringBootApplication

@EnableDiscoveryClient

@EnableFeignClients

public class ServiceRibbonApplication {

    public static void main(String[] args) {

        SpringApplication.run(ServiceRibbonApplication.class, args);

    }

    @Bean

    @LoadBalanced

    RestTemplate restTemplate() {

        return new RestTemplate();

    }

}

4.服务接口

        创建一个接口,

package com.zuikc;

import org.springframework.cloud.openfeign.FeignClient;

import org.springframework.web.bind.annotation.RequestMapping;

@FeignClient(value = "hello-service")

public interface HelloService {

    //服务中方法的映射路径

    @RequestMapping("/hello")

    String hello();

}

        这个接口就比较重要了。

        注解@FeignClient说明了,我们需要去eureka服务上去调用“hello-service”这个服务。

        注解@RequestMapping指的是我们需要调用的路径。

5.控制器

        现在我们还需要最后一步,就是创建一个控制器来接受请求,然后让robbin去分发,

package com.zuikc;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

/**

 * @ClassName ConsumerController

 * @Description 我们提供咨询和培训服务,关于本文有任何困惑,请关注并联系“码农星球”

 * @Author 码农星球

 **/

@RestController

public class HelloController {

    @Autowired

    HelloService helloService;

    @RequestMapping("/hello")

    public String helloConsumer(){

        return helloService.hello();

    }

}

        当一切处理完成,让我们启动这个项目,我们仍旧看到ribbon的这个服务,

        然后,http://localhost:9291/hello吧,可以看到LB非常的成功。

        现在,假设其中一个服务提供者因为某种原因(比如异常、断网)停掉了,看看结果会是怎么样的。为了模拟这个现象,让我们关闭provider2,发现我们无论怎么刷新上面的url,LB永远分配到provider1。这也是分布式系统容错能力更强的一种保证!

        感谢关注“码农星球”。本文版权属于“码农星球”。我们提供咨询和培训服务,关于本文有任何困惑,请关注并联系我们。

posted @   陆敏技  阅读(577)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
历史上的今天:
2015-03-27 C#零基础入门07:打老鼠之面向对象重构
2014-03-27 关于 as 播放器的记录
Web Counter
Coupon for Contacts
点击右上角即可分享
微信分享提示