一 环境准备:
首先好一套简单的springcloud基本框架(zhangpba-springcloud)
公共代码:study-common
注册中心:study-eureka 端口:8815
文件服务:study-file 端口:8816
用户服务:study-user 端口:8817
1 其中study-user外露两个接口,外露接口的代码:
http://127.0.0.1:8817/client/getFile?name=名称参数【利用feign调用study-file的外露接口(getHost)】
http://127.0.0.1:8817/client/postFile 【利用feign调用study-file的外露接口(postHost)】
package com.study.user.controller; import com.study.vo.User; import com.study.user.feign.FileServiceFeign; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; /** * 测试feign接口:fegin接口消费方 * * @author zhangpba */ @RestController public class FeignTestController { private static final Logger logger = LoggerFactory.getLogger(FeignTestController.class); @Autowired private FileServiceFeign fileServiceFeign; /** * 测试post请求:消费方 * * @param user 用户参数 * @return */ @RequestMapping(value = "/client/postFile", method = RequestMethod.POST) public String postFile(User user) { return fileServiceFeign.postFileHost(user); } /** * 测试get请求:消费方 * * @param name 参数 * @return */ @RequestMapping(value = "/client/getFile", method = RequestMethod.GET) public String getFile(String name) { return fileServiceFeign.getFileHost(name); } }
2 study-file外露两个接口,外露接口的代码:
http://127.0.0.1:8816/getFileHost?name=名称参数
http://127.0.0.1:8816/postFileHost?name=名称参数
package com.study.file.controller; import com.study.vo.User; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.web.bind.annotation.*; /** * 测试feign接口:fegin接口服务提供方 * * @author zhangpba */ @RestController public class FeginTestController { private static final Logger logger = LoggerFactory.getLogger(FeginTestController.class); /** * 测试feign接口的get请求:服务提供方 * * @param name * @return */ @RequestMapping(value = "/getFileHost", method = RequestMethod.GET) public String getFileHost(@RequestParam("name") String name) { logger.info("进入feign服务提供者:getFileHost"); return "我是file服务get请求返回的数据:" + name; } /** * 测试feign接口的post请求:服务提供方 * * @param user * @return */ @RequestMapping(value = "/postFileHost", method = RequestMethod.POST, produces = "application/json; charset=UTF-8") public String postFileHost(@RequestBody User user) { logger.info("进入feign服务提供者:postFileHost"); return "我是file服务post请求返回的数据: " + user; } }
二 网关服务准备
增加网关服务:study-zuul 端口号:8888
搭建一个springboot服务,注册到study-eureka中,并添加zuul的包
pom.xml
<!--2021-06-01 zuul网关--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-zuul</artifactId> </dependency> <!--zuul网关的重试机制,不是使用ribbon内置的重试机制是借助spring-retry组件实现的重试 开启zuul网关重试机制需要增加下述依赖--> <dependency> <groupId>org.springframework.retry</groupId> <artifactId>spring-retry</artifactId> </dependency>
启动类 StudyZuulApplication
package com.study.zuul; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.cloud.netflix.zuul.EnableZuulProxy; /** * @author zhangpba * @EnableZuulProxy - 开启Zuul网关。 * 当前应用是一个Zuul微服务网关。会在Eureka注册中心中注册当前服务。并发现其他的服务。 * Zuul需要的必要依赖是spring-cloud-starter-zuul。 */ @SpringBootApplication @EnableEurekaClient @EnableDiscoveryClient @EnableZuulProxy public class StudyZuulApplication { public static void main(String[] args) { SpringApplication.run(StudyZuulApplication.class, args); } }
三 测试
3.1 测试网关的代理功能
配置增加,请求‘http://127.0.0.1:8888/api/user/client/getFile?name=测试zuul’,可以看到直接请求的是zuul的端口8888,但是可以请求到study-file中,说明网关代理了study-user,而study-user又调用了study-file,所以此时网关已经代理了配置文件中的study-user的外露接口
zuul: prefix: /api # 配置请求路径前缀,所有基于此前缀的请求都由zuul网关提供代理 routes: study-user-service.path: /user/** # 使用路径方式匹配路由规则 study-user-service.serviceId: study-user # 使用服务名称匹配
study-file打印的日志如下:
2021-06-02 23:03:37.368 INFO 7312 --- [nio-8816-exec-4] c.s.file.controller.FeginTestController : 进入feign服务提供者:getFileHost
3.2 测试网关的路由功能
3.2.1 配置file路由,请求‘http://127.0.0.1:8888/api/file/getFileHost?name=测试zuul’,网关可以能代理study-file的外露接口
zuul: prefix: /api # 配置请求路径前缀,所有基于此前缀的请求都由zuul网关提供代理 routes: study-user-service.path: /user/** # 使用路径方式匹配路由规则 study-user-service.serviceId: study-user # 使用服务名称匹配 study-file-service.path: /file/** # 使用路径方式匹配路由规则 study-file-service.serviceId: study-file # 使用服务名称匹配
study-file输出的日志如下:
2021-06-02 23:20:20.083 INFO 7312 --- [nio-8816-exec-2] c.s.file.controller.FeginTestController : 进入feign服务提供者:getFileHost
3.2.2 不配置file路由,请求‘http://127.0.0.1:8888/api/file/getFileHost?name=测试zuul’,返回404找不到路径,说明网关不能代理study-file的外露接口。
zuul: prefix: /api # 配置请求路径前缀,所有基于此前缀的请求都由zuul网关提供代理 routes: study-user-service.path: /user/** # 使用路径方式匹配路由规则 study-user-service.serviceId: study-user # 使用服务名称匹配 # study-file-service.path: /file/** # 使用路径方式匹配路由规则 # study-file-service.serviceId: study-file # 使用服务名称匹配
请求结果如下
因此,配置为:http://ip:port/api/appservice/**的请求提供zuul网关代理,可以将要访问服务进行前缀分类