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

spring boot3 一、 spring alibaba cloud 整合 网关 nacos config_第1张图片

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

jianmu-springboot3-springalibabacloud pom.xml


<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.0modelVersion>

    <groupId>org.jmgroupId>
    <artifactId>jianmu-springboot3-springalibabacloudartifactId>
    <packaging>pompackaging>
    <version>1.0-SNAPSHOTversion>
    <modules>
        <module>jm-gatewaymodule>
        <module>jm-apismodule>
        <module>jm-apis-commonmodule>
        <module>jm-dubbo-apismodule>
    modules>

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

    <dependencies>
        
        <dependency>
            <groupId>org.slf4jgroupId>
            <artifactId>slf4j-apiartifactId>
        dependency>

    dependencies>

    <dependencyManagement>
       <dependencies>
           
           <dependency>
               <groupId>org.slf4jgroupId>
               <artifactId>slf4j-apiartifactId>
               <version>2.0.6version>
           dependency>

           <dependency>
               <groupId>org.springframework.bootgroupId>
               <artifactId>spring-boot-dependenciesartifactId>
               <version>3.0.0version>
               <type>pomtype>
               <scope>importscope>
           dependency>

           <dependency>
               <groupId>com.alibaba.cloudgroupId>
               <artifactId>spring-cloud-alibaba-dependenciesartifactId>
               <version>2022.0.0.0-RC1version>
               <type>pomtype>
               <scope>importscope>
           dependency>

           
           <dependency>
               <groupId>org.springframework.cloudgroupId>
               <artifactId>spring-cloud-dependenciesartifactId>
               <version>2022.0.0version>
               <type>pomtype>
               <scope>importscope>
           dependency>
       dependencies>
    dependencyManagement>

project>

jm-apis-common pom.xml


<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-springalibabacloudartifactId>
        <groupId>org.jmgroupId>
        <version>1.0-SNAPSHOTversion>
    parent>
    <modelVersion>4.0.0modelVersion>

    <artifactId>jm-apis-commonartifactId>
    <packaging>pompackaging>
    <modules>
        <module>jm-apis-common-confmodule>
        <module>jm-apis-common-toolmodule>
        <module>jm-apis-common-beanmodule>
    modules>

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

    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.bootgroupId>
                    <artifactId>spring-boot-starter-tomcatartifactId>
                exclusion>
            exclusions>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-undertowartifactId>
        dependency>
    dependencies>

project>

jm-apis pom.xml


<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-springalibabacloudartifactId>
        <groupId>org.jmgroupId>
        <version>1.0-SNAPSHOTversion>
    parent>
    <modelVersion>4.0.0modelVersion>

    <artifactId>jm-apisartifactId>
    <packaging>pompackaging>
    <modules>
        <module>jm-user-apimodule>
    modules>

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


    <dependencies>
        <dependency>
            <groupId>org.jmgroupId>
            <artifactId>jm-apis-commonartifactId>
            <version>1.0-SNAPSHOTversion>
        dependency>

        <dependency>
            <groupId>com.alibaba.cloudgroupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discoveryartifactId>
        dependency>
        
        <dependency>
            <groupId>com.alibaba.cloudgroupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-configartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-bootstrapartifactId>
        dependency>

    dependencies>

project>

jm-gateway 网关 pom.xml


<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-springalibabacloudartifactId>
        <groupId>org.jmgroupId>
        <version>1.0-SNAPSHOTversion>
    parent>
    <modelVersion>4.0.0modelVersion>

    <artifactId>jm-gatewayartifactId>

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

    <dependencies>
        <dependency>
            <groupId>com.alibaba.cloudgroupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discoveryartifactId>
        dependency>
        <dependency>
            <groupId>com.alibaba.cloudgroupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-configartifactId>
        dependency>
        <dependency>
            <groupId>com.alibaba.cloudgroupId>
            <artifactId>spring-cloud-alibaba-sentinel-gatewayartifactId>
        dependency>
        <dependency>
            <groupId>com.alibaba.cspgroupId>
            <artifactId>sentinel-datasource-nacosartifactId>
            <version>2.0.0-alphaversion>
        dependency>
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-bootstrapartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-gatewayartifactId>
        dependency>

    dependencies>

project>

nacos 配置中心

spring boot3 一、 spring alibaba cloud 整合 网关 nacos config_第2张图片

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

spring boot3 一、 spring alibaba cloud 整合 网关 nacos config_第3张图片

完整代码请看项目git地址

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

你可能感兴趣的:(springboot,spring,cloud,spring,spring,boot,java)