第一步:修改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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.ck.springcloud</groupId> <artifactId>feign-consumer</artifactId> <version>1.0-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.3.7.RELEASE</version> <relativePath/> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </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>Brixton.SR5</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> </project>
第二步:resources目录下增加配置文件appilcation.yml了
spring: application: name: feign-consumer eureka: client: serviceUrl: defaultZone: http://localhost:1111/eureka/ server: port: 9001
第三步:增加启动类:
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.netflix.feign.EnableFeignClients; @SpringBootApplication @EnableDiscoveryClient @EnableFeignClients public class ConsumerApplication { public static void main(String[] args) { SpringApplication.run(ConsumerApplication.class, args); } }
第四步:编写调用服务提供者的service
import org.springframework.cloud.netflix.feign.FeignClient; import org.springframework.web.bind.annotation.*; //value是eureka服务注册中心上服务提供者的服务名称,HelloServiceFallback是服务降级的处理类,可以去了 @FeignClient(value = "HELLO-SERVICE", fallback = HelloServiceFallback.class) public interface HelloService { @RequestMapping("/hello") String hello(); //RequestParam和RequestHeader注解的名字必须写 @RequestMapping(value = "/hello1", method = RequestMethod.GET ) String hello(@RequestParam("name") String name); @RequestMapping(value = "/hello2", method = RequestMethod.GET ) User hello(@RequestHeader("name") String name, @RequestHeader("age") Integer age); @RequestMapping(value = "/hello3", method = RequestMethod.POST) String hello(@RequestBody User user); }
第五步:编写测试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.RestController; @RestController public class ConsumerController { @Autowired HelloService helloService; @RequestMapping(value = "feign-consumer2", method = RequestMethod.GET) public String helloConsumer2(){ StringBuffer sb = new StringBuffer(); sb.append(helloService.hello()).append("\n"); sb .append(helloService.hello("DIDI")).append("\n"); sb .append(helloService.hello("DIDI",20)).append("\n"); sb .append(helloService.hello(new User("DIDI",20))); return sb.toString(); } }