SpringCloud(Finchley.RELEASE版本)入门学习之————Feign服务消费者(声明式服务调用)

一、Spring Cloud Feign的简介

Spring Cloud Feign 它是基于Netflix Feign实现,整合了Spring Cloud Ribbon 与Spring Cloud Hystrix,除了提供这两者的强大功能之外,他还提供了一种声明式的web服务端定义方式。
在Spring Cloud Feign的实现下,我们只需要创建一个接口并使用注解方式来配置他,即可完成对服务提供方接口的绑定,简化了Spring Cloud Ribbon时自行封装服务调用客户端的开发量。Spring Cloud Feign具备可插拔的注解支持,包括Feign注解和JAX-Rs注解。同时为了适应Spring 的广大用户,他在Netflix Feign的基础上扩展了对SpringMVC的注解支持。

二、搭建Spring Cloud Feign

1、和之前一样,创建一个maven工程,pom文件引入如下配置


<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>
    <parent>
        <artifactId>spring-CloudartifactId>
        <groupId>com.demo.cloudgroupId>
        <version>1.0-SNAPSHOTversion>
    parent>
    <groupId>com.demo.cloudgroupId>
    <artifactId>eureka-feignartifactId>
    <version>0.0.1-SNAPSHOTversion>
    <packaging>jarpackaging>
    <name>eureka-feignname>
    <description>Demo project for Spring Bootdescription>


    <dependencies>
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
        
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-openfeignartifactId>
        dependency>
    dependencies>


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

project>

与之前相比就是多了feign的依赖

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

2、在主启动类中通过@EnableFeignClients注解开启Spring Cloud Feign 的支持。

package com.demo.cloud.eurekafeign;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;

@EnableDiscoveryClient
@EnableFeignClients//开启Spring Cloud Feign支持
@SpringBootApplication
public class EurekaFeignApplication {

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

}

3、yml文件指定端口、注册中心,服务名


server:
  port: 8765

spring:
  application:
    name: eureka-feign
eureka:
  client:
    service-url:
      defalutZone: http://localhost:8761/eureka/

4、定义FeignService接口,通过@FeignClient注解指定服务名来绑定服务,然后再通过SpringMVC注解来绑定具体该服务提供的rest接口。

package com.demo.cloud.eurekafeign.service;

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

@FeignClient(value = "eureka-client")
public interface FeignService {

    @RequestMapping(value = "hi",method = RequestMethod.GET)
    public String sayHi(@RequestParam(value = "message") String message);
}

注:这里服务名不区分大小写。

5、Controller调用service

package com.demo.cloud.eurekafeign.controller;

import com.demo.cloud.eurekafeign.service.FeignService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * @ClassName feginController
 * @Description TODO
 * @Author shanzz
 * @Date 2019/5/28 17:28
 * @Version 1.0
 **/
@RestController
public class FeginController {

    @Autowired
    private FeignService feignService;

    @RequestMapping(value = "/hi",method = RequestMethod.GET)
    public String sayHi(String message){
        return feignService.sayHi(message);
    }

}

6、启动后eureka-server服务注册中心结果
在这里插入图片描述

7、多次访问http://localhost:8765/hi?message=helloFeign ;可发现页面交替显示如下结果

1 Hello the port is : 8763 , message is : helloFeign
2 Hello the port is : 8762 , message is : helloFeign
说明我们已经实现了Feign来访问不同端口的实例

【参考资料】
《Spring Cloud微服务实战》
https://www.funtl.com/

你可能感兴趣的:(一起读书,Spring,Cloud,微服务)