微服务-(声明式调用feign)

寵の児 提交于 2020-02-27 08:39:21

声明式调用 Fegin

一.初步认知fegin

相比ribbon更方便,简化了代码了,代理了请求。整合了hystrix。

二.写一个feign

三.fegin的应用

pom

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

yml

server:
  port: 8083

spring:
  application:
    name: feign-consumer
eureka:
  client:
    service-url:
      defaultZone : http://127.0.0.1:10000/eureka/

feign:
  hystrix:
    enabled: true


hystrix:
  command:
    default:
      execution:
        isolation:
          thread :
            timeoutInMilliseconds: 100

客户端注解


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;

@SpringBootApplication
@EnableFeignClients //启用feign,如果说需要启动默认配置,就这么写,如果需要覆盖就写成(@EnableFeignClients(defaultConfiguration = "FooConfiguartion.class"))
public class FeignDemoApplication {

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

}

import com.dn.feigndemo.fallback.HelloDemoFallback;
import com.dn.feigndemo.model.Teacher;
import feign.Body;
import feign.Param;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

@Component
@FeignClient(name="HELLOCLIENT",fallback = HelloDemoFallback.class)
public interface HelloDemoService {

    @RequestMapping(value = "",method = RequestMethod.GET)
    public Object getTeacher();

    @RequestMapping(value = "/user",method = RequestMethod.GET)
    public Teacher getByTeacher(@RequestParam("id")String id);

    @Body("%7B\"orderNo\": \"{orderNo}\"%7D")
    @RequestMapping(value = "/user",method = RequestMethod.POST)
    public int addOrder(@Param("orderNo")String orderNo);

    @RequestMapping(value = "/user",method = RequestMethod.POST)
    public int addTeacher(@Param("orderNo")String orderNo);

}

1.模板 2.设置请求头

package com.dn.feigndemo.service;

import com.dn.feigndemo.model.Teacher;
import feign.Headers;
import feign.Param;
import org.springframework.web.bind.annotation.RequestMapping;

@Headers("Accept:appliaction/json") //当前接口下,所有的feign请求都会被设置这个头
public interface FeginHeaderDemo {

    @Headers("Content-Type:appliaction/json")
    @RequestMapping("")
    int addOrder(Teacher teacher);

    @Headers("Token:{token}")//设置动态值
    @RequestMapping("")
    Teacher getOrder(@Param("token")String token);
}

四.fegin的工作流程

五.fegin与hystrix的集成

六.fegin和ribbon的集成

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!