Feign:Feign is a declarative, templated HTTP client.

As I understand it, Feign functions like the Dubbo Exposure service, but differs slightly from Dubbo in that Feign is exposed as an HTTP REST interface.

Service (8762) (8763) (8763) (8762) (8763) (8763)

New project, service-feign (8765), POM added feIGN dependency, complete POM code 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 > < groupId > com. Dalaoyang < / groupId > < artifactId > springcloud_feign < / artifactId > < version > 0.0.1 - the SNAPSHOT < / version > < packaging > jar < / packaging > < name > springcloud_feign < / name > <description>springcloud_feign</description> <parent> <groupId>org.springframework.boot</groupId> The < artifactId > spring - the boot - starter - parent < / artifactId > < version > 1.5.9. 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>Edgware.SR1</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope>
        </dependency>

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


</project>
Copy the code

Add the @enableFeignClients annotation to the startup class. If you do not add basePackages to the scan package, the default is to find all @FeignClients that the project will scan. The startup class code is as follows:

package com.dalaoyang;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;


@SpringBootApplication
@EnableEurekaClient
//@EnableFeignClients(basePackages = "com.dalaoyang.interfaces") @EnableFeignClients public class SpringcloudFeignApplication { public static void main(String[] args) { SpringApplication.run(SpringcloudFeignApplication.class, args); }}Copy the code

Create Feign exposed interface, the interface with @ FeignClient (value = “service”, the fallback = FeignFallbackService. Class). Fallback: fallback: fallback: fallback: fallback: fallback: fallback: fallback: fallback: fallback: fallback: fallback: fallback: fallback: fallback: fallback: fallback: fallback: fallback: fallback: fallback: fallback: fallback: fallback: fallback The code is as follows:

package com.dalaoyang.interfaces;

import com.dalaoyang.back.FeignFallbackService;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;

/**
 * @author dalaoyang
 * @Description
 * @project springcloud_learn
 * @package com.dalaoyang.interfaces
 * @email [email protected]
 * @date 2018/4/20
 */
@FeignClient(value="service", fallback = FeignFallbackService. Class) / / here is the name of the service of the remote call, Public interface FeignInterface {@getMapping () {fallback = fallback ();"/")
    String IndexInfo();
}
Copy the code

Remote call failure callback class FeignFallbackService, to implement the corresponding FeignClient implementation of the corresponding method:

package com.dalaoyang.back;

import com.dalaoyang.interfaces.FeignInterface;
import org.springframework.stereotype.Service;

/**
 * @author dalaoyang
 * @Description
 * @project springcloud_learn
 * @package com.dalaoyang.back
 * @email [email protected]
 * @date 2018/4/20
 */
@Service
public class FeignFallbackService implements FeignInterface {
    @Override
    public String IndexInfo() {return "Remote call failed!";
    };
}
Copy the code

The last thing to note about the configuration file is feign.hystrix.enabled. If you want to configure the remote call callback, you need to declare Feign’s Hystrix support, otherwise the page will still display errors.

# # the port number
server.port=8765

# service name
spring.application.name=service_feign

Registry address
eureka.client.service-url.defaultZone=http://eureka.dalaoyang.cn/eureka/


## Declare Feign's Hystrix support
feign.hystrix.enabled=true
Copy the code

Start the service – feign (8765), the first to take a look at http://eureka.dalaoyang.cn

You can see that the service was registered and then go to http://localhost:8765/, and you can see that the page alternates as it does when you use the ribbon.

The console

Then we close service (8762), service (8763), access again, you can see

Source code download: elder Yang code cloud

Personal website: dalaoyang.cn