spring cloud alibaba nacos整合gateway

項目父工程spring boot版本為2.2.2,spring cloud版本為Hoxton.SR1,
spring cloud alibaba版本為2.1.0。版本最佳搭配詳見官網(wǎng)。
項目大致結(jié)構(gòu)


image.png
  1. gateway模塊引入依賴
<!-- nacos -->
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-configuration-processor</artifactId>
    <scope>provided</scope>
</dependency>

  1. 配置yml


    image.png
spring:
  profiles:
    #    active: dev
    active: @profiles.active@

  application:
    name: glorious-gateway
  cloud:
    nacos:
      discovery: #Nacos注冊中心地址
        server-addr: 127.0.0.1:8848
    gateway:
      discovery:
        locator:
          enabled: true  #開啟注冊中心路由功能
      #          lower-case-service-id: true
      routes:
        - id: mini-api
          uri: lb://glorious-mini-api
          predicates:
            - Path=/mini/**
          filters:
            - StripPrefix=1
        - id: system-api
          uri: lb://glorious-system-api
          predicates:
            - Path=/system/**
          filters:
            - StripPrefix=1

啟動類加上發(fā)現(xiàn)服務(wù)注解

@EnableDiscoveryClient
  1. 啟動網(wǎng)關(guān)gateway以及其他服務(wù)模塊,然后登錄nacos,服務(wù)已經(jīng)發(fā)現(xiàn)


    image.png

查看swagger


image.png

訪問網(wǎng)關(guān)接口數(shù)據(jù),登錄返回token

image.png

訪問微服務(wù)接口數(shù)據(jù)


image.png

關(guān)于nacos安裝我之前博客介紹過,感興趣的可以看看,詳情

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容