pom.xml
4.0.0 com.ggr feign 0.0.1-SNAPSHOT jar springcloudService-feign Demo project for Spring Boot org.springframework.boot spring-boot-starter-parent 2.0.2.BUILD-SNAPSHOT UTF-8 UTF-8 1.8 Finchley.BUILD-SNAPSHOT org.springframework.cloud spring-cloud-starter-netflix-eureka-server org.springframework.cloud spring-cloud-starter-openfeign org.springframework.boot spring-boot-starter-test test org.springframework.cloud spring-cloud-dependencies ${spring-cloud.version} pom import org.springframework.boot spring-boot-maven-plugin spring-snapshots Spring Snapshots https://repo.spring.io/snapshot true spring-milestones Spring Milestones https://repo.spring.io/milestone false spring-snapshots Spring Snapshots https://repo.spring.io/snapshot true spring-milestones Spring Milestones https://repo.spring.io/milestone false
App
package com.ggr.feign;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.client.discovery.EnableDiscoveryClient;import org.springframework.cloud.openfeign.EnableFeignClients;@SpringBootApplication@EnableDiscoveryClient@EnableFeignClientspublic class SpringcloudServiceFeignApplication { public static void main(String[] args) { SpringApplication.run(SpringcloudServiceFeignApplication.class, args); }}
Controller
package com.ggr.feign.controller;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.RequestParam;import org.springframework.web.bind.annotation.RestController;@RestControllerpublic class HiController { @Autowired HystrixClient schedualServiceHi; @RequestMapping(value = "/hi",method = RequestMethod.GET) public String sayHi(@RequestParam String name){ return schedualServiceHi.sayHiFromClientOne(name); }}
FeignClient
package com.ggr.feign.controller;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-hi",fallback=HystrixClientFallbackFactory.class)public interface HystrixClient { @RequestMapping(value = "/hi",method = RequestMethod.GET) String sayHiFromClientOne(@RequestParam(value = "name") String name);}
application.yml
eureka: client: serviceUrl: defaultZone: http://localhost:9000/eureka/server: port: 9005spring: application: name: service-feign