Nacos实现配置管理的:SpringCloud使用Nacos实现配置管理_生骨大头菜的博客-CSDN博客
之前使用 RestTemplate进行远程调用,但是RestTemplate存在一下问题:
- 代码可读性差,编程体验不统一
- 参数复杂URL难以维护
1、在order-service中引入依赖
<!--feign依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
2、在order-service的启动类中添加注解开启Feign功能
package com.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;
@EnableFeignClients
@SpringBootApplication
public class OrderApplication {
public static void main(String[] args) {
SpringApplication.run(OrderApplication.class, args);
}
}
3、编写Feign客户端
package com.demo.clients;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
@FeignClient("userservice")
public interface UserClient {
@GetMapping("/user/{id}")
String findById(@PathVariable String id);
}
4、controller中使用Feign客户端
package com.demo.controller;
import com.demo.clients.UserClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
/**
* TODO
*
* @Author: 冯健
* @Date: 2022/3/30
* @Description:
* @Modified By:
*/
@RestController
public class OrderController {
@Autowired
UserClient userClient;
@GetMapping("/order/{id}")
public String order(@PathVariable String id) {
// 这里访问user-service的微服务
String user = userClient.findById(id);
return "订单:" + id + "=====" + user;
}
}
5、重启服务并且测试
6、性能优化
Feign低层客户端默认使用URLConnection,不支持连接池,可以改为Apache HttpClient或者OKHttp,添加HttpClient的支持,引入依赖:
<!--feign的HttpClient支持-->
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-httpclient</artifactId>
</dependency>
编辑application.yml,配置连接池与日志,日志级别最好用basic或none
feign:
client:
config:
default: # default全局的配置
loggerLevel: BASIC # 日志级别,BASIC就是最基本的请求和响应信息
httpclient:
enabled: true # 开启feign对HttpClient的支持
max-connections: 200 # 最大的连接数
max-connections-per-route: 50 # 每个路径的最大连接数
logging:
level:
com.demo: debug
重启order-service并测试:
7、Feign的最佳实践,创建一个Feign功能的Module
1.新建一个Module
2.添加feign依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--feign依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<!--feign的HttpClient支持-->
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-httpclient</artifactId>
</dependency>
</dependencies>
3.添加相关client接口
package com.demo.feign.clients;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
@FeignClient("userservice")
public interface UserClient {
@GetMapping("/user/{id}")
String findById(@PathVariable String id);
}
4.无需创建application.yml文件,feign日志等相关配置需要写在order-service,否则不生效
5.在order-service引入feign-api
<!--引入feign的统一api-->
<dependency>
<groupId>com.demo</groupId>
<artifactId>feign-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
6.删除order-service中关于feign的引用,并且引用feign-api的相关类
package com.demo.controller;
import com.demo.feign.clients.UserClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class OrderController {
@Autowired
UserClient userClient;
@GetMapping("/order/{id}")
public String order(@PathVariable String id) {
// 这里访问user-service的微服务
String user = userClient.findById(id);
return "订单:" + id + "=====" + user;
}
}
7.重启,报错了
原因:当定义的FeignClient不在SpringBootApplication的扫描包范围时,这些FeignClient无法使用。有两种方式解决:
方式一:指定FeignClient所在包
@EnableFeignClients(basePackages = "com.demo.feign.clients")
方式二:指定FeignClient字节码
@EnableFeignClients(clients = {UserClient.class})
使用第二种:
package com.demo;
import com.demo.feign.clients.UserClient;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;
@EnableFeignClients(clients = UserClient.class)
@SpringBootApplication
public class OrderApplication {
public static void main(String[] args) {
SpringApplication.run(OrderApplication.class, args);
}
}
重新启动order-service,并且访问
注意事项:feign的相关配置需要写在order-service中,而不是feign-api中