SpringCloud学习-(5)服务消费与负载均衡(feign)

前面两篇SpringCloud学习-(3)服务消费与负载均衡(ribbon)

SpringCloud学习-(4)服务消费与负载均衡(自定义ribbon)
介绍了使用ribbon+restTemplate实现负载均衡消费服务,本篇将介绍Feign实现服务的消费。

1.Feign
Feign is a declarative web service client. It makes writing web service clients easier. To use Feign create an interface and annotate it. It has pluggable annotation support including Feign annotations and JAX-RS annotations. Feign also supports pluggable encoders and decoders. Spring Cloud adds support for Spring MVC annotations and for using the same HttpMessageConverters used by default in Spring Web. Spring Cloud integrates Ribbon and Eureka to provide a load balanced http client when using Feign.
Feign 是一个声明web服务客户端,这便得编写web服务客户端更容易,使用Feign 创建一个接口并对它进行注解,它具有可插拔的注解支持包括Feign注解与JAX-RS注解,Feign还支持可插拔的编码器与解码器,Spring Cloud 增加了对 Spring MVC的注解,Spring Web 默认使用了HttpMessageConverters, Spring Cloud 集成 Ribbon 和 Eureka 提供的负载均衡的HTTP客户端 Feign。
也就是说Feign是一个声明式接口,且整合了Ribbon。
2.创建Feign工程
延续前几篇的项目服务注册中心springcloud-eureka-server端口号8761,服务提供者springcloud-eureka-client1端口号8762,springcloud-eureka-client2端口号8763,服务名service-sayHello。
创建新的maven工程

GroupId:com.tangjinyi
ArtifactId:springcloud-eureka-feign

2.1 pom.xml配置文件如下:

<?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>

    <groupId>com.tangjinyi</groupId>
    <artifactId>springcloud-eureka-feign</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>springcloud-eureka-feign</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <spring-cloud.version>Finchley.M9</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
        <!--
        springboot1.x版本使用的feign使用的artifactId为spring-cloud-starter-feign,2.x变更为spring-cloud-starter-openfeign,配置的时候注意版本
        -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</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>

    <repositories>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>


</project>

2.2application.properties文件配置

#依然是将feign服务注册到eureka
eureka.client.service-url.defaultZone = http://localhost:8761/eureka/
server.port=8765
spring.application.name=service-feign

2.3修改启动类
SpringcloudEurekaFeignApplication

package com.tangjinyi;

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

/**
*使用@EnableFeignClients注解开启Feign功能
*/
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class SpringcloudEurekaFeignApplication {

    /**
     * Feign对ribbon进行了封装。
     * 不使用restTemplate调用client1和client2的链接地址。
     * 通过在接口上面标记@FeignClient(value="service-sayHello")注解,调用client1和client2的服务service-sayHello
     */
    public static void main(String[] args) {
        SpringApplication.run(SpringcloudEurekaFeignApplication.class, args);
    }
}

2.4填写feign调用流程程序
UserController

package com.tangjinyi.controller;

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.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.tangjinyi.repository.UserRepository;
@RestController
public class UserController {

    @Autowired
    public UserRepository userRepository;

    @RequestMapping(value = "/sayHello",method = RequestMethod.GET)
    public String sayHello(@RequestParam String name){
        return userRepository.sayHello(name);
    }

}

UserRepository
UserRepository 采用接口的方式,在接口上面标记Feign提供的注解@FeignClient,表示调用eureka注册中心上注册的service-sayHello应用。

package com.tangjinyi.repository;

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

@FeignClient(value="service-sayHello")
@Repository
public interface UserRepository {

    @RequestMapping(value = "/sayHello",method = RequestMethod.GET)
    public String sayHello(@RequestParam(value = "name") String name);
}

完整的项目结构如下:
这里写图片描述

3.启动服务
启动springcloud-eureka-server
启动springcloud-eureka-client1和springcloud-eureka-client2
启动springcloud-eureka-feign
浏览器访问eureka注册中心控制台http://localhost:8761
这里写图片描述

浏览器访问Feign客户端
http://localhost:8765/sayHello?name=1234567
这里写图片描述

不断刷新浏览器,Feign按照轮询的方式访问两个服务提供者。

猜你喜欢

转载自blog.csdn.net/jinjin603/article/details/80404603