Spring Cloud服务接入

此篇文章是介绍 springCloud 服务接入到 Apache ShenYu 网关,Apache ShenYu 网关使用 springCloud 插件来接入Spring Cloud服务。

接入前,请正确启动 shenyu-admin,并开启springCloud插件,在网关端和springCloud服务端引入相关依赖。可以参考前面的 Spring Cloud快速开始

应用客户端接入的相关配置请参考:客户端接入配置

数据同步的相关配置请参考:数据同步配置

在网关中引入 springCloud 插件

  • 在网关的 pom.xml 文件中引入如下依赖。
  1. <!-- apache shenyu springCloud plugin start-->
  2. <dependency>
  3. <groupId>org.apache.shenyu</groupId>
  4. <artifactId>shenyu-spring-boot-starter-plugin-springcloud</artifactId>
  5. <version>${project.version}</version>
  6. </dependency>
  7. <dependency>
  8. <groupId>org.apache.shenyu</groupId>
  9. <artifactId>shenyu-spring-boot-starter-plugin-httpclient</artifactId>
  10. <version>${project.version}</version>
  11. </dependency>
  12. <!-- apache shenyu springCloud plugin end-->
  13. <dependency>
  14. <groupId>org.springframework.cloud</groupId>
  15. <artifactId>spring-cloud-commons</artifactId>
  16. <version>2.2.0.RELEASE</version>
  17. </dependency>
  • 如果你使用 eureka 作为 springCloud的注册中心

    • 在网关的pom.xml文件中,新增如下依赖:
    1. <dependency>
    2. <groupId>org.springframework.cloud</groupId>
    3. <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    4. <version>2.2.0.RELEASE</version>
    5. </dependency>
  • 在网关的yml文件中,新增如下配置:

    1. eureka:
    2. client:
    3. serviceUrl:
    4. defaultZone: http://localhost:8761/eureka/ # 你的eureka地址
    5. instance:
    6. prefer-ip-address: true
  • 如果你使用 nacos 作为 springCloud的注册中心

    • 在网关的pom.xml文件中,新增如下依赖:
    1. <dependency>
    2. <groupId>com.alibaba.cloud</groupId>
    3. <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
    4. <version>2.1.0.RELEASE</version>
    5. </dependency>
  • 在网关的yml文件中 新增如下配置:

    1. spring:
    2. cloud:
    3. nacos:
    4. discovery:
    5. server-addr: 127.0.0.1:8848 # 你的nacos地址

特别提示:请确保spring Cloud注册中心服务发现配置开启

  • 配置方式
  1. spring:
  2. cloud:
  3. discovery:
  4. enabled: true
  • 代码方式
  1. @SpringBootApplication
  2. @EnableDiscoveryClient
  3. public class ShenyuBootstrapApplication {
  4. /**
  5. * Main Entrance.
  6. *
  7. * @param args startup arguments
  8. */
  9. public static void main(final String[] args) {
  10. SpringApplication.run(ShenyuBootstrapApplication.class, args);
  11. }
  12. }
  • 重启你的网关服务。

SpringCloud服务接入网关

可以参考:shenyu-examples-springcloud

  • 在由SpringCloud构建的微服务中,引入如下依赖:
  1. <dependency>
  2. <groupId>org.apache.shenyu</groupId>
  3. <artifactId>shenyu-spring-boot-starter-client-springcloud</artifactId>
  4. <version>${shenyu.version}</version>
  5. </dependency>
  • controller接口上加上 @ShenyuSpringCloudClient 注解。 注解可以加到类或方法上面,path属性为前缀,如果含有 /** 代表你的整个接口需要被网关代理。

  • 示例一: 代表 /test/payment, /test/findByUserId 都会被网关代理。

  1. @RestController
  2. @RequestMapping("/test")
  3. @ShenyuSpringCloudClient(path = "/test/**")
  4. public class HttpTestController {
  5. @PostMapping("/payment")
  6. public UserDTO post(@RequestBody final UserDTO userDTO) {
  7. return userDTO;
  8. }
  9. @GetMapping("/findByUserId")
  10. public UserDTO findByUserId(@RequestParam("userId") final String userId) {
  11. UserDTO userDTO = new UserDTO();
  12. userDTO.setUserId(userId);
  13. userDTO.setUserName("hello world");
  14. return userDTO;
  15. }
  16. }
  • 示例二: 代表 /order/save,会被网关代理,而/order/findById 则不会。
  1. @RestController
  2. @RequestMapping("/order")
  3. @ShenyuSpringCloudClient(path = "/order")
  4. public class OrderController {
  5. @PostMapping("/save")
  6. @ShenyuSpringMvcClient(path = "/save")
  7. public OrderDTO save(@RequestBody final OrderDTO orderDTO) {
  8. orderDTO.setName("hello world save order");
  9. return orderDTO;
  10. }
  11. @GetMapping("/findById")
  12. public OrderDTO findById(@RequestParam("id") final String id) {
  13. OrderDTO orderDTO = new OrderDTO();
  14. orderDTO.setId(id);
  15. orderDTO.setName("hello world findById");
  16. return orderDTO;
  17. }
  18. }
  • 示例三: isFulltrue 代表整个服务都会被网关代理。
  1. shenyu:
  2. register:
  3. registerType: http #zookeeper #etcd #nacos #consul
  4. serverLists: http://localhost:9095 #localhost:2181 #http://localhost:2379 #localhost:8848
  5. props:
  6. username: admin
  7. password: 123456
  8. client:
  9. springCloud:
  10. props:
  11. contextPath: /springcloud
  12. isFull: true
  13. # port: 8884
  14. # registerType : 服务注册类型,请参考应用客户端接入文档
  15. # serverList: 服务列表,请参考应用客户端接入文档
  16. # contextPath: 为你的项目在shenyu网关的路由前缀。 比如/order ,/product 等等,网关会根据你的这个前缀来进行路由。
  17. # appName:你的应用名称,不配置的话,会默认取application 中的名称
  18. # isFull: 设置true 代表代理你的整个服务,false表示代理你其中某几个controller
  1. @RestController
  2. @RequestMapping("/order")
  3. public class OrderController {
  4. @PostMapping("/save")
  5. @ShenyuSpringMvcClient(path = "/save")
  6. public OrderDTO save(@RequestBody final OrderDTO orderDTO) {
  7. orderDTO.setName("hello world save order");
  8. return orderDTO;
  9. }
  10. @GetMapping("/findById")
  11. public OrderDTO findById(@RequestParam("id") final String id) {
  12. OrderDTO orderDTO = new OrderDTO();
  13. orderDTO.setId(id);
  14. orderDTO.setName("hello world findById");
  15. return orderDTO;
  16. }
  17. }
  • 示例四:这是一种简化的使用方式,只需要一个简单的注解,使用元数据注册到网关。 特别说明:目前只支持@RequestMapping、@GetMapping、@PostMapping、@DeleteMapping、@PutMapping注解,并且只对@XXXMapping中的第一个路径有效。
  1. @RestController
  2. @RequestMapping("new/feature")
  3. public class NewFeatureController {
  4. /**
  5. * no support gateway access api.
  6. *
  7. * @return result
  8. */
  9. @RequestMapping("/gateway/not")
  10. public EntityResult noSupportGateway() {
  11. return new EntityResult(200, "no support gateway access");
  12. }
  13. /**
  14. * Do not use shenyu annotation path. used request mapping path.
  15. *
  16. * @return result
  17. */
  18. @RequestMapping("/requst/mapping/path")
  19. @ShenyuSpringCloudClient
  20. public EntityResult requestMappingUrl() {
  21. return new EntityResult(200, "Do not use shenyu annotation path. used request mapping path");
  22. }
  23. /**
  24. * Do not use shenyu annotation path. used post mapping path.
  25. *
  26. * @return result
  27. */
  28. @PostMapping("/post/mapping/path")
  29. @ShenyuSpringCloudClient
  30. public EntityResult postMappingUrl() {
  31. return new EntityResult(200, "Do not use shenyu annotation path. used post mapping path");
  32. }
  33. /**
  34. * Do not use shenyu annotation path. used post mapping path.
  35. *
  36. * @return result
  37. */
  38. @GetMapping("/get/mapping/path")
  39. @ShenyuSpringCloudClient
  40. public EntityResult getMappingUrl() {
  41. return new EntityResult(200, "Do not use shenyu annotation path. used get mapping path");
  42. }
  43. }
  • 启动你的服务成功注册后,进入后台管理系统的插件列表 -> rpc proxy -> springCloud,会看到自动注册的选择器和规则信息。

用户请求

和之前的访问方式没有大的改变,需要注意的是:

  • 你之前请求的域名是你自己的服务,现在要换成网关的域名。

  • 网关需要有一个路由前缀,这个路由前缀就是你接入项目进行配置 contextPath,可以在 shenyu-admin 中的 springCloud插件进行更改。

比如你有一个 order 服务 它有一个接口,请求路径 http://localhost:8080/test/save

现在就需要换成:http://localhost:9195/order/test/save

其中 localhost:9195 为网关的 ip 端口,默认端口是 9195/order 是你接入网关配置的 contextPath

其他参数,请求方式不变。然后你就可以进行访问了,如此的方便与简单。