HM-SCAli3.3【Fegin】
1 what is Fegin ?
- Feign是Spring Cloud提供的一个声明式的伪Http客户端, 它使得调用远程服务就像调用本地服务一样简单, 只需要创建一个接口并添加一个注解即可。
- Nacos很好的兼容了Feign, Feign默认集成了 Ribbon, 所以在Nacos下使用Fegin默认就实现了负载均衡的效果。
2 how to use Fegin ?
1 加入Fegin的依赖
<!--fegin组件-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
2 在主类上添加Fegin的注解
3 创建一个service接口, 并使用Fegin实现微服务调用
package com.yppah.service;
import com.yppah.domain.Product;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
@FeignClient(value = "service-product") //value用于指定调用nacos下的哪个微服务
public interface ProductService {
@RequestMapping("/product/{pid}")
Product findByPid(@PathVariable Integer pid);
}
4 修改controller代码
5 重启order微服务,查看效果