官方文档:
1、gateway-ha 提供服务
<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.yzm</groupId> <artifactId>springcloud</artifactId> <version>0.0.1-SNAPSHOT</version> <relativePath>../pom.xml</relativePath> </parent> <artifactId>gateway-ha</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>gateway-ha</name> <description>Demo project for Spring Boot</description> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <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-openfeign</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build></project>@EnableFeignClients@EnableEurekaClient@SpringBootApplicationpublic class GatewayHaApplication { public static void main(String[] args) { SpringApplication.run(GatewayHaApplication.class, args); }}server: port: 8021spring: application: name: gateway-haeureka: client: serviceUrl: defaultZone: http://localhost:8088/eureka/# 定制小程序开发启动熔断降级feign: hystrix: enabled: true
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
Controller
@RestControllerpublic class HaController { @Resource private HaFeign haFeign; @Value("${server.port}") private String port; @GetMapping("hello") public String hello(@RequestParam String name) { return "ha," + name + " ! " + "定制小程序开发定制小程序开发访问端口号:" + port; } @GetMapping("ha") public String ha(@RequestParam String name) { return haFeign.callHi(name); }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
Service
@FeignClient(value = "gateway-hi", fallback = HaBack.class)public interface HaFeign { @GetMapping("hello") String callHi(@RequestParam String name);}@Servicepublic class HaBack implements HaFeign { @Override public String callHi(String name) { return "gateway-hi 服务挂了"; }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
2、gateway-hi 提供服务
<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.yzm</groupId> <artifactId>springcloud</artifactId> <version>0.0.1-SNAPSHOT</version> <relativePath>../pom.xml</relativePath> </parent> <artifactId>gateway-hi</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>gateway-hi</name> <description>Demo project for Spring Boot</description> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <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-openfeign</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build></project>@EnableFeignClients@EnableEurekaClient@SpringBootApplicationpublic class GatewayHiApplication { public static void main(String[] args) { SpringApplication.run(GatewayHiApplication.class, args); }}server: port: 8022spring: application: name: gateway-hieureka: client: serviceUrl: defaultZone: http://localhost:8088/eureka/feign: hystrix: enabled: true
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
Controller
@RestControllerpublic class HiController { @Resource private HiFeign hiFeign; @Value("${server.port}") private String port; @GetMapping("hello") public String hello(@RequestParam String name) { return "hi," + name + " ! " + "访问端口号:" + port; } @GetMapping("hi") public String hi(@RequestParam String name) { return hiFeign.callHa(name); }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
Service
@FeignClient(value = "gateway-ha", fallback = HiBack.class)public interface HiFeign { @GetMapping("hello") String callHa(@RequestParam String name);}@Servicepublic class HiBack implements HiFeign { @Override public String callHa(String name) { return "gateway-ha 服务挂了"; }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
3、gateway-route 网关服务-路由转发
<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.yzm</groupId> <artifactId>springcloud</artifactId> <version>0.0.1-SNAPSHOT</version> <relativePath>../pom.xml</relativePath> </parent> <artifactId>gateway-route</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>gateway-route</name> <description>Demo project for Spring Boot</description> <dependencies> <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-gateway</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build></project>@EnableEurekaClient@SpringBootApplicationpublic class GatewayRouteApplication { public static void main(String[] args) { SpringApplication.run(GatewayRouteApplication.class, args); }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
application.yml
server: port: 8023eureka: client: serviceUrl: defaultZone: http://localhost:8088/eureka/spring: application: name: gateway-route cloud: gateway: discovery: locator: enabled: true # serviceId是否小写 lowerCaseServiceId: true
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
通过gateway定制小程序开发实现路由转发有两种方式:
方式一:通过代码配置类实现
@Configurationpublic class RouteConfig { @Bean public RouteLocator customerRouteLocator(RouteLocatorBuilder builder) { return builder.routes() .route("to_gateway_ha", r -> r .path("/ha/**") .filters(f -> f. stripPrefix(1) ) .uri("http://localhost:8021/") ) .route(r -> r .path("/gw-ha/**", "/gw-a/**") .filters(f -> f. stripPrefix(1) ) .uri("lb://gateway-ha") ) .build(); }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
to_gateway_ha:路由ID,具备全局唯一性,可以不指定,UUID。
stripPrefix(1):去掉前缀,1表示个数,如路由前请求/a/b/c/d,去掉1个前缀后请求/b/c/d再转发
uri():可以指定详细地址(不能负载),也可以指定服务ID
依次启动eureka、ha、hi和route服务
访问
http://localhost:8023/gateway-ha/helloname=yzm
http://localhost:8023/gateway-hi/helloname=yzm
这是网关的默认路由规则
访问
http://localhost:8023/gw-a/helloname=yzm
http://localhost:8023/gw-ha/helloname=yzm
http://localhost:8023/ha/helloname=yzm
这是自定义的路由规则
方式二:通过配置文件yml实现
spring: application: name: gateway-route cloud: gateway: discovery: locator: enabled: true lowerCaseServiceId: true routes: - id: gw_ha uri: http://localhost:8021/ predicates: - Path=/hhha/* filters: - StripPrefix=1 - id: to_gateway_hi uri: lb://gateway-hi predicates: - Path=/hi/* filters: - StripPrefix=1
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
重启route服务
访问
http://localhost:8023/hhha/helloname=yzm
http://localhost:8023/hi/helloname=yzm
路由转发简单实现完成!
4、 断言,即路由条件
Path 路径匹配
routes: - id: path_route uri: lb://gateway-ha predicates: - Path=/ha
- 1
- 2
- 3
- 4
- 5
Method 请求方式
routes: - id: method_route uri: lb://gateway-ha predicates: - Method=POST
- 1
- 2
- 3
- 4
- 5
Before 某时间点之前
routes: - id: time_route uri: lb://gateway-ha predicates: - Before=2022-03-10T16:00:22.432+08:00[Asia/Shanghai] #在该时区前发生
- 1
- 2
- 3
- 4
- 5
After 某时间点之后
routes: - id: time_route uri: lb://gateway-ha predicates: - After=2022-03-10T15:00:22.432+08:00[Asia/Shanghai] #在该时区后发生
- 1
- 2
- 3
- 4
- 5
Between 两时间点之间
routes: - id: time_route uri: lb://gateway-ha predicates: - Between=2022-01-10T15:00:22.432+08:00[Asia/Shanghai],2022-10-10T16:00:22.432+08:00[Asia/Shanghai] #在两个时区内发生
- 1
- 2
- 3
- 4
- 5
Header 请求头
routes: - id: header_route uri: lb://gateway-ha predicates: - Header=id,001 #携带header信息,相当于键值对,id为key,001为value - Header=id,d+ #携带header信息,相当于键值对,id为key,d+为value,是一个正则表达式,表达为正数
- 1
- 2
- 3
- 4
- 5
- 6
Cookie
routes: - id: cookie_route uri: lb://gateway-ha predicates: - Cookie=username,yzm #携带cookies信息,相当于键值对,username为key,yzm为value - Cookie=username,d+ #携带cookies信息,相当于键值对,username为key,d+为value
- 1
- 2
- 3
- 4
- 5
- 6
Host 域名
routes: - id: host_route uri: lb://gateway-ha predicates: - Host=**.baidu.com,**.another.com
- 1
- 2
- 3
- 4
- 5
Query 查询条件
routes: - id: query_route uri: lb://gateway-ha predicates: - Query=number,123 #带查询条件,第一个是查询参数名称,第二个是可选的值,有参数名为number且值=123 - Query=number #带查询条件,有参数名为number,值无所谓 - Query=number,d+
- 1
- 2
- 3
- 4
- 5
- 6
- 7
RemoteAddr 远程地址
routes: - id: raddr_route uri: lb://gateway-ha predicates: - RemoteAddr=192.168.1.1/24
- 1
- 2
- 3
- 4
- 5
server: port: 8024eureka: client: serviceUrl: defaultZone: http://localhost:8088/eureka/spring: application: name: gateway-predicate cloud: gateway: discovery: locator: enabled: true lowerCaseServiceId: true routes: #Path - id: path_route uri: lb://gateway-ha predicates: - Path=/ha #Method - id: method_route uri: lb://gateway-ha predicates: - Method=POST #Header - id: header_route uri: lb://gateway-ha predicates: - Header=id,d+ #携带header信息,相当于键值对,id为key,d+为value,是一个正则表达式,表达为正数 #Cookie - id: cookie_route uri: lb://gateway-ha predicates: - Cookie=username,d+ #携带cookies信息,相当于键值对,username为key,d+为value #Host - id: host_route uri: lb://gateway-ha predicates: - Host=**.baidu.com,**.another.com #Query - id: query_route uri: lb://gateway-ha predicates: - Query=number,d+ #Time# - id: time_route# uri: lb://gateway-ha# predicates:# - After=2020-10-30T15:00:22.432+08:00[Asia/Shanghai] #在该时区后发生# - Before=2020-10-30T16:00:22.432+08:00[Asia/Shanghai] #在该时区前发生# - Between=2020-10-30T15:00:22.432+08:00[Asia/Shanghai],2020-10-30T16:00:22.432+08:00[Asia/Shanghai] #在两个时区内发生
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
在gateway-ha服务中添加接口
@PostMapping("post") public String post() { System.out.println("post"); return "post"; } @GetMapping("time") public String time() { System.out.println("time"); return "time"; } @GetMapping("/header") public String header(HttpServletRequest request) { Enumeration<String> headerNames = request.getHeaderNames(); while (headerNames.hasMoreElements()) { String name = headerNames.nextElement(); System.out.println(name + " = " + request.getHeader(name)); } return "header"; } @GetMapping("/cookie") public String cookie(HttpServletRequest request) { Cookie[] cookies = request.getCookies(); for (Cookie cookie : cookies) { System.out.println(cookie.getName() + " = " + cookie.getValue()); } return "cookie"; } @GetMapping("/host") public String host(HttpServletRequest request) { Enumeration<String> headerNames = request.getHeaderNames(); while (headerNames.hasMoreElements()) { String name = headerNames.nextElement(); System.out.println(name + " = " + request.getHeader(name)); } return "host"; } @GetMapping("/query") public String query(Integer number) { System.out.println(number); return "query"; }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
依次启动eureka、ha、predicates服务
使用curl访问
curl -XPOST localhost:8024/post
curl localhost:8024/header -H “id:111”
curl localhost:8024/cookie --cookie “username=222”
curl localhost:8024/host -H “host:a.another.com”
curl localhost:8024/querynumber=333
相关链接