17. Declarative REST Client: Feign
Feign是一个声明式的web service client17.1. How to Include Feign
eureka服务器端口号是8761
服务提供者microservice-provider-user端口号是7900
服务消费者8011
pom.xml
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactI d>spring-cloud-starter-openfeign</artifactId>
</dependency>
服务提供者访问地址http://localhost:7900/demo/dd
启动类注解
@EnableEurekaClient
@SpringBootApplication
@EnableFeignClients
Feign接口
package com.ldgx.eshop.feign;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@FeignClient("microservice-provider-user")//服务提供者spring.application.name=microservice-provider-user
public interface UserFeignClient {
@RequestMapping(value="/demo/dd",method = RequestMethod.GET)//服务提供者的访问路径是/demo/dd
public String dd();
}
在controller里面使用
@Autowired
private UserFeignClient client;//调用Feign
17.2. Overriding Feign Defaults
注意:配置文件Configuration1 不能在@ComponentScan可以扫描的范围下面.如下面的配置文件,启动类是
com/ldgx/eshop/MicroserviceCustomerMovie1Application.java
配置文件不在启动类的同包和子包下面
package com.ldgx.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import feign.Contract;
@Configuration
public class Configuration1 {
/**
* feignContract:Defines what annotations and values are valid on interfaces. *
* @return 用的注解方式
*/
@Bean
public Contract feignContract() {
return new feign.Contract.Default();//feign.Contract.Default()的注解方法举例:@RequestLine("GET /repos/{owner}/{repo}/contributors")
}
}
feign
package com.ldgx.eshop.feign;
import org.springframework.cloud.netflix.feign.FeignClient;
import com.ldgx.config.Configuration1;
import feign.RequestLine;
@FeignClient(name = "microservice-provider-user",configuration = Configuration1.class)//服务提供者spring.application.name=microservice-provider-user
public interface UserFeignClient {
//@RequestMapping(value="/demo/dd",method = RequestMethod.GET)//服务提供者的访问路径是/demo/dd
@RequestLine("GET /demo/dd")
public String dd();
}
如果还用@RequestMapping(value="/demo/dd",method = RequestMethod.GET)会报错:
Caused by: java.lang.IllegalStateException: Method dd not annotated with HTTP method type (ex. GET, POST)
查看具体的注解(new feign.Contract.Default()),所以打开https://github.com/OpenFeign/feign查看
例子2,还是在原来的代码基础上
如果eureka服务端的地址是http://localhost:8761。
那么可以访问http://localhost:8761/eureka/apps/microservice-provider-user
microservice-provider-user是注册到eureka的服务提供者
Feign,访问改地址
package com.ldgx.eshop.feign;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
@FeignClient(name="xxxx",url = "http://localhost:8761")//eureka服务器地址是http://localhost:8761
public interface FeignClient2 {
@RequestMapping(value="/eureka/apps/{serviceName}")//url的访问路径是http://localhost:8761/eureka/apps/microservice-provider-user
public String findServiceInfoFromEurekaByServiceName(@PathVariable("serviceName") String serviceName);
}
controller
@RestController
public class UseDemoController {
@Autowired
private FeignClient2 client2;
@GetMapping("/{serviceName}")
public String findServiceInfoFromEurekaByServiceName(@PathVariable String serviceName) {
return client2.findServiceInfoFromEurekaByServiceName(serviceName);
}
}
17.3. Creating Feign Clients Manually
在application.yml中增加配置
feign.hystrix.enabled: true
17.4. Feign Hystrix Support
启动类增加注解
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
@EnableCircuitBreaker
package com.ldgx.eshop.feign;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@FeignClient(name="microservice-provider-user", fallback = HystrixClientFallback.class)//服务提供者spring.application.name=microservice-provider-user
public interface UserFeignClient {
@RequestMapping(value="/demo",method = RequestMethod.GET)//服务提供者的访问路径是/demo
public String dd();
}
@Component
class HystrixClientFallback implements UserFeignClient {//断路器
@Override
public String dd() {
// TODO Auto-generated method stub
return "===feign fall back===";
//return null;
}
}
17.5. Feign Hystrix Fallbacks
17.6. Feign and @Primary
17.7. Feign Inheritance Support
17.8. Feign request/response compression
17.9. Feign logging