1. 程式人生 > >springBoot Feign

springBoot Feign

1.引入依賴包

<!-- 引入關於 eureka-server的依賴 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
            <version>2.0.2.RELEASE</version>
        </
dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> <version>2.0.2.RELEASE</version> </dependency>

2.主函式

@EnableEurekaClient
@EnableFeignClients

3.建立feign配置檔案

package com.example.eurekafeignclient.config;

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

import java.util.concurrent.TimeUnit;


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

4.建立介面

package com.example.eurekafeignclient;

import com.example.eurekafeignclient.config.feignConfig;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;


@FeignClient(value = "eureka-client", configuration = feignConfig.class)
@Service
public interface imp_eurekaClientFeign {
    @GetMapping(value = "/hello")
    String hello11();
}//end

5.建立controller呼叫

package com.example.eurekafeignclient.controller;

import com.example.eurekafeignclient.imp_eurekaClientFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class clientController {
    @Autowired
    imp_eurekaClientFeign imp_eurekaClientFeign_;
    @RequestMapping("/hello")
    public String hello() {
        return imp_eurekaClientFeign_.hello11();
    }
}