API模块

trip_modules-api父模块下创建子模块trip-comment-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-comment-api</artifactId>

<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
</dependencies>

</project>

微服务模块

trip_modules父模块下创建子模块trip-comment-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-comment-server</artifactId>

<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>com.swx</groupId>
<artifactId>trip-comment-api</artifactId>
</dependency>
<dependency>
<groupId>com.swx</groupId>
<artifactId>trip-common-security</artifactId>
</dependency>
</dependencies>

</project>

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

spring:
application:
name: comment-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:comment-service-dev.yaml

  • Group:DEFAULT_GROUP

  • 描述:评论微服务配置

  • 配置内容:

    comment-service-dev.yaml
    server:
    port: 8094
    spring:
    cloud:
    nacos:
    server-addr: xxx.xxx.xxx.xxx:8848
    discovery:
    namespace: ${spring.cloud.nacos.config.namespace}

    data:
    mongodb:
    uri: mongodb://swcode:123456@xxx.xxx.xxx.xxx:27017/trip-comment

配置网关路由

  • ID:trip-gateway-dev.yaml

  • Group:DEFAULT_GROUP

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

  • 配置内容:

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

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

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