Nacos实现配置管理的:SpringCloud使用Nacos实现配置管理_生骨大头菜的博客-CSDN博客
之前使用 RestTemplate进行远程调用,但是RestTemplate存在一下问题:
org.springframework.cloud
spring-cloud-starter-openfeign
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);
}
}
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);
}
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;
}
}
Feign低层客户端默认使用URLConnection,不支持连接池,可以改为Apache HttpClient或者OKHttp,添加HttpClient的支持,引入依赖:
io.github.openfeign
feign-httpclient
编辑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并测试:
1.新建一个Module
2.添加feign依赖
org.springframework.boot
spring-boot-starter
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-starter-web
org.springframework.cloud
spring-cloud-starter-openfeign
io.github.openfeign
feign-httpclient
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
com.demo
feign-api
1.0-SNAPSHOT
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中