微服务(声明式调用feign)

编程

相比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的集成

 

以上是 微服务(声明式调用feign) 的全部内容, 来源链接: utcz.com/z/513741.html

回到顶部