Eclipse runtime configuration SpringCloud (Hoxton + 2.2.4) + Micro Services Framework to build a service consumer Feign

Feign is a web service client a declarative, it makes writing web service clients easier. Creating interfaces, add annotations for the interface, you can use Feign. Feign Feign annotations may be used or JAX-RS annotations, also hot swappable the encoder and decoder. Spring Cloud Add to Feign a Spring MVC annotation support and integrated Ribbon and Eureka to provide load balancing when using Feign.

Start registration centers and service providers

https://blog.csdn.net/miaodichiyou/article/details/104160284

1 start SpringCloud high availability service registry (the Eureka) built registry
2, starting three springcloud-eureka-provider instance, ports 8001,8002 and 8003, respectively

Create a service consumer

Here Insert Picture Description

Add file dependencies POM.xml

<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>com.springcloud</groupId>
    <artifactId>springcloud-root</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <artifactId>springcloud-feign</artifactId>
  <name>springcloud-feign</name>
  <url>http://maven.apache.org</url>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </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>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
    </plugins>
  </build>
</project>

Application.yml configuration file

Here Insert Picture Description

spring:
  application:
    name: springcloud-feign
  freemarker:
    prefer-file-system-access: false
  security:
    user:
      name: admin
      password: 123456
    
server:
  port: 8101

eureka:
  instance:
    hostname: eureka-feign.com
    instance-id: eureka-feign
  client:
    service-url:
      defaultZone: http://${spring.security.user.name}:${spring.security.user.password}@eureka-peer1.com:8897/eureka/,http://${spring.security.user.name}:${spring.security.user.password}@eureka-peer2.com:8898/eureka/,http://${spring.security.user.name}:${spring.security.user.password}@eureka-peer3.com:8899/eureka/

Modify C: \ Windows \ System32 \ drivers \ etc \ hosts

127.0.0.1 eureka-feign.com

Add a service consumer class start

Here Insert Picture Description

  • FeignApplication.java

    In the program's startup class annotate @EnableFeignClients open Feign Client function

package org.springcloud.feign;

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
@EnableDiscoveryClient
@SpringBootApplication
public class FeignApplication {

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

New Interface

Here Insert Picture Description

  • FeignConsumer.java

    Create a new FeignConsumer interface:

    • By @FeignClient ( "service") to specify which service calls. value call service name other services for remote, FeignConfig.class for the configuration class.
    • FeignConsumer within a sayHiFromEurekaProvider () method, the method is invoked springcloud-eureka-provider services through Feign "/ hi" of api interface.
package org.springcloud.feign;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Component
/*指定这个接口所要调用的提供者服务名称 */
@FeignClient(value = "springcloud-eureka-provider",configuration = FeignConfig.class)

public interface FeignConsumer {
    @GetMapping(value = "/hi")
    String sayHiFromEurekaProvider(@RequestParam(value = "name")String name);
}

Add Configuration

Here Insert Picture Description

  • FeignConfig .java

    Plus @Configuration comment on FeignConfig class, that this is a configuration class, and inject a BeanName to Retryer of feignRetryer of Bean. It can feign after the remote call failure can be retried.

package org.springcloud.feign;

import java.util.concurrent.TimeUnit;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import feign.Retryer;

@Configuration
public class FeignConfig {
    @Bean
    public Retryer feignRetryer(){
        return new Retryer.Default(100,TimeUnit.SECONDS.toMillis(1),5);
    }
}

Add the service consumer service provided by the service provider instance

Here Insert Picture Description

  • FeignService .java

    Service Class layer FeignService injection of Bean FeignConsumer

package org.springcloud.feign;

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

@Service
public class FeignService {
    @Autowired
    FeignConsumer feignConsumer;
    public String hi(String name){
        return feignConsumer.sayHiFromEurekaProvider(name);
    }
}

Create a controller, the method call service

Here Insert Picture Description

  • FeignController.java

    FeignController class called FeignService of hi () method, FeignService call API interface springcloud-eureka-provider services "/ hi" by FeignConsumer remote.

package org.springcloud.feign;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/*调用提供者的home方法*/
@RestController
public class FeignController {
    @Autowired
    FeignService feignService;

    @GetMapping("/hi")
    public String hi(@RequestParam(defaultValue = "zhaojq",required = false)String name){
        return feignService.hi(name);
    }
}

to sum up

Feign source implementation process is as follows:

  1. First open FeignClient function by @EnableFeignClients comment. Only the existence of the notes, will open the scan @FeignConfig annotations packages when the program starts,
  2. Feign implement the interface according to the rules, and add annotations in the interface @FeignClient above.
  3. When the program starts the package will be scanned, scan all annotations @FeignClient class, and injected into the IOC container information.
  4. When a method is called an interface, is generated by a specific template object RequestTemplate JDK agent.
  5. Regenerated Http Request object request according RequestTemplate.
  6. Client Request object to the processing to which the Client network request frame may be HttpURLConnection, HttpClient and OkHttp.
  7. Finally Client is encapsulated LoadBalanceClient class, class Ribbon binding do load balancing.

Start springcloud-feign project

View Eureka registry

Run startup class, visit http://eureka-peer1.com:8897/
Here Insert Picture Description
http://eureka-peer2.com:8898/,http://eureka-peer3.com:8899/ results above.
Examples of consumer service has been successfully registered.

Browser numerous visits http://eureka-feign.com:8101/hi
Here Insert Picture Description

In the command window curl http://eureka-feign.com:8101/hi, Feign has been found to achieve load balancing
Here Insert Picture Description

Published 72 original articles · won praise 66 · Views 150,000 +

Guess you like

Origin blog.csdn.net/miaodichiyou/article/details/104304853