spring boot3 一、 spring alibaba cloud 整合 网关 nacos config

在这里插入图片描述

jm-apis服务
jm-user-api用户服务
jm-apis-common公共模块
jm-apis-common-bean公共bean
jm-apis-common-conf公共配置
jm-apis-common-tool公共工具
jm-dubbo-apisRPC服务
jm-dubbo-user-api用户RPC服务

jianmu-springboot3-springalibabacloud pom.xml

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.jm</groupId>
    <artifactId>jianmu-springboot3-springalibabacloud</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>
    <modules>
        <module>jm-gateway</module>
        <module>jm-apis</module>
        <module>jm-apis-common</module>
        <module>jm-dubbo-apis</module>
    </modules>

    <properties>
        <java.version>17</java.version>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    </properties>

    <dependencies>
        <!--        日志-->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
        </dependency>

    </dependencies>

    <dependencyManagement>
       <dependencies>
           <!--        日志-->
           <dependency>
               <groupId>org.slf4j</groupId>
               <artifactId>slf4j-api</artifactId>
               <version>2.0.6</version>
           </dependency>

           <dependency>
               <groupId>org.springframework.boot</groupId>
               <artifactId>spring-boot-dependencies</artifactId>
               <version>3.0.0</version>
               <type>pom</type>
               <scope>import</scope>
           </dependency>

           <dependency>
               <groupId>com.alibaba.cloud</groupId>
               <artifactId>spring-cloud-alibaba-dependencies</artifactId>
               <version>2022.0.0.0-RC1</version>
               <type>pom</type>
               <scope>import</scope>
           </dependency>

           <!--            spring cloud-->
           <dependency>
               <groupId>org.springframework.cloud</groupId>
               <artifactId>spring-cloud-dependencies</artifactId>
               <version>2022.0.0</version>
               <type>pom</type>
               <scope>import</scope>
           </dependency>
       </dependencies>
    </dependencyManagement>

</project>

jm-apis-common pom.xml

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>jianmu-springboot3-springalibabacloud</artifactId>
        <groupId>org.jm</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>jm-apis-common</artifactId>
    <packaging>pom</packaging>
    <modules>
        <module>jm-apis-common-conf</module>
        <module>jm-apis-common-tool</module>
        <module>jm-apis-common-bean</module>
    </modules>

    <properties>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-undertow</artifactId>
        </dependency>
    </dependencies>

</project>

jm-apis pom.xml

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>jianmu-springboot3-springalibabacloud</artifactId>
        <groupId>org.jm</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>jm-apis</artifactId>
    <packaging>pom</packaging>
    <modules>
        <module>jm-user-api</module>
    </modules>

    <properties>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
    </properties>


    <dependencies>
        <dependency>
            <groupId>org.jm</groupId>
            <artifactId>jm-apis-common</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
        <!--        config-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bootstrap</artifactId>
        </dependency>

    </dependencies>

</project>

jm-gateway 网关 pom.xml

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>jianmu-springboot3-springalibabacloud</artifactId>
        <groupId>org.jm</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>jm-gateway</artifactId>

    <properties>
        <java.version>17</java.version>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-alibaba-sentinel-gateway</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba.csp</groupId>
            <artifactId>sentinel-datasource-nacos</artifactId>
            <version>2.0.0-alpha</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bootstrap</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway</artifactId>
        </dependency>

    </dependencies>

</project>

nacos 配置中心

在这里插入图片描述

jm-gateway bootstrap.yml

spring:
  application:
    name: jm-gateway
  main:
    allow-bean-definition-overriding: true
  profiles:
    active: dev

jm-gateway bootstrap-dev.yml

spring:
  cloud:
    nacos:
      server-addr: localhost:8848
      discovery:
        namespace: f5d53f37-d171-43c7-9b77-01c64eeae769
      config:
        namespace: f5d53f37-d171-43c7-9b77-01c64eeae769
        file-extension: yaml
        extension-configs:
          - dataId: jm-gateway.yaml
            refresh: true

jm-gateway.yaml

server:
  port: 9700
spring:
    gateway:
      routes:
        - id: jm-user-api
          uri: lb://jm-user-api
          filters:
            - StripPrefix=1
          predicates:
            - Path=/user/**

      discovery:
        locator:
          #开启从注册中心动态创建路由的功能
          enabled: true
          #使用小写服务名,默认是大写
          lower-case-service-id: true
    
logging:
  level:
    root: info
    web: info
    com.jm: debug
  file:
    name: jm-gateway.log

jm-user-api bootstrap.yml

spring:
  profiles:
    active: dev
  application:
    name: jm-user-api

jm-user-api bootstrap-dev.yml

spring:
  cloud:
    nacos:
      server-addr: localhost:8848
      discovery:
        namespace: f5d53f37-d171-43c7-9b77-01c64eeae769
      config:
        namespace: f5d53f37-d171-43c7-9b77-01c64eeae769
        extension-configs:
          - dataId: jm-user-api.yaml
            refresh: true  

jm-user-api.yaml

server:
  port: 9001
logging:
  level:
    root: info
    web: info
    com.jm: debug

在这里插入图片描述

完整代码请看项目git地址

https://gitee.com/sunuping/jianmu-example-jdk17.git

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
根据提供的引用内容,springcloud nacos gateway knife的问题可能是关于如何在Spring Cloud中使用Nacos作为注册中心和配置中心,以及如何引入Knife4j的相关依赖的。下面是一个可能的回答: 在你的Spring Cloud项目中,你可以通过引入Nacos的相关依赖来将Nacos作为注册中心和配置中心。具体来说,你需要在pom.xml中引入以下依赖: ```xml <!-- Nacos注册中心依赖 --> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> <version>${nacos.version}</version> </dependency> <!-- Nacos配置中心依赖 --> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId> <version>${nacos.version}</version> </dependency> ``` 请在pom.xml中添加上述依赖以启用Nacos作为注册中心和配置中心。 另外,如果你还想在服务和网关中使用Knife4j,你需要在pom.xml中引入以下依赖: ```xml <!-- Knife4j依赖 --> <dependency> <groupId>com.github.xiaoymin</groupId> <artifactId>knife4j-spring-boot-starter</artifactId> <version>2.0.3</version> </dependency> ``` 请在pom.xml中添加上述依赖以启用Knife4j。 此外,你还需要在配置文件中进行相应的配置。例如,你可以在application.yml或application.properties中添加以下配置: ```yaml spring: profiles: active: dev application: name: bssp-gateway-service cloud: nacos: config: server-addr: 127.0.0.1:8848 namespace: 3a44ef28-b35c-44bb-8cd2-873c14ebc911 group: DEFAULT_GROUP file-extension: yml discovery: server-addr: 127.0.0.1:8848 namespace: 3a44ef28-b35c-44bb-8cd2-873c14ebc911 gateway: discovery: locator: enabled: true ``` 请根据你的具体需求修改上述配置,确保Nacos和Knife4j的配置正确。 希望以上信息对你有帮助。如果还有其他问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

等一场春雨

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值