springCloud的feign使用

第一步:引入maven的依赖:

<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>

第二步:配置application.yml配置文件:

spring:
  application:
    name: feign-consumer
eureka:
  client:
    serviceUrl:
        defaultZone: http://localhost:1111/eureka/
server:
  port: 9001
fegin:
  compression:
    request:
      enable: true
    response:
      enable: true
logging:
  level:
    controller:
      HelloService: DEBUG

第三步:创建springboot的启动类,加上feign启动注解类@EnableFeignClients

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class ConsumerApplication {

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

  /*  @Bean
    Logger.Level feginLoggerLevel(){
        return Logger.Level.FULL;
    }*/
}
 
 

第四步:创建server接口,并在接口上使用@FeignClient注解,这里的fallback是接口的服务降级类,是一个实现HelloService的实现类,这样就能实现feign接口的服务降级

@FeignClient(value = "HELLO-SERVICE", fallback = HelloServiceFallback.class)
@Service
public interface HelloService {

    @RequestMapping("/hello")
    String hello();

    //这是请求的微服务接口/hello1是以form表单形式接受对象的请求,@RequestParam注解的value不能省略
    @RequestMapping(value = "/hello1", method = RequestMethod.GET )
     String hello(@RequestParam("name") String name);
  
  //请求的微服务接口/hello2是以请求头的方式接受参数,@RequestHeader注解的value不能省略
  @RequestMapping(value = "/hello2", method = RequestMethod.GET )
  User hello(@RequestHeader("name") String name, @RequestHeader("age") Integer age);
//请求的微服务接口/hello3是以body对象的方式接受 @RequestMapping(value = "/hello3", method = RequestMethod. POST) String hello( @RequestBody User user);}
 
 

@Component
public class HelloServiceFallback implements HelloService {
    @Override
    public String hello() {
        return "error";
    }

    @Override
    public String hello(@RequestParam("name") String name) {
        return "error";
    }

    @Override
    public User hello(@RequestHeader("name") String name, @RequestHeader("age") Integer age) {
        return new User("未知",0);
    }

    @Override
    public String hello(@RequestBody User user) {
        return "error";
    }

}

第五步:创建controller控制器,并注入接口service,调用微服务

@RestController
public class ConsumerController {

    @Autowired
    HelloService helloService;

    @LoadBalanced
    @Autowired
    public RestTemplate getRestTemplate(){
        return new RestTemplate();
    }


    @RequestMapping(value = "feign-consumer", method = RequestMethod.GET)
    public String helloConsumer(){
        System.out.println("feign-consumer start");
        return  helloService.hello();
    }

    @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();
    }

    @RequestMapping(value = "hello-world", method = RequestMethod.GET)
    public String helloWorld(){
        System.out.println("hello-world start");
        RestTemplate restTemplate = new RestTemplate();
        String s = (String) getRestTemplate().getForEntity("http://hello-service/hello1?name={name}",String.class,"test").getBody();
        return s;
    }

}

由于feign底层用的也是Ribbon框架和hystrix框架,所以Ribbon和hystrix的配置同样适用与feign

eg.

ribbon.ConnectTimeout=5000//全局配置

HELLO-SERVICE.ribbon.ConnectTimeout=5000//针对指定的客户端的配置

hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=5000//设置hystrix的全局超时时间

猜你喜欢

转载自blog.csdn.net/chengkui1990/article/details/80773297