-
(2)启动第二个product_service
-
(3)访问测试[http://localhost:9002/order/buy/1]( )
-
二、服务调用Feign高级
-
- 1、Feign的配置
-
2、请求压缩
-
3、打印Feign的日志
-
4、源码分析
-
三、高并发问题
-
- 1、创建工程
-
2、模拟环境
-
- (1)设置tomcat最大线程数
-
(2)设置product_service延时返回数据
-
(3)运行启动所有的工程测试
-
3、微服务架构的高并发问题
-
- (1)性能工具Jmetter
-
- 1)安装Jmetter
-
2) 配置Jmetter
-
3)测试
-
4) 上述问题分析
-
(2 )系统负载过高存在的问题(解决上述问题)
-
- 1) 问题分析
-
2)线程池的形式实现服务隔离
-
3)运行测试
Java之 Spring Cloud 微服务搭建 Feign组件(第二个阶段)【一】【SpringBoot项目实现商品服务器端是调用】
========================================================================
前面我们使用的RestTemplate实现REST API调用,代码大致如下:
@RequestMapping(value = “/buy/{id}”, method = RequestMethod.GET)
public Product findById(@PathVariable Long id) {
//如何调用商品服务?
return restTemplate.getForObject(“http://service-product/product/”+id,Product.class);
}
由代码可知,我们是使用拼接字符串的方式构造URL的,该URL只有一个参数。
但是,在现实中,URL中往往含有多个参数。
这时候我们如果还用这种方式构造URL,那么就会非常痛苦。那应该如何解决?
我们带着这样的问题进入到本章的学习。
Feign是Netflix开发的声明式,模板化的HTTP客户端,其灵感来自Retrofit,JAXRS-2.0以及WebSocket.
-
Feign可帮助我们更加便捷,优雅的调用HTTP API。
-
在SpringCloud中,使用Feign非常简单——创建一个接口,并在接口上添加一些注解,代码就完成了。
-
Feign支持多种注解,例如Feign自带的注解或者JAX-RS注解等。
-
SpringCloud对Feign进行了增强,使Feign支持了SpringMVC注解,并整合了Ribbon和- Eureka,从而让Feign的使用更加方便。
(1)引入依赖
在我们上节课程当中对应的第一个项目当中spring_cloud_demo
在order_service
当中的pom.xml
引入依赖
org.springframework.cloud
spring-cloud-starter-openfeign
(2)创建Feign相关接口
package cn.itbluebox.order.feign;
import cn.itbluebox.order.entity.Product;
import org.bouncycastle.cert.ocsp.Req;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import javax.persistence.Id;
/*
-
ProductFeignClient上声明需要调用的微服务名称
-
@FeignClient
-
name: 服务提供者的名称
*/
@FeignClient(name = “service-product”)
public interface ProductFeignClient {
/*
配置需要调用的微服务接口
*/
@RequestMapping(value = “/product/{id}”, method = RequestMethod.GET)
public Product findById(@PathVariable(“id”) Long id);
}
上述的操作配置相当于之前的微服务调用的时候的
restTemplate.getForObject(“http://service-product/product/”+id,Product.class);
(3)启动类激活FeignClient
@SpringBootApplication
@EntityScan(“cn.itbluebox.order.entity”)
@EnableFeignClients
public class OrderApplication {
/*
使用Spring提供的 RestTemplate 发送http请求到商品服务
1、创建RestTemplate对象交给容器管理
2、在使用的时候,调用其方法完成操作(getXX,postXX)