API模块

trip_modules-api父模块下创建子模块trip-article-api,pom文件内容如下:

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>
<parent>
<groupId>com.swx</groupId>
<artifactId>trip-modules-api</artifactId>
<version>1.0.0</version>
</parent>

<artifactId>trip-article-api</artifactId>

<dependencies>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<scope>provided</scope> <!-- 只在编译和测试生效,运行时不生效,阻止依赖的传递性 -->
</dependency>
<dependency>
<groupId>com.swx</groupId>
<artifactId>trip-users-api</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>

</project>

微服务模块

API模块不需要启动类,在运行时不需要Mybatis Plus的依赖,但是开发时使用到了注解,所需使用scope限制只在编译和测试依赖,并且阻止依赖的传递性。而Server需要运行时依赖,所以不使用scope。

api和server都引入了依赖,但是使用scope限制api中依赖的传递。这样的好处是,当service不需要api中的依赖时,能避免因为引入依赖而没有配置导致项目启动失败。

trip_modules父模块下创建子模块trip-article-server,pom文件内容如下:

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>
<parent>
<groupId>com.swx</groupId>
<artifactId>trip-modules</artifactId>
<version>1.0.0</version>
</parent>

<artifactId>trip-article-server</artifactId>

<dependencies>
<dependency>
<groupId>com.swx</groupId>
<artifactId>trip-article-api</artifactId>
</dependency>
<!-- 安全模块,做登陆拦截 -->
<dependency>
<groupId>com.swx</groupId>
<artifactId>trip-common-security</artifactId>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- 阿里云 OSS 对象存储 -->
<dependency>
<groupId>com.aliyun.oss</groupId>
<artifactId>aliyun-sdk-oss</artifactId>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
</dependency>
<dependency>
<groupId>javax.activation</groupId>
<artifactId>activation</artifactId>
</dependency>
<!-- no more than 2.3.3-->
<dependency>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-runtime</artifactId>
</dependency>
</dependencies>

</project>

配置文件:bootstrap.yaml,内容如下:

spring:
application:
name: article-service
cloud:
nacos:
server-addr: xxx.xxx.xxx.xxx:8848
config:
file-extension: yaml
namespace: trip_cloud_dev
shared-configs:
- data-id: redis-${spring.profiles.active}.yaml
refresh: true
- data-id: jwt-${spring.profiles.active}.yaml
refresh: true
profiles:
active: dev

Nacos配置文件

  • ID:article-service-dev.yaml

  • Group:DEFAULT_GROUP

  • 描述:文章微服务配置

  • 配置内容:

    article-service-dev.yaml
    server:
    port: 8092
    spring:
    cloud:
    nacos:
    server-addr: xxx.xxx.xxx.xxx:8848
    discovery:
    namespace: ${spring.cloud.nacos.config.namespace}
    datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql:///trip-article?serverTimezone=GMT%2B8&useSSL=false&characterEncoding=utf-8&allowPublicKeyRetrieval=true
    username: root
    password: xxxxxx

    mybatis-plus:
    mapper-locations: classpath*:mapper/*.xml
    type-aliases-package: com.swx.article.domain

    aliyun:
    oss:
    endpoint:
    bucket-name:
    domain:
    accessKey:
    id:
    secret:

配置网关路由

  • ID:trip-gateway-dev.yaml

  • Group:DEFAULT_GROUP

  • 描述:旅游项目网关配置

  • 配置内容:

    trip-gateway-dev.yaml
    - id: trip_article
    uri: lb://article-service
    predicates:
    - Path=/article/**
    filters:
    - StripPrefix=1

创建启动类:TripArticleApplication,首先创建com.swx.article包,在该包下创建。

TripArticleApplication
@EnableFeignClients
@SpringBootApplication
public class TripArticleApplication {
public static void main(String[] args) {
SpringApplication.run(TripArticleApplication.class, args);
}
}