SpringCloud (Finchley version) tutorial (Three): Consumer Services (Feign)

On an article about how to go through RestTemplate + Ribbon consumer services, this article focuses on how to consume services through Feign.

A, Feign About
Feign is a pseudo Http client a declarative, it makes writing easier Http client. Using Feign, only you need to create an interface and annotation. It has pluggable annotation feature can be used Feign JAX-RS annotations and notes. Feign pluggable encoder and decoder. Feign default integrated Ribbon, and Eureka and combined, the default implementation of load balancing.

in short:

Feign uses annotation-based interface
Feign integrated ribbon, with load balancing capabilities
integrated Hystrix, has the ability to fuse the
two, preparations
continue to spend a project, start eureka-server, port 8761; start service- hi twice ports are 8762, 8773.

Third, create a service feign
a new spring-boot project, named serice-feign, in its pom-dependent file introduced Feign start spring-cloud-starter-feign, Eureka start dependent spring-cloud-starter-netflix -eureka-client, Web start dependent spring-boot-starter-web, code 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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>com.liu</groupId>
        <artifactId>SpringCloudLearn01</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <groupId>com.liu.example</groupId>
    <artifactId>serice-feign</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>serice-feign</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    </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>
    </dependencies>

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

</project>

In the project's profile application.yml file, specify the program called service-feign, port number 8765, registered service address is http: // localhost: 8761 / eureka /, the following code:

server:
  port: 8765
spring:
  application:
    name: service-feign
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/

In the startup class ServiceFeignApplication program, plus @EnableFeignClients comment open Feign features:

package com.liu.example.sericefeign;

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

@SpringBootApplication
@EnableEurekaClient    //不能缺少的
@EnableDiscoveryClient
@EnableFeignClients
public class SericeFeignApplication {

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

Feign a defined interface through @ FeignClient ( "service name"), which specifies the service call. For example, to call the "/ hi" Interface service-hi services in the code, the code is as follows:

package com.liu.example.sericefeign.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 = "service-client")
public interface SchedualServiceHi {
    @RequestMapping(value = "/hi",method = RequestMethod.GET)
    String sayHiFromClientOne(@RequestParam(value = "name") String name);
}

In the controller layer Web layer, external exposure to a "/ hi" API interface, Feign customers through the above-defined end SchedualServiceHi to consumer services. code show as below:

package com.liu.example.sericefeign.controller;

import com.liu.example.sericefeign.service.SchedualServiceHi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
public class HelloController {

    @Autowired
    public SchedualServiceHi schedualServiceHi;

    @RequestMapping(value = "/hi",method = RequestMethod.GET)
    public String sayHi(@RequestParam String name){
        return schedualServiceHi.sayHiFromClientOne(name);
    }

}

Start the program, many visits to http: // localhost:? 8765 / hi name = forezp, browser alternates:

! Hello my port number is: 8762

! Hello my port number is: 8763

Download Source: https://github.com/MrLiu1227/MySpringCloud.git

Guess you like

Origin www.cnblogs.com/liuyuan1227/p/11220266.html