创建api-gateway-server
org.springframework.cloud spring-cloud-starter-gateway org.springframework.cloud spring-cloud-starter-netflix-eureka-client
package com.itheima.geteway;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;@SpringBootApplication
@EnableEurekaClient
public class APiGatewayApp {public static void main(String[] args) {SpringApplication.run(APiGatewayApp.class, args);}
}
server:port: 80spring:application:name: api-gateway-servercloud:#网关配置gateway:#路由配置:转发规则routes: #集合# id 唯一标识 默认是UUID# uri 转发路径# predicates 条件,用于请求网关路径的匹配规则- id: gateway-provideruri: http://localhost:8001/predicates:- Path=/goods/**
server:
port: 80
spring:
application:
name: api-gateway-server
cloud:
#网关配置
gateway:
#路由配置:转发规则
routes: #集合
# id 唯一标识 默认是UUID
# uri 转发路径
# predicates 条件,用于请求网关路径的匹配规则
- id: gateway-provider
uri: http://localhost:8001/ #静态路由
predicates:
- Path=/goods/**
- id: gateway-consumeruri: http://localhost:9000/ #静态路由predicates:- Path=/order/**
spring:application:name: api-gateway-servercloud:#网关配置gateway:#路由配置:转发规则routes: #集合# id 唯一标识 默认是UUID# uri 转发路径# predicates 条件,用于请求网关路径的匹配规则- id: gateway-provider#uri: http://localhost:8001/#动态路由uri: lb://GATEWAY-PROVIDERpredicates:- Path=/goods/**- id: gateway-consumer#uri: http://localhost:9000/uri: lb://GATEWAY-CONSUMERpredicates:- Path=/order/**#设置eureka服务端的路径
eureka:client:service-url:defaultZone: http://localhost:8761/eureka
spring: cloud: discovery: locator: enabled: true # 开启微服发现功能 lower-case-service-id: true # 讲请求路径上的服务名配置为小写
server:port: 80spring:application:name: api-gateway-servercloud:#网关配置gateway:#路由配置:转发规则routes: #集合# id 唯一标识 默认是UUID# uri 转发路径# predicates 条件,用于请求网关路径的匹配规则- id: gateway-provider#uri: http://localhost:8001/#动态路由uri: lb://GATEWAY-PROVIDERpredicates:- Path=/goods/**- id: gateway-consumer#uri: http://localhost:9000/uri: lb://GATEWAY-CONSUMERpredicates:- Path=/order/**#微服务名称配置discovery:locator:enabled: true #设置为true 请求路径前可以添加微服务名称lower-case-service-id: true #允许为小写#设置eureka服务端的路径
eureka:client:service-url:defaultZone: http://localhost:8761/eureka
server:port: 80spring:application:name: api-gateway-servercloud:#网关配置gateway:#路由配置:转发规则routes: #集合# id 唯一标识 默认是UUID# uri 转发路径# predicates 条件,用于请求网关路径的匹配规则# filters: 配置局部过滤器- id: gateway-provider#uri: http://localhost:8001/#动态路由uri: lb://GATEWAY-PROVIDERpredicates:- Path=/goods/**filters:- AddRequestParameter=username,zhangsan #key value- id: gateway-consumer#uri: http://localhost:9000/uri: lb://GATEWAY-CONSUMERpredicates:- Path=/order/**#微服务名称配置discovery:locator:enabled: true #设置为true 请求路径前可以添加微服务名称lower-case-service-id: true #允许为小写#设置eureka服务端的路径
eureka:client:service-url:defaultZone: http://localhost:8761/eureka
package com.itheima.provider.controller;import com.itheima.provider.domain.Goods;
import com.itheima.provider.service.GoodsService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import java.util.Date;/*** Goods Controller 服务提供方*/@RestController
@RequestMapping("/goods")
public class GoodsController {@Autowiredprivate GoodsService goodsService;@Value("${server.port}")private int port;/*** 降级:* 1. 出现异常* 2. 服务调用超时* * 默认1s超时** @HystrixCommand(fallbackMethod = "findOne_fallback")* fallbackMethod:指定降级后调用的方法名称*/@GetMapping("/findOne/{id}")@HystrixCommand(fallbackMethod = "findOne_fallback",commandProperties = {//设置Hystrix的超时时间,默认1s@HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds",value = "3000"),//监控时间 默认5000 毫秒@HystrixProperty(name="circuitBreaker.sleepWindowInMilliseconds",value = "5000"),//失败次数。默认20次@HystrixProperty(name="circuitBreaker.requestVolumeThreshold",value = "20"),//失败率 默认50%@HystrixProperty(name="circuitBreaker.errorThresholdPercentage",value = "50")})public Goods findOne(@PathVariable("id") int id,String username){System.out.println(username);//如果id == 1 ,则出现异常,id != 1 则正常访问if(id == 1){//1.造个异常int i = 3/0;}/*try {//2. 休眠2秒Thread.sleep(2000);} catch (InterruptedException e) {e.printStackTrace();}*/Goods goods = goodsService.findOne(id);goods.setTitle(goods.getTitle() + ":" + port);//将端口号,设置到了 商品标题上return goods;}/*** 定义降级方法:* 1. 方法的返回值需要和原方法一样* 2. 方法的参数需要和原方法一样*/public Goods findOne_fallback(int id, String username){Goods goods = new Goods();goods.setTitle("降级了~~~");return goods;}}
执行http://localhost/goods/findOne/2 打印
package com.itheima.geteway.filter;import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;@Component
public class MyFilter implements GlobalFilter, Ordered {@Overridepublic Mono filter(ServerWebExchange exchange, GatewayFilterChain chain) {System.out.println("自定义全局过滤器执行了~~~");return chain.filter(exchange); ///放行}/*** 过滤器排序* @return 数值越小,越先执行*/@Overridepublic int getOrder() {return 0;}
}
下一篇:Vue核心(阶段性总结)