1.微服务集群
1.为什么要集群
提高并发量
2.结构
3.服务者提供集群
服务提供者集群 同一个服务部署多份(上线) 拷贝一份一样改端口(测试学习)
4.服务消费者负载均衡调用
1.有哪些技术
ribbon
feign
2常见的负载均衡策略
轮休
可用性检查
权重
3.ribbon
@RestController
public class OrderController {
@Autowired
private RestTemplate restTemplate;
//通过服务名来完成调用
private final String URL_PREFIX="http://product-service/product/";
//暴露通过id远程查询product的接口
@GetMapping("/order/product/{id}")
public Product getProductByid(@PathVariable("id")Long id){
String utl=URL_PREFIX+id;//要拼接地址,加入很长
//期望以接口的方式进行调用
return restTemplate.getForObject(utl, Product.class);
}
4.feign
入门类扫描client
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients如果你的client就在该类的子子孙孙包就不用指定包名,否则可以指定报名
//加了它以后,就会扫描加了@FeignClient这个注解的接口,并且为这些接口产生代理对象
//并且把这些代理对象纳入spring管理,我们要使用时直接获取就可以完成远程调用
public class OrderService9003Application {
public static void main(String[] args) {
SpringApplication.run(OrderService9003Application.class,args);
}
}
client代码
@FeignClient(value = "product-service")//里面所有的方法都要调用PRODUCT-SERVCIE这个服务
@RequestMapping("/product")
public interface ProductClient {
@GetMapping("/{id}")//回调托底
public Product getProductById(@PathVariable(name = "id")Long id);
}
2. hystrix
1.是什么
微服务架构处理服务健壮性的框架
2.为什么用它
解决微服务架构的雪崩线下
3.措施
隔离&熔断&降级
hystrix使用
1.服务提供者实现ribbon
导包
<dependencies>
<!--公共依赖-->
<dependency>
<groupId>com.sx</groupId>
<version>1.0-SNAPSHOT</version>
<artifactId>product_interface</artifactId>
</dependency>
<!--springboot支持-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<!--eureka客户端支持 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!--断路器-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
</dependencies>
入口类
@SpringBootApplication
@EnableEurekaClient
@EnableHystrix //开启Hystrix,熔断和降解才会生效
public class ProductService8003Application {
public static void main(String[] args) {
SpringApplication.run(ProductService8003Application.class,args);
}
}
3.feign
feign熔断机制是封装hystrix
1.导包
<dependencies>
<!--公共依赖-->
<dependency>
<groupId>com.sx</groupId>
<version>1.0-SNAPSHOT</version>
<artifactId>product_interface</artifactId>
</dependency>
<!--springboot支持-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<!-- Eureka客户端 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!--feign的支持-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
</dependencies>
入口类
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
public class OrderService9004Application {
public static void main(String[] args) {
SpringApplication.run(OrderService9004Application.class,args);
}
}
productClient
@FeignClient(value = "product-service",fallbackFactory = ProductClientFallbackFactory.class)//里面所有的方法都要调用PRODUCT-SERVCIE这个服务
@RequestMapping("/product")
public interface ProductClient {
@GetMapping("/{id}")//回调托底
public Product getProductById(@PathVariable(name = "id")Long id);
}
ProductClientFallbackFactory
//这个工程类要翻写应给ProductClient的托底类
@Component
public class ProductClientFallbackFactory implements FallbackFactory<ProductClient> {
public ProductClient create(Throwable throwable) {
return new ProductClient(){
public Product getProductById(Long id) {
Product product = new Product();
product.setId(id);
product.setName("系统发生故障");
return product;
}
};
}
}
配置类
@Configuration
public class CfgBean {
@Bean
@LoadBalanced
public RestTemplate restTemplate(){
return new RestTemplate();
}
@Bean
public IRule myRule(){
return new RandomRule();//随机
}
}
3.zuul
1.是什么
为外部访问提供统一的入口,并且我们同可以通过过滤完成过渡,并且封装负载均衡ribbon,封装了熔断hystrix
4.2.2.导入pom
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency>
4.2.3.修改yml
server:
port: 4399
spring:
application:
name: ZUUL-GATEWAY
eureka:
client:
service-url:
defaultZone: http://localhost:7001/eureka #集群环境配置需要改一下
instance:
instance-id: gateway-9527.com
prefer-ip-address: false
4.2.4.修改入口类
4.2.5.启动测试
Eureka 服务提供者 zuul
启动一个服务的提供者:
直接访问地址:http://localhost:8001/user/provider/1
路由访问地址:http://localhost:4399/user-provider(小写)/user/provider/1
4.3.路由访问映射规则
1) 安全加固:不用服务名,映射路径
2) 忽略原来服务名访问:原来模式不可以访问
3) 加上统一前缀
zuul:
routes:
myUser.serviceId: user-provider # 服务名
myUser.path: /myUser/** # 把myUser打头的所有请求都转发给user-provider
ignored-services: “*” #所有服务都不允许以服务名来访问
prefix: “/services” #加一个统一前缀
4.4.过滤器
Zuul作为网关的其中一个重要功能,就是实现请求的鉴权。而这个动作我们往往是通过Zuul提供的过滤器来实现的。
4.4.1.ZuulFilter
ZuulFilter是过滤器的顶级父类。在这里我们看一下其中定义的4个最重要的方法:
- shouldFilter:返回一个Boolean值,判断该过滤器是否需要执行。返回true执行,返回false不执行。
- run:过滤器的具体业务逻辑。
- filterType:返回字符串,代表过滤器的类型。包含以下4种:
- pre:请求在被路由之前执行
- routing:在路由请求时调用
- post:在routing和errror过滤器之后调用
- error:处理请求时发生错误调用
- filterOrder:通过返回的int值来定义过滤器的执行顺序,数字越小优先级越高。
4.4.2.过滤器执行周期
这张是Zuul官网提供的请求生命周期图,清晰的表现了一个请求在各个过滤器的执行顺序。
- 正常流程:
- 请求到达首先会经过pre类型过滤器,而后到达routing类型,进行路由,请求就到达真正的服务提供者,执行请求,返回结果后,会到达post过滤器。而后返回响应。
- 异常流程:
- 整个过程中,pre或者routing过滤器出现异常,都会直接进入error过滤器,再error处理完毕后,会将请求交给POST过滤器,最后返回给用户。
- 如果是error过滤器自己出现异常,最终也会进入POST过滤器,而后返回。
- 如果是POST过滤器出现异常,会跳转到error过滤器,但是与pre和routing不同的时,请求不会再到达POST过滤器了。
4.4.3.使用场景
场景非常多:
- 请求鉴权:一般放在pre类型,如果发现没有访问权限,直接就拦截了
- 异常处理:一般会在error类型和post类型过滤器中结合来处理。
- 服务调用时长统计:pre和post结合使用。
4.4.4.自定义过滤器
接下来我们来自定义一个过滤器,模拟一个登录的校验。基本逻辑:如果请求中有access-token参数,则认为请求有效,放行。
1)自定义过滤器
@Component
public class LoginFilter extends ZuulFilter{
@Override
public String filterType() {
// 登录校验,肯定是在前置拦截
return “pre”;
}
@Override
public int filterOrder() {
// 顺序设置为1
return 1;
}
@Override
public boolean shouldFilter() {
// 返回true,代表过滤器生效。
return true;
}
@Override
public Object run() throws ZuulException {
// 登录校验逻辑。
// 1)获取Zuul提供的请求上下文对象
RequestContext ctx = RequestContext.getCurrentContext();
// 2) 从上下文中获取request对象
HttpServletRequest req = ctx.getRequest();
// 3) 从请求中获取token
String token = request.getHeader("token");
// 4) 判断
if(token == null || "".equals(token.trim())){
// 没有token,登录校验失败,拦截
ctx.setSendZuulResponse(false);
// 返回401状态码。也可以考虑重定向到登录页。
ctx.setResponseStatusCode(HttpStatus.UNAUTHORIZED.value());
}
// 校验通过,可以考虑把用户信息放入上下文,继续向后执行
return null;
}
}
2)测试 postman
没有token参数时,访问失败:
4.config server
为什么需要
配置文件统一管理
1.准备github配置文件
2.通过configserver读取配置文件
3.写一个configclient真正读取配置文件的地方