189 8069 5689

SpringCloudGateway之服务注册与发现-创新互联

简介

上几篇主要讲解了网关在单个服务的使用,在实际的工作中,服务的相互调用都是依赖于服务中心提供的入口来使用,服务中心往往注册了很多服务,如果每个服务都需要单独配置的话,非常麻烦。Spring Cloud Gateway 提供了一种默认转发的能力,只要将 Spring Cloud Gateway 注册到服务中心,Spring Cloud Gateway 默认就会代理服务中心的所有服务,下面就具体讲解下。

创新互联服务项目包括石门网站建设、石门网站制作、石门网页制作以及石门网络营销策划等。多年来,我们专注于互联网行业,利用自身积累的技术优势、行业经验、深度合作伙伴关系等,向广大中小型企业、政府机构等提供互联网行业的解决方案,石门网站推广取得了明显的社会效益与经济效益。目前,我们服务的客户以成都为中心已经辐射到石门省份的部分城市,未来相信会继续扩大服务区域并继续获得客户的支持与信任!

工程介绍

本节案例中一共有四个工程,如下:

工程名端口作用
sc-eureka-server8760注册中心
sc-service-gateway8761路由网关
sc-service-hi8762服务提供者

工程详情

注册中心

pom.xml

    org.springframework.cloud
    spring-cloud-starter-netflix-eureka-server
application.yml
server:
  port: 8760

eureka:
  instance:
    hostname: localhost
  client:
    registerWithEureka: false
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

spring:
  application:
    name: sc-eurka-server

路由网关

pom.xml

    org.springframework.cloud
    spring-cloud-starter-gateway


    org.springframework.cloud
    spring-cloud-starter-netflix-eureka-client
application.yml
server:
  port: 8761

spring:
  application:
    name: sc-service-gateway
  cloud:
    gateway:
      discovery:
        locator:
          enabled: true
          lowerCaseServiceId: true

eureka:
  client:
    service-url:
      defaultZone: http://localhost:8760/eureka/

配置说明:

  • spring.cloud.gateway.discovery.locator.enabled:是否与服务注册于发现组件进行结合,通过 serviceId 转发到具体的服务实例。默认为 false,设为 true 便开启通过服务中心的自动根据 serviceId 创建路由的功能。
  • pring.cloud.gateway.discovery.locator.lowerCaseServiceId:是将请求路径上的服务名配置为小写(因为服务注册的时候,向注册中心注册时将服务名转成大写的了)。
  • eureka.client.service-url.defaultZone:指定注册中心的地址,以便使用服务发现功能。
  • logging.level.org.springframework.cloud.gateway:调整相 gateway 包的 log 级别,以便排查问题。

服务提供者

pom.xml

    org.springframework.boot
    spring-boot-starter-web


    org.springframework.cloud
    spring-cloud-starter-netflix-eureka-client
application.yml
server:
  port: 8762

spring:
  application:
    name: sc-service-hi

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8760/eureka/
启动类
@SpringBootApplication
@EnableEurekaClient
@RestController
public class ScServiceHiApplication {

    public static void main(String[] args) {
        SpringApplication.run( ScServiceHiApplication.class, args );
    }

    @Value("${server.port}")
    String port;

    @GetMapping("/hi")
    public String home(@RequestParam(value = "name", defaultValue = "zhangsan") String name) {
        return "hi " + name + " ,i am from port:" + port;
    }

}

启动三个项目后,访问 http://localhost:8761/sc-service-hi/hi?name=zhangsan~,返回如下:

hi zhangsan~ ,i am from port:8762

说明服务网关转发成功了。

自定义请求路径

在上面的例子中,向sc-service-gateway发送的请求时,url必须带上服务名sc-service-hi这个前缀,才能转发到sc-service-hi上,转发之前会将sc-service-hi去掉。有时服务名称过长,不易使用,需要自定义路径并转发到具体的服务上。配置如下:

server:
  port: 8761

spring:
  application:
    name: sc-service-gateway
  cloud:
    gateway:
      discovery:
        locator:
          enabled: false
          lowerCaseServiceId: true
      routes:
        - id: sc-service-hi
          uri: lb://SC-SERVICE-HI
          predicates:
          - Path=/demo/**
          filters:
          - StripPrefix=1

eureka:
  client:
    service-url:
      defaultZone: http://localhost:8760/eureka/

logging:
  level:
    org.springframework.cloud.gateway: debug

在上面的配置中,配置了一个Path 的 predict,将以/demo/**开头的请求都会转发到uri为lb://SC-SERVICE-HI的地址上,lb://SC-SERVICE-HI即sc-service-hi服务的负载均衡地址,并用StripPrefix的filter 在转发之前将/demo去掉。同时将spring.cloud.gateway.discovery.locator.enabled改为false,如果不改的话,之前的localhost:8761/sc-service-hi/hi?name=zhangsan~这样的请求地址也能正常访问,因为这时为每个服务创建了2个router。

重启sc-service-gateway项目后,访问 http://localhost:8761/demo/hi?name=zhangsan~ ,返回如下:

hi zhangsan~ ,i am from port:8762

服务网关转发成功,说明自定义请求路径生效了。

源码:https://github.com/gf-huanchupk/SpringCloudLearning/tree/master/chapter13

欢迎关注我的公众号《程序员果果》,关注有惊喜~~
Spring Cloud Gateway 之 服务注册与发现

创新互联www.cdcxhl.cn,专业提供香港、美国云服务器,动态BGP最优骨干路由自动选择,持续稳定高效的网络助力业务部署。公司持有工信部办法的idc、isp许可证, 机房独有T级流量清洗系统配攻击溯源,准确进行流量调度,确保服务器高可用性。佳节活动现已开启,新人活动云服务器买多久送多久。


本文题目:SpringCloudGateway之服务注册与发现-创新互联
文章地址:http://cdxtjz.cn/article/ceeojo.html

其他资讯