微服务与中间件系列——Feign
Feign
Feign的引入使得我们能够解决:
- 远程调用可读性差,编程体验不统一
- 参数复杂URL难以维护
- 负载均衡的实现
Feign是一个声明式的http客户端,官方地址: https://github.com/OpenFeign/feign其作用就是帮助我们优雅的实现http请求的发送,解决上面提到的问题
QuickStart
准备工作
构建如下微服务项目
引入使用Nacos
如果你对这个地方有问题,请参照:微服务与中间件系列——Nacos快速使用
1.引入依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
2.完成service模块业务
2.1 编写TestService接口
public interface TestService {
void test();
}
2.2编写TestServiceImpl
@Service
public class TestServiceImpl implements TestService {
@Override
public void test() {
System.out.println("test....");
}
}
2.3编写TestController
@RestController
public class TestController {
@Autowired
private TestService testService;
@GetMapping("/service/test/{id}")
public String test(