当前位置: 代码迷 >> 综合 >> 从零开始:Feign
  详细解决方案

从零开始:Feign

热度:39   发布时间:2023-11-27 01:04:22.0

一、工作环境

操作系统:Ubuntu 16.04

java环境:JDK 1.8

SpringBoot版本:1.5.13

SpringCloud版本:Edgware.SR3


二、项目配置

1. Eureka Client

feign(调用服务)和feign-client(被调用服务)应为两个项目,分别注册到eureka上,此处为了偷懒节约篇幅只写了关键的不同点

feign-pom.xml

<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

feign-client-pom.xml

<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

feign-application.yml

spring:application:name: feignserver:port: 8004eureka:client:serviceUrl:defaultZone: http://eureka-server:8761/eureka/

feign-client-application.yml

spring:application:name: feign-clientserver:port: 8005eureka:client:serviceUrl:defaultZone: http://eureka-server:8761/eureka/

FeignApplication.java

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
@EnableFeignClients  //调用服务比被调用服务多了这个注解
@EnableEurekaClient
public class FeignApplication {public static void main(String[] args) {SpringApplication.run(FeignApplication.class, args);}
}

FeignClientApplication.java

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;@SpringBootApplication
@EnableEurekaClient
public class FeignclientApplication {public static void main(String[] args) {SpringApplication.run(FeignclientApplication.class, args);}
}

2. feign 、feign-client

FeignClientController.java

@RestController
@RequestMapping("/client")
public class FeignClientController {@RequestMapping(value = "/getKeys", method = RequestMethod.GET)public String getKeys() {String keys = "123456789";return keys;}
}

FeignSeivice.java

@FeignClient注解中的值对应feignClient在Eureka上的服务名

import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;@FeignClient("feign-client")
public interface FeignClientService {@RequestMapping(value = "/client/getKeys", method = RequestMethod.GET)String getKeys();
}

FeignController.java

import com.example.feign.mvc.service.FeignClientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;@RestController
public class FeignController {@Autowiredprivate FeignClientService feignClientService;@RequestMapping(value = "/getKeys", method = RequestMethod.GET)public String getKeys() {return feignClientService.getKeys();}
}

三、测试

用postman输入FeignController的url


调用成功!可以看到实际上调用的是feign-client的controller,但是我们全程并没有写任何和RestTemplate有关的代码,仅用注解就实现了功能


四、参考

https://www.cnblogs.com/EddieBlog/p/8657073.html


小白所学尚浅,文章内容是根据参考+实践理解所得,如果有错误的地方欢迎指正!

  相关解决方案