Mr. Cappuccino的第11杯咖啡——冒泡APP(升级版)之项目初始化

冒泡APP(升级版)之项目初始化

      • 1. 整体项目结构
      • 2. 创建父工程(bubble-parent):进行版本控制
      • 3. 创建公共依赖工程(bubble-common-lib):管理公共依赖
      • 4. 创建会员服务(bubble-member):管理用户信息
      • 5. 创建日记服务(bubble-diary):管理日记信息
      • 6. 搭建网关(bubble-gateway):项目统一入口
      • 7. 项目启动时遇到的一些问题
      • 8. 测试集成功能点

前言:心血来潮,打算将大学期间做的一些项目进行整合升级改造,出来工作已经将近两年了,现在每天的生活无一不是在为公司的事业奋斗着,殊不知自己对于这一行业的兴趣也正在慢慢地被消磨着。在大学期间,对这行业的兴趣十分浓厚,那时候自己就是整个项目的项目经理兼产品经理,完全可以去做着自己想要做的事情,而且通过自己的努力把整个项目给捣鼓起来了,那绝对是一件非常有成就感的事儿,在我看来这些就是让兴趣之火得以燃烧的主要原因。希望通过这个项目能让自己找到之前的那份初心与热爱,这个项目我会一直迭代下去的。加油!一起进步!一起成长!

话不多说,咱们先看一下之前写的那些项目。

  1. 冒泡交友APP:这是大二期间模仿QQ写的一款聊天交友的APP。主要使用了SpringBoot、Mybatis、MySQL等技术。
    项目结构:
    Mr. Cappuccino的第11杯咖啡——冒泡APP(升级版)之项目初始化_第1张图片
  2. 钟意日记APP:这是去年上半年送给女朋友的一份礼物,主要是为了彼此之间能够更加方便的交换日记进行阅读而开发的一款应用。
    Mr. Cappuccino的第11杯咖啡——冒泡APP(升级版)之项目初始化_第2张图片
    可见这两款应用都是非常传统的单体项目,技术栈偏于老旧,准备此次对它进行升级改造。

在写这篇博文之前,项目结构以及项目启动的一些问题已经得以解决,这篇博文主要用于记录分享。


1. 整体项目结构

Mr. Cappuccino的第11杯咖啡——冒泡APP(升级版)之项目初始化_第3张图片


2. 创建父工程(bubble-parent):进行版本控制

当前项目是基于JDK11+spring-cloud:2020.0.1+spring-cloud-alibaba:2021.1+spring-boot:2.4.2进行搭建。
Mr. Cappuccino的第11杯咖啡——冒泡APP(升级版)之项目初始化_第4张图片
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>com.bubblegroupId>
    <artifactId>bubble-parentartifactId>
    <version>1.0-RELEASEversion>

    <packaging>pompackaging>

    <properties>
        <maven.compiler.source>11maven.compiler.source>
        <maven.compiler.target>11maven.compiler.target>
        <boot.version>2.4.2boot.version>
        <mybatis.plus.starter.version>3.4.1mybatis.plus.starter.version>
        <swagger.starter.version>1.7.0.RELEASEswagger.starter.version>
        <cloud.version>2020.0.1cloud.version>
        <cloud.alibaba.version>2021.1cloud.alibaba.version>
    properties>

    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>2.4.2version>
        <relativePath/>
    parent>

    <dependencies>
        
        <dependency>
            <groupId>org.projectlombokgroupId>
            <artifactId>lombokartifactId>
        dependency>
        
        <dependency>
            <groupId>org.apache.commonsgroupId>
            <artifactId>commons-lang3artifactId>
        dependency>
        
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-bootstrapartifactId>
        dependency>
        
        
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-loadbalancerartifactId>
        dependency>
    dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-dependenciesartifactId>
                <version>${boot.version}version>
                <type>pomtype>
                <scope>importscope>
            dependency>
            <dependency>
                <groupId>org.springframework.cloudgroupId>
                <artifactId>spring-cloud-dependenciesartifactId>
                <version>${cloud.version}version>
                <type>pomtype>
                <scope>importscope>
            dependency>
            <dependency>
                <groupId>com.alibaba.cloudgroupId>
                <artifactId>spring-cloud-alibaba-dependenciesartifactId>
                <version>${cloud.alibaba.version}version>
                <type>pomtype>
                <scope>importscope>
            dependency>
        dependencies>
    dependencyManagement>

    
    <repositories>
        <repository>
            <id>spring-snapshotsid>
            <url>https://repo.spring.io/libs-snapshoturl>
        repository>
    repositories>
    <pluginRepositories>
        <pluginRepository>
            <id>spring-snapshotsid>
            <url>https://repo.spring.io/libs-snapshoturl>
        pluginRepository>
    pluginRepositories>

project>

版本组合关系
Mr. Cappuccino的第11杯咖啡——冒泡APP(升级版)之项目初始化_第5张图片


3. 创建公共依赖工程(bubble-common-lib):管理公共依赖

Mr. Cappuccino的第11杯咖啡——冒泡APP(升级版)之项目初始化_第6张图片
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>bubble-commonartifactId>
        <groupId>com.bubblegroupId>
        <version>1.0-RELEASEversion>
    parent>
    <modelVersion>4.0.0modelVersion>

    <artifactId>bubble-common-libartifactId>

    <packaging>jarpackaging>

    <dependencies>
        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
        
        <dependency>
            <groupId>com.baomidougroupId>
            <artifactId>mybatis-plus-boot-starterartifactId>
            <version>${mybatis.plus.starter.version}version>
        dependency>
        
        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
        dependency>
        
        <dependency>
            <groupId>com.spring4allgroupId>
            <artifactId>swagger-spring-boot-starterartifactId>
            <version>${swagger.starter.version}version>
        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-openfeignartifactId>
        dependency>
        
        <dependency>
            <groupId>javax.xml.bindgroupId>
            <artifactId>jaxb-apiartifactId>
        dependency>
        
        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-validationartifactId>
        dependency>
    dependencies>

project>

其中:bubble-common-base用于封装统一返回格式、bubble-common-cache用于封装缓存工具类(如redis)、bubble-common-core用于封装常用工具类(如MD5Util、SpringContextUtil)、bubble-common-log用于封装统一日志输出。


4. 创建会员服务(bubble-member):管理用户信息

Mr. Cappuccino的第11杯咖啡——冒泡APP(升级版)之项目初始化_第7张图片

boostrap.yml(配置注册中心、配置中心)

server:
  port: 8070
spring:
  application:
    name: bubble-member
  cloud:
    nacos:
      discovery:
        server-addr: X.X.X.X:8848
      # 分布式配置中心相关配置
      config:
        server-addr: X.X.X.X:8848
        file-extension: yml
  # 指定版本
  profiles:
    active: dev

application.yml(配置Swagger、数据库连接、Redis连接)

# swagger相关配置
swagger:
  base-package: com.bubble.member.service.impl
  title: 冒泡APP-会员服务接口
  description: 冒泡APP
  version: 1.1
  terms-of-service-url: https://honeyyxk.com
  contact:
    name: 袁小康
    email: [email protected]
    url: https://honeyyxk.com
  enabled: true

# 数据库相关配置
spring:
  datasource:
    username: honey
    password: honey@163
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://X.X.X.X.com:3306/bubble_member?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai
  redis:
    host: X.X.X.X.com
    password: honey@163
    port: 6379
    database: 0

AppMember.java(启动类,开启Swagger)

package com.bubble.member;

import com.spring4all.swagger.EnableSwagger2Doc;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * @author honey
 * @date 2021-09-07 12:55:09
 */
@EnableSwagger2Doc
@SpringBootApplication
public class AppMember {

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

bubble-member模块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>

    <artifactId>bubble-memberartifactId>
    <packaging>pompackaging>
    <version>1.0-RELEASEversion>
    <modules>
        <module>bubble-member-servicemodule>
        <module>bubble-member-apimodule>
    modules>

    <parent>
        <groupId>com.bubblegroupId>
        <artifactId>bubble-parentartifactId>
        <version>1.0-RELEASEversion>
    parent>
    <dependencies>
        <dependency>
            <groupId>com.bubblegroupId>
            <artifactId>bubble-common-libartifactId>
            <version>1.0-RELEASEversion>
        dependency>
    dependencies>

project>

bubble-member-api模块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>bubble-memberartifactId>
        <groupId>com.bubblegroupId>
        <version>1.0-RELEASEversion>
    parent>
    <modelVersion>4.0.0modelVersion>

    <artifactId>bubble-member-apiartifactId>

    <packaging>jarpackaging>

project>

bubble-member-service模块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>bubble-memberartifactId>
        <groupId>com.bubblegroupId>
        <version>1.0-RELEASEversion>
    parent>
    <modelVersion>4.0.0modelVersion>

    <artifactId>bubble-member-serviceartifactId>

    <packaging>jarpackaging>

    <dependencies>
        <dependency>
            <groupId>com.bubblegroupId>
            <artifactId>bubble-member-apiartifactId>
            <version>1.0-RELEASEversion>
        dependency>
    dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
            plugin>
        plugins>
    build>

project>

5. 创建日记服务(bubble-diary):管理日记信息

Mr. Cappuccino的第11杯咖啡——冒泡APP(升级版)之项目初始化_第8张图片

boostrap.yml(配置注册中心、配置中心)

server:
  port: 8060
spring:
  application:
    name: bubble-diary
  cloud:
    nacos:
      discovery:
        server-addr: X.X.X.X:8848
      # 分布式配置中心相关配置
      config:
        server-addr: X.X.X.X:8848
        file-extension: yml
  # 指定版本
  profiles:
    active: dev

application.yml(配置Swagger、数据库连接、Redis连接)

# swagger相关配置
swagger:
  base-package: com.bubble.diary.service.impl
  title: 冒泡APP-日记服务接口
  description: 冒泡APP
  version: 1.1
  terms-of-service-url: https://honeyyxk.com
  contact:
    name: 袁小康
    email: [email protected]
    url: https://honeyyxk.com
  enabled: true

# 数据库相关配置
spring:
  datasource:
    username: honey
    password: honey@163
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://X.X.X.X.com:3306/bubble_diary?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai
  redis:
    host: X.X.X.X.com
    password: honey@163
    port: 6379
    database: 1

AppDiary.java(启动类,开启Swagger、Feign客户端)

package com.bubble.diary;

import com.spring4all.swagger.EnableSwagger2Doc;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;

/**
 * @author honey
 * @date 2021-09-07 23:15:09
 */
@EnableFeignClients
@EnableSwagger2Doc
@SpringBootApplication
public class AppDiary {

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

bubble-diary模块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>

    <artifactId>bubble-diaryartifactId>
    <packaging>pompackaging>
    <version>1.0-RELEASEversion>
    <modules>
        <module>bubble-diary-apimodule>
        <module>bubble-diary-servicemodule>
    modules>

    <parent>
        <groupId>com.bubblegroupId>
        <artifactId>bubble-parentartifactId>
        <version>1.0-RELEASEversion>
    parent>
    <dependencies>
        <dependency>
            <groupId>com.bubblegroupId>
            <artifactId>bubble-common-libartifactId>
            <version>1.0-RELEASEversion>
        dependency>
    dependencies>

project>

bubble-diary-api模块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>bubble-diaryartifactId>
        <groupId>com.bubblegroupId>
        <version>1.0-RELEASEversion>
    parent>
    <modelVersion>4.0.0modelVersion>

    <artifactId>bubble-diary-apiartifactId>

    <packaging>jarpackaging>

project>

bubble-diary-service模块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>bubble-diaryartifactId>
        <groupId>com.bubblegroupId>
        <version>1.0-RELEASEversion>
    parent>
    <modelVersion>4.0.0modelVersion>

    <artifactId>bubble-diary-serviceartifactId>

    <packaging>jarpackaging>

    <dependencies>
        <dependency>
            <groupId>com.bubblegroupId>
            <artifactId>bubble-diary-apiartifactId>
            <version>1.0-RELEASEversion>
        dependency>
    dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
            plugin>
        plugins>
    build>

project>

6. 搭建网关(bubble-gateway):项目统一入口

Mr. Cappuccino的第11杯咖啡——冒泡APP(升级版)之项目初始化_第9张图片
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>

    <artifactId>bubble-gatewayartifactId>
    <version>1.0-RELEASEversion>

    <packaging>jarpackaging>

    <parent>
        <groupId>com.bubblegroupId>
        <artifactId>bubble-parentartifactId>
        <version>1.0-RELEASEversion>
    parent>
    <dependencies>
        
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-gatewayartifactId>
        dependency>
        
        <dependency>
            <groupId>com.spring4allgroupId>
            <artifactId>swagger-spring-boot-starterartifactId>
            <version>${swagger.starter.version}version>
        dependency>
        
        <dependency>
            <groupId>com.alibaba.cloudgroupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discoveryartifactId>
        dependency>
    dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
            plugin>
        plugins>
    build>

project>

boostrap.yml(配置注册中心、转发路由)

server:
  port: 81
spring:
  application:
    name: bubble-gateway
  cloud:
    gateway:
      routes:
        - id: member
          uri: lb://bubble-member
          predicates:
            - Path=/bubble-member/**
          filters:
            - SwaggerHeaderFilter
            - StripPrefix=1
        - id: diary
          uri: lb://bubble-diary
          predicates:
            - Path=/bubble-diary/**
          filters:
            - SwaggerHeaderFilter
            - StripPrefix=1
      x-forwarded:
        enabled: false
    nacos:
      discovery:
        server-addr: X.X.X.X:8848

AppGateway.java

package com.bubble.gateway;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * @author honey
 * @date 2021-09-07 12:55:09
 */
@SpringBootApplication
public class AppGateway {

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


7. 项目启动时遇到的一些问题

Error creating bean with name 'xmlModelPlugin’

解决方案:引入jaxb-api依赖


<dependency>
    <groupId>javax.xml.bindgroupId>
    <artifactId>jaxb-apiartifactId>
dependency>

java.lang.NoClassDefFoundError: javax/validation/constraints/Min

解决方案:引入validation依赖



<dependency>
    <groupId>org.springframework.bootgroupId>
    <artifactId>spring-boot-starter-validationartifactId>
dependency>

SpringCloud 2020.x.x工程bootstrap引导配置不生效

解决方案:引入boostrap依赖


<dependency>
    <groupId>org.springframework.cloudgroupId>
    <artifactId>spring-cloud-starter-bootstrapartifactId>
dependency>

网关无法转发/OpenFeign无法远程调用

解决方案:引入loadbalancer依赖



<dependency>
    <groupId>org.springframework.cloudgroupId>
    <artifactId>spring-cloud-loadbalancerartifactId>
dependency>

8. 测试集成功能点

注册中心
Mr. Cappuccino的第11杯咖啡——冒泡APP(升级版)之项目初始化_第10张图片
如何集成?

  1. 引入nacos-discovery的依赖;
  2. boostrap.yml配置注册中心地址;

配置中心

在会员服务进行功能点测试,新增测试接口用于测试

MemberService.java

package com.bubble.member.service;

import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @author honey
 * @date 2021-09-07 12:55:09
 */
@Api(tags = "会员信息服务接口")
@RequestMapping(value = "/member")
public interface MemberService {

    /**
     * 根据令牌查询用户基本信息接口
     *
     * @param loginToken 登录令牌
     * @return String
     */
    @GetMapping(value = "/getUserInfo")
    @ApiOperation("根据令牌查询用户基本信息接口")
    @ApiImplicitParam(name = "loginToken", value = "登录令牌", required = true)
    @ApiResponses({@ApiResponse(code = 200, message = "成功"), @ApiResponse(code = 500, message = "失败")})
    String getUserInfo(String loginToken);
}

MemberServiceImpl.java

package com.bubble.member.service.impl;

import com.bubble.member.service.MemberService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author honey
 * @date 2021-09-07 12:55:09
 */
@RestController
public class MemberServiceImpl implements MemberService {

    @Value("${test}")
    private String test;

    @Override
    public String getUserInfo(String loginToken) {
        return "信息" + test;
    }
}

nacos新增配置文件
Mr. Cappuccino的第11杯咖啡——冒泡APP(升级版)之项目初始化_第11张图片
请求接口能达到预期则说明集成成功
Mr. Cappuccino的第11杯咖啡——冒泡APP(升级版)之项目初始化_第12张图片

如何集成?

  1. 引入nacos-config依赖
  2. boostrap.yml配置配置中心地址;

注意:nacos新增配置文件名称bubble-member-dev.yml需要和boostrap.yml中的配置保持一致。

openfeign

在日记服务进行功能点测试,新增测试接口用于测试

DiaryService.java

package com.bubble.diary.service;

import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @author honey
 * @date 2021-09-07 12:55:09
 */
@Api(tags = "日记信息服务接口")
@RequestMapping(value = "/diary")
public interface DiaryService {

    /**
     * 根据令牌查询日记基本信息接口
     *
     * @param loginToken 登录令牌
     * @return String
     */
    @GetMapping(value = "/getDiaryInfo")
    @ApiOperation("根据令牌查询日记基本信息接口")
    @ApiImplicitParam(name = "loginToken", value = "登录令牌", required = true)
    @ApiResponses({@ApiResponse(code = 200, message = "成功"), @ApiResponse(code = 500, message = "失败")})
    String getDiaryInfo(String loginToken);
}

DiaryServiceImpl.java

package com.bubble.diary.service.impl;

import com.bubble.diary.feign.MemberFeignService;
import com.bubble.diary.service.DiaryService;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author honey
 * @date 2021-09-07 23:15:09
 */
@RestController
public class DiaryServiceImpl implements DiaryService {

    @Resource
    private MemberFeignService memberFeignService;

    @Override
    public String getDiaryInfo(String loginToken) {
        return memberFeignService.getUserInfo(loginToken);
    }
}

MemberFeignService.java

package com.bubble.diary.feign;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * @author honey
 * @date 2021-09-07 23:15:09
 */
@FeignClient(name = "bubble-member", path = "/member")
public interface MemberFeignService {

    /**
     * 获取用户信息
     *
     * @param loginToken 令牌
     * @return String
     */
    @GetMapping(value = "/getUserInfo")
    String getUserInfo(@RequestParam("loginToken") String loginToken);
}

请求接口能达到预期则说明集成成功
Mr. Cappuccino的第11杯咖啡——冒泡APP(升级版)之项目初始化_第13张图片

如何集成?

  1. 引入openfeign、loadbalancer依赖;
  2. 启动类开启Feign客户端,加上@EnableFeignClients;
  3. 调用类加上@FeignClient(name = “bubble-member”, path = “/member”)

网关转发

请求接口能达到预期则说明集成成功
Mr. Cappuccino的第11杯咖啡——冒泡APP(升级版)之项目初始化_第14张图片

如何集成?

  1. 引入gateway、loadbalancer依赖;
  2. boostrap.yml配置转发路由;

网关swagger统一入口

请求网关整合的swagger页面,能实现服务之间来回切换并成功请求服务的接口则说明集成成功
Mr. Cappuccino的第11杯咖啡——冒泡APP(升级版)之项目初始化_第15张图片

如何集成?

新增swagger配置类
Mr. Cappuccino的第11杯咖啡——冒泡APP(升级版)之项目初始化_第16张图片

SwaggerProvider.java

package com.bubble.gateway.swagger.config;

import lombok.AllArgsConstructor;
import org.springframework.cloud.gateway.config.GatewayProperties;
import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.support.NameUtils;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;
import springfox.documentation.swagger.web.SwaggerResource;
import springfox.documentation.swagger.web.SwaggerResourcesProvider;

import java.util.ArrayList;
import java.util.List;

/**
 * @author honey
 * @date 2021-09-07 12:55:09
 */
@Component
@Primary
@AllArgsConstructor
public class SwaggerProvider implements SwaggerResourcesProvider {

    public static final String API_URI = "/v2/api-docs";
    private final RouteLocator routeLocator;
    private final GatewayProperties gatewayProperties;


    @Override
    public List<SwaggerResource> get() {
        List<SwaggerResource> resources = new ArrayList<>();
        List<String> routes = new ArrayList<>();
        routeLocator.getRoutes().subscribe(route -> routes.add(route.getId()));
        gatewayProperties.getRoutes().stream().filter(routeDefinition -> routes.contains(routeDefinition.getId()))
                .forEach(routeDefinition -> routeDefinition.getPredicates().stream()
                        .filter(predicateDefinition -> ("Path").equalsIgnoreCase(predicateDefinition.getName()))
                        .forEach(predicateDefinition -> resources.add(swaggerResource(routeDefinition.getId(),
                                predicateDefinition.getArgs().get(NameUtils.GENERATED_NAME_PREFIX + "0")
                                        .replace("/**", API_URI)))));
        return resources;
    }

    private SwaggerResource swaggerResource(String name, String location) {
        SwaggerResource swaggerResource = new SwaggerResource();
        swaggerResource.setName(name);
        swaggerResource.setLocation(location);
        swaggerResource.setSwaggerVersion("2.0");
        return swaggerResource;
    }
}

SwaggerHeaderFilter.java

package com.bubble.gateway.swagger.filter;

import com.bubble.gateway.swagger.config.SwaggerProvider;
import org.apache.commons.lang3.StringUtils;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;

/**
 * @author honey
 * @date 2021-09-07 12:55:09
 */
@Component
public class SwaggerHeaderFilter extends AbstractGatewayFilterFactory<Object> {

    private static final String HEADER_NAME = "X-Forwarded-Prefix";

    @Override
    public GatewayFilter apply(Object config) {
        return (exchange, chain) -> {
            ServerHttpRequest request = exchange.getRequest();
            String path = request.getURI().getPath();
            if (!StringUtils.endsWithIgnoreCase(path, SwaggerProvider.API_URI)) {
                return chain.filter(exchange);
            }
            String basePath = path.substring(0, path.lastIndexOf(SwaggerProvider.API_URI));
            ServerHttpRequest newRequest = request.mutate().header(HEADER_NAME, basePath).build();
            ServerWebExchange newExchange = exchange.mutate().request(newRequest).build();
            return chain.filter(newExchange);
        };
    }
}

SwaggerHandler.java

package com.bubble.gateway.swagger.handler;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Mono;
import springfox.documentation.swagger.web.*;

import java.util.Optional;

/**
 * @author honey
 * @date 2021-09-07 12:55:09
 */
@RestController
@RequestMapping("/swagger-resources")
public class SwaggerHandler {


    private SecurityConfiguration securityConfiguration;

    private UiConfiguration uiConfiguration;

    private final SwaggerResourcesProvider swaggerResources;

    @Autowired
    public SwaggerHandler(SwaggerResourcesProvider swaggerResources) {
        this.swaggerResources = swaggerResources;
    }


    @GetMapping("/configuration/security")
    public Mono<ResponseEntity<SecurityConfiguration>> securityConfiguration() {
        return Mono.just(new ResponseEntity<>(Optional.ofNullable(securityConfiguration).orElse(SecurityConfigurationBuilder.builder().build()), HttpStatus.OK));
    }

    @GetMapping("/configuration/ui")
    public Mono<ResponseEntity<UiConfiguration>> uiConfiguration() {
        return Mono.just(new ResponseEntity<>(Optional.ofNullable(uiConfiguration).orElse(UiConfigurationBuilder.builder().build()), HttpStatus.OK));
    }

    @GetMapping("")
    public Mono<ResponseEntity<Object>> swaggerResources() {
        return Mono.just((new ResponseEntity<>(swaggerResources.get(), HttpStatus.OK)));
    }
}

注意:boostrap.yml文件中需要加入配置点
Mr. Cappuccino的第11杯咖啡——冒泡APP(升级版)之项目初始化_第17张图片

好啦,到这里项目初始化就已经结束啦,小伙伴们有什么疑问可以咨询小主哟

逆境总是有的,人生总要进击。愿你不要屈从于命运的安排,坚韧不拔,锲而不舍!做永远的生活强者!

你可能感兴趣的:(Bubble,spring,boot,spring,spring,cloud,spring,cloud,alibaba)