需求
- 在使用restTemplate访问远程接口的时候,我们难以将接口管理起来,当接口变动的时候我们可能会修改多处。所以,需要通过springcloud的openfegin来解决这个问题。
1.概念
OpenFeign是一种声明式、模板化的HTTP客户端。在Spring Cloud中使用OpenFeign,可以做到使用HTTP请求访问远程服务,就像调用本地方法一样的,开发者完全感知不到这是在调用远程方法,更感知不到在访问HTTP请求。
2.引入依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
<version>2.2.1.RELEASE</version>
</dependency>
3.创建启动类,并添加注解
@EnableFeignClients申明该项目是Feign客户端,扫描对应的feign client。
@EnableFeignClients
@EnableEurekaClient
@SpringBootApplication
public class OpenfeginOrder80 {
public static void main(String[] args) {
SpringApplication.run(OpenfeginOrder80.class,args);
}
}
4.创建API接口
我们需要集中化管理API,就可以通过接口统一管理,需要新增商品服务的接口类OpenFeginService ,然后添加@FeignClient(name = “cloud-payment-service”)注解,其中name表示的是我们要访问的服务名称。 @GetMapping("/v1payp/payment/get/{id}")和我们访问提供者的这个方法路径是一致的。
@Component
@FeignClient(name = "cloud-payment-service")
public interface OpenFeginService {
@GetMapping("/v1payp/payment/get/{id}")
@ApiOperation(value = "根据id查询payment",notes = "cloud-provider-payment8001.getPaymentById")
@ApiImplicitParam(name = "id", value = "payment的id", required = false, dataType = "String", paramType = "path")
public CommonResult<Payment> getPaymentById(@PathVariable("id") Long id);
}
5.修改controller
现在接口已经封装在OpenFeginService 里面了,现在我们不需要通过微服务名加地址去访问了。
import com.study.cloud.entities.CommonResult;
import com.study.cloud.entities.Payment;
import com.study.cloud.service.OpenFeginService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
@RestController
public class OpenfeginController {
@Resource
private OpenFeginService openFeginService;
@GetMapping("/consumer/payment/get/{id}")
public CommonResult<Payment> getPaymentByid(@PathVariable("id") Long id){
return openFeginService.getPaymentById(id);
}
}
6.测试访问结果
7.超时控制
直接在application.yaml上ribbon的相关信息, Readtimeout: 5000,指的是建立连接所用的时间,适用于网络正常情况下,两端连接所用的时间.ConnectTimeout: 5000,指的是建立连接后,从服务器读取资源所用的时间
eureka:
client:
#表示是否将自己注册金eurekaserver
register-with-eureka: true
#是否从eureka抓取自己的注册信息,默认为true,集群必须为true才能配合ribbon使用负载均衡
fetch-registry: true
service-url:
defaultZone: http://eureka7001.com:7001/eureka/, http://eureka7002.com:7002/eureka/
ribbon:
#指的是建立连接所用的时间,适用于网络正常情况下,两端连接所用的时间
Readtimeout: 5000
#指的是建立连接后,从服务器读取资源所用的时间
ConnectTimeout: 5000
8.日志控制
首先编写一个config类
import feign.Logger;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class FeginConfig {
@Bean
Logger.Level feginLoggerLevel(){
return Logger.Level.FULL;
}
}
然后在application.yaml文件中配置相关内容
eureka:
client:
#表示是否将自己注册金eurekaserver
register-with-eureka: true
#是否从eureka抓取自己的注册信息,默认为true,集群必须为true才能配合ribbon使用负载均衡
fetch-registry: true
service-url:
defaultZone: http://eureka7001.com:7001/eureka/, http://eureka7002.com:7002/eureka/
ribbon:
#指的是建立连接所用的时间,适用于网络正常情况下,两端连接所用的时间
Readtimeout: 5000
#指的是建立连接后,从服务器读取资源所用的时间
ConnectTimeout: 5000
logging:
level:
#fegin日志以什么级别监控哪个接口
com.study.cloud.service.OpenFeginService: debug
其中,logging下就是配置日志的相关内容