目录
Long userId = orderById.getUserId();
String url = "http://userservice/users/getUserById/" + userId;
User userById = http.getForObject(url, User.class);
✏️ 代码可读性差、编程体验不统一
✏️ 当发送网络请求时的请求参数特别复杂的时候,URL 难以维护
✏️ Github 地址:https://github.com/OpenFeign/feign
✏️ Feign 是一个声明式的 HTTP 客户端
✏️ 可帮助开发者优雅地发送 HTTP 请求
✏️ 依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
✏️ 启动开关 @EnableFeignClients
@EnableFeignClients // 开启声明式 HTTP 客户端 Feign 的使用
@MapperScan("com.gq.order.mapper")
@SpringBootApplication
public class OrderApplication {
public static void main(String[] args) {
SpringApplication.run(OrderApplication.class, args);
}
}
✏️ 编写 Feign 客户端(声明远程调用的信息)
基于 SpringMVC 注解声明远程调用的信息:
① 服务名称:userservice
② 请求方式:GET
③ 请求路径:/user/{id}
④ 请求参数:Long id
⑤ 返回值类型:User
@FeignClient("userservice") // 服务名称
public interface UserFeignClient {
@GetMapping("/users/getUserById/{id}")
User getById(@PathVariable String id);
}
@Service
@Transactional
public class OrderServiceImpl implements OrderService {
@Resource
private OrderMapper orderMapper;
@Resource
private UserFeignClient userFeignClient;
/**
* 根据订单 id 查询订单
*/
@Transactional(readOnly = true)
@Override
public Order getOrderById(Long orderId) {
Order orderById = orderMapper.getOrderById(orderId);
if (orderById != null) {
Long userId = orderById.getUserId();
// 使用 Feign 发起远程调用
User userById = userFeignClient.getById(userId + "");
orderById.setUser(userById);
}
return orderById;
}
}

☘️ 一般修改的都是日志级别
全局配置:
feign:
client:
config:
default: # default 是全局配置(若写服务名称, 则是针对该微服务的配置)
loggerLevel: FULL # 日志级别
局部配置:
feign:
client:
config:
userservice: # 写服务名称表示只针对该微服务的配置
loggerLevel: FULL # 日志级别
必须配置下面的配置结合 Feign 的日志配置才能有效果
logging:
level:
com.gq: debug
📖 创建 FeignClientConfiguration
/**
* 对 Feign 的配置(该配置类在 Feign 相关的注解中使用)
*/
public class FeignClientConfiguration {
@Bean
public Logger.Level feignLogLevel() {
return Logger.Level.FULL;
}
}
📖 若是全局配置,将 FeignClientConfiguration 配置类放在 @EnableFeignClients 注解中
// 全局配置 Feign 的日志级别
@EnableFeignClients(defaultConfiguration = FeignClientConfiguration.class)
@MapperScan("com.gq.order.mapper")
@SpringBootApplication
public class OrderApplication {
public static void main(String[] args) {
SpringApplication.run(OrderApplication.class, args);
}
}
📖 若是局部配置,将 FeignClientConfiguration 配置类放在 @FeignClient 注解中
// 局部配置 Feign 的日志级别
@FeignClient(value = "userservice", configuration = FeignClientConfiguration.class)
public interface UserFeignClient {
@GetMapping("/users/getUserById/{id}")
User getById(@PathVariable String id);
}
📔 Feign 底层的客户端实现方案:
① URLConnection: 默认实现(不支持连接池)
② Apache HttpClient:支持连接池
③ OKHttp:支持连接池
📔 以上 OKHttp、URLConnection 和
Apache HttpClient是几种发送 HTTP 请求的客户端工具
📔 不使用连接池性能会很差
🍒 Feign 性能优化主要包括两个方面:
① 使用支持连接池的 HTTP 请求客户端 (如 Apache HttpClient、OKHttp) 代替 URLConnection
② 日志级别最好使用 BASIC或 NONE【debug 的时候才用 FULL】
🎁 添加 HttpClient 依赖
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-httpclient</artifactId>
</dependency>
🎁 在 yaml 文件配置连接池
feign:
client:
config:
default:
loggerLevel: BASIC # 打印基本的请求和响应信息
httpclient:
enabled: true # 开启 Feign 对 HttpClient 的支持
max-connections: 168
max-connections-per-route: 39 # 每个路径的最大连接数
🎄 给消费者的 FeignClient 和提供者的 Controller 定义统一的父接口作为标准

🎄 将 Feign 抽取为独立的模块

当定义的 FeignClient 不在 SpringBootApplication 的扫描包范围时,FeignClient 将无法使用(解决方法如下)
🚀 指定 FeignClient 所在包
@EnableFeignClients(basePackages = {"com.guoqing.feign"})

🚀 指定 FeignClient 字节码
@EnableFeignClients(clients = {UserFeignClient.class})
是的,我知道最好使用webmock,但我想知道如何在RSpec中模拟此方法:defmethod_to_testurl=URI.parseurireq=Net::HTTP::Post.newurl.pathres=Net::HTTP.start(url.host,url.port)do|http|http.requestreq,foo:1endresend这是RSpec:let(:uri){'http://example.com'}specify'HTTPcall'dohttp=mock:httpNet::HTTP.stub!(:start).and_yieldhttphttp.shou
我正在使用active_admin,我在Rails3应用程序的应用程序中有一个目录管理,其中包含模型和页面的声明。时不时地我也有一个类,当那个类有一个常量时,就像这样:classFooBAR="bar"end然后,我在每个必须在我的Rails应用程序中重新加载一些代码的请求中收到此警告:/Users/pupeno/helloworld/app/admin/billing.rb:12:warning:alreadyinitializedconstantBAR知道发生了什么以及如何避免这些警告吗? 最佳答案 在纯Ruby中:classA
我目前正在使用以下方法获取页面的源代码:Net::HTTP.get(URI.parse(page.url))我还想获取HTTP状态,而无需发出第二个请求。有没有办法用另一种方法做到这一点?我一直在查看文档,但似乎找不到我要找的东西。 最佳答案 在我看来,除非您需要一些真正的低级访问或控制,否则最好使用Ruby的内置Open::URI模块:require'open-uri'io=open('http://www.example.org/')#=>#body=io.read[0,50]#=>"["200","OK"]io.base_ur
这篇文章是继上一篇文章“Observability:从零开始创建Java微服务并监控它(一)”的续篇。在上一篇文章中,我们讲述了如何创建一个Javaweb应用,并使用Filebeat来收集应用所生成的日志。在今天的文章中,我来详述如何收集应用的指标,使用APM来监控应用并监督web服务的在线情况。源码可以在地址 https://github.com/liu-xiao-guo/java_observability 进行下载。摄入指标指标被视为可以随时更改的时间点值。当前请求的数量可以改变任何毫秒。你可能有1000个请求的峰值,然后一切都回到一个请求。这也意味着这些指标可能不准确,你还想提取最小/
1.错误信息:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:requestcanceledwhilewaitingforconnection(Client.Timeoutexceededwhileawaitingheaders)或者:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:TLShandshaketimeout2.报错原因:docker使用的镜像网址默认为国外,下载容易超时,需要修改成国内镜像地址(首先阿里
Rails中有没有一种方法可以提取与路由关联的HTTP动词?例如,给定这样的路线:将“users”匹配到:“users#show”,通过:[:get,:post]我能实现这样的目标吗?users_path.respond_to?(:get)(显然#respond_to不是正确的方法)我最接近的是通过执行以下操作,但它似乎并不令人满意。Rails.application.routes.routes.named_routes["users"].constraints[:request_method]#=>/^GET$/对于上下文,我有一个设置cookie然后执行redirect_to:ba
我正在使用Heroku(heroku.com)来部署我的Rails应用程序,并且正在构建一个iPhone客户端来与之交互。我的目的是将手机的唯一设备标识符作为HTTPheader传递给应用程序以进行身份验证。当我在本地测试时,我的header通过得很好,但在Heroku上它似乎去掉了我的自定义header。我用ruby脚本验证:url=URI.parse('http://#{myapp}.heroku.com/')#url=URI.parse('http://localhost:3000/')req=Net::HTTP::Post.new(url.path)#boguspara
我试图在我的网站上实现使用Facebook登录功能,但在尝试从Facebook取回访问token时遇到障碍。这是我的代码:ifparams[:error_reason]=="user_denied"thenflash[:error]="TologinwithFacebook,youmustclick'Allow'toletthesiteaccessyourinformation"redirect_to:loginelsifparams[:code]thentoken_uri=URI.parse("https://graph.facebook.com/oauth/access_token
我是Ruby的新手。我试过查看在线文档,但没有找到任何有效的方法。我想在以下HTTP请求botget_response()和get()中包含一个用户代理。有人可以指出我正确的方向吗?#PreliminarycheckthatProggitisupcheck=Net::HTTP.get_response(URI.parse(proggit_url))ifcheck.code!="200"puts"ErrorcontactingProggit"returnend#Attempttogetthejsonresponse=Net::HTTP.get(URI.parse(proggit_url)
我正在尝试解析网页,但有时会收到404错误。这是我用来获取网页的代码:result=Net::HTTP::getURI.parse(URI.escape(url))如何测试result是否为404错误代码? 最佳答案 像这样重写你的代码:uri=URI.parse(url)result=Net::HTTP.start(uri.host,uri.port){|http|http.get(uri.path)}putsresult.codeputsresult.body这将打印状态码和正文。