天天看点

SpringBoot集成feign,优雅的调用其他项目的接口

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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.cxb</groupId>
    <artifactId>springboot</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springboot-feign</name>
    <description>Demo project for Spring Boot</description>

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

        <dependencies>

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

            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>

            <dependency>
                <groupId>io.github.openfeign</groupId>
                <artifactId>feign-core</artifactId>
                <version>9.5.0</version>
            </dependency>
            <dependency>
                <groupId>io.github.openfeign</groupId>
                <artifactId>feign-slf4j</artifactId>
                <version>9.5.0</version>
            </dependency>
            <dependency>
                <groupId>io.github.openfeign</groupId>
                <artifactId>feign-hystrix</artifactId>
                <version>9.5.0</version>
            </dependency>
            <dependency>
                <groupId>io.github.openfeign</groupId>
                <artifactId>feign-jackson</artifactId>
                <version>9.5.0</version>
            </dependency>
        </dependencies>
    
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>
           

2.application.properties

server.port=9090

feign.client.url = http://localhost:8082/ssm_war
           

3.book 实体类

package com.cxb.springboot.entity;

import java.io.Serializable;

/**
 * 图书实体
 */
public class Book implements Serializable {

	private long bookId;// 图书ID

	private String name;// 图书名称

	private int number;// 馆藏数量

	public Book() {
	}

	public Book(long bookId, String name, int number) {
		this.bookId = bookId;
		this.name = name;
		this.number = number;
	}

	public long getBookId() {
		return bookId;
	}

	public void setBookId(long bookId) {
		this.bookId = bookId;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getNumber() {
		return number;
	}

	public void setNumber(int number) {
		this.number = number;
	}

	@Override
	public String toString() {
		return "Book [bookId=" + bookId + ", name=" + name + ", number=" + number + "]";
	}


}
           

4.BookConfig配置 这里配置了熔断机制,服务方出错,则会访问默认的接口

package com.cxb.springboot.feign;

import com.netflix.hystrix.HystrixCommandProperties;
import feign.hystrix.HystrixFeign;
import feign.hystrix.SetterFactory;
import feign.jackson.JacksonDecoder;
import feign.jackson.JacksonEncoder;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class BookConfig {

    @Value("${feign.client.url}")
    private String baseUrl;

    @Bean
    IBookClient nodeClient() throws InterruptedException {
        return HystrixFeign.builder()
                .decoder(new JacksonDecoder())
                .encoder(new JacksonEncoder())
                .setterFactory((target, method) ->
                        new SetterFactory.Default().create(target, method).
                                andCommandPropertiesDefaults(HystrixCommandProperties.defaultSetter().
                                        withExecutionTimeoutInMilliseconds(10000)))
                .target(IBookClient.class, this.baseUrl);
    }
}
           

5.IBookClient 这里指定需要访问的接口信息

package com.cxb.springboot.feign;

import com.cxb.springboot.entity.Book;
import feign.Headers;
import feign.Param;
import feign.RequestLine;

@Headers("Content-Type:application/json")
public interface IBookClient {

    @RequestLine("GET /book/getBookById?bookId={bookId}")
    Book getBookById(@Param("bookId") Long bookId);

}
           

6.HelloController 服务方失败的时候会调用该接口

package com.cxb.springboot.web;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

    @GetMapping()
    public String hello(){
        return "hello feign !";
    }
    
}
           

7.启动类,启动该项目

package com.cxb.springboot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringbootHelloApplication {

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

}
           

8.部署需要调用的接口并启动 https://github.com/ChenXbFrank/ssm/tree/devlop 下载代码

9.测试

http://localhost:8082/ssm_war/book/getBookById?bookId=1001

SpringBoot集成feign,优雅的调用其他项目的接口

接下来在springboot的项目中调用

http://localhost:9090/getBookById?bookId=1001

SpringBoot集成feign,优雅的调用其他项目的接口

测试出错的时候:http://localhost:9090/getBookById666?bookId=1001 接口调用失败,默认访问hello feign。

SpringBoot集成feign,优雅的调用其他项目的接口

springboot集成feign搞定!

项目下载地址:https://github.com/ChenXbFrank/springboot-feign

继续阅读