SpringCloud Feign application

One: Brief Introduction

On the interface to be invoked add comment @FeignClient

Two: Create a eureka-feign-client services

2.1 Module to create a new project in the main Maven project, named eureka-feign-client. Create a way of using Spring Initializr way.
Here Insert Picture Description
2.2 eureka-feign-client pom.xml content is as follows:

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.springcloud</groupId>
        <artifactId>springcloud-hx</artifactId>
        <version>1.0-SNAPSHOT</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>eureka-feign-client</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>eureka-feign-client</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <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-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

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

</project>

Main Module of pom.xml 2.3 plus:
Here Insert Picture Description
content profile application.yml of 2.4 eureka-feign-client are as follows:

server:
  port: 8766

spring:
  #配置程序名为eureka-feign-client
  application:
    name: eureka-feign-client

eureka:
  client:
    #服务注册地址
    serviceUrl:
      #注意: Eureka Server 的注册地址
      #将服务提供者注册到三个Eureka Server中去
      #defaultZone: http://peer1:8001/eureka/,http://peer2:8002/eureka/,http://peer3:8003/eureka/
      #defaultZone: http://peer1:8001/eureka/
      defaultZone: http://localhost:8761/eureka/

2.5 on the open service start classes with the @EnableDiscoveryClient registered in discovery, coupled with @EnableFeignClients open Feign Client function code is as follows:

package com.example.eurekafeignclient;

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

@SpringBootApplication
//开启服务注册于发现
@EnableDiscoveryClient
//开启 Feign Client 功能
@EnableFeignClients
public class EurekaFeignClientApplication {

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

}

2.6 Create a new interface, named FeignInter, plus @FeignClient comment on the interface to declare a Feign Client, which calls the service name value other services for the remote.
FeignConfig.class to Feign Client configuration class, the class Retryer injection instance, so that after remote call fails, Feign will retry.
code show as below:

package com.example.eurekafeignclient.inter;


import com.example.eurekafeignclient.config.FeignConfig;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;


//在接口上加 @FeignClient 注解来声明 一个Feign Client,其中 value 为 远程调用其他服务的服务名
//FeignConfig.class 为 Feign Client 的配置类,注入Retryer类的实例,这样在远程调用失败后,feign会进行重试
//使用 Spring MVC 的注解来绑定具体该服务提供的 REST 接口

@FeignClient(value = "eureka-client",configuration = FeignConfig.class)
public interface FeignInter {

    //使用 Spring MVC 的注解来绑定具体该服务提供的 REST 接口
    @GetMapping(value = "/HiController/hi/{name}")
    String sayHiFromEurekaClient(@PathVariable(value = "name") String name);
}

2.7 Creating a configuration class FeignConfig code is as follows:

package com.example.eurekafeignclient.config;


import feign.Retryer;
import org.springframework.context.annotation.Configuration;

import static java.util.concurrent.TimeUnit.SECONDS;

//注入Retryer类的实例,这样在远程调用失败后,feign会进行重试
@Configuration
public class FeignConfig {


    public Retryer feignRetryer(){
        //Feign 默认的配置在请求失败后,重试次数为0,即不重试。
        //重试间隔 为100毫秒,最大重试时间为1秒,重试次数为5次
        //return new Retryer.Default();
        return new Retryer.Default(100,SECONDS.toMillis(1),5);
    }

}

2.8 FeignService create a class, as follows:

package com.example.eurekafeignclient.service;

import com.example.eurekafeignclient.inter.FeignInter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class FeignService {

    @Autowired
    private FeignInter feignInter;

    public String helloFeign(String name) {
        return feignInter.sayHiFromEurekaClient(name);
    }
}

2.9 FeignController create a class, as follows:

package com.example.eurekafeignclient.web;

import com.example.eurekafeignclient.service.FeignService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/FeignController")
public class FeignController {

   @Autowired
    private FeignService feignService;

    @GetMapping("/hi/{name}")
    public String helloFeign(@PathVariable("name") String name) {
        StringBuffer sb  = new StringBuffer();
        //模仿 50次用户请求
        for (int i=0;i<50;i++){
            if(i==49){
                sb.append(feignService.helloFeign(name));
            }else {
                sb.append(feignService.helloFeign(name)).append("---");
            }
        }
        return sb.toString();
    }

}

2.10 respectively start eureka-server, eureka-client- 8762, eureka-client-8763, eureka-feign-client services.
Browser to access http: // localhost: 8761 /
Here Insert Picture Description

As of input in the browser: http: // localhost: 8766 / FeignController / hi / java, get the results as follows:
Here Insert Picture Description
At this point can be found when accessing http: // localhost: When API interface 8766 / FeignController / hi / java, and Feign Client remote call "/ hi" AP Interface eureka-client port 8762 and 8763 are two examples of. Therefore, Feign Client has in load balancing, two examples will alternately eureka-client requests in the "/ hi" API interface.

See feign start dependent spring-cloud-starter-openfeign, code is as follows:

<dependencies>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-openfeign-core</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-commons</artifactId>
		</dependency>
		<dependency>
			<groupId>io.github.openfeign</groupId>
			<artifactId>feign-core</artifactId>
		</dependency>
		<dependency>
			<groupId>io.github.openfeign</groupId>
			<artifactId>feign-slf4j</artifactId>
		</dependency>
		<dependency>
			<groupId>io.github.openfeign</groupId>
			<artifactId>feign-hystrix</artifactId>
		</dependency>
		<dependency>
			<groupId>io.github.openfeign</groupId>
			<artifactId>feign-java8</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
			<optional>true</optional>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-archaius</artifactId>
			<optional>true</optional>
		</dependency>
	</dependencies>

We can see the initial introduction of the Ribbon and rely on the default Hystrix dependence, that is dependent on load balancing and fuse

The next article to learn Feign from source

Published 33 original articles · won praise 42 · views 3162

Guess you like

Origin blog.csdn.net/weixin_40991408/article/details/103859068
Recommended