一、新建spring boot微服务模块
1)在pigx下鼠标右键新建目录: my-demo
2)新建module
二、删除多余文件或目录
my-demo/.mvn
my-demo/mvnw.cmd
my-demo/mvnw
my-demo/src/main/resources/application.properties
my-demo/HELP.md
my-demo/.gitignore
my-demo/my-demo.iml
my-demo/src/test
三、修改pom文件
1)pom.xml
特别注意 模块的版本号一定要注释掉,不然在集成mybatis-plus时会报异常
Unsatisfied dependency expressed through field 'baseMapper'; nested exception
就算加上
<properties>
<pigx.version>3.0.0</pigx.version>
</properties>
然后在应用pigx公共类库时
<dependency>
<groupId>com.pig4cloud</groupId>
<artifactId>pigx-common-data</artifactId>
<version>${pigx.version}</version>
</dependency>
也会报错
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
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.pig4cloud</groupId>
<artifactId>pigx</artifactId>
<version>3.0.0</version>
</parent>
<artifactId>my-demo</artifactId>
<!--<version>0.0.1</version>-->
<packaging>jar</packaging>
<description>在pigx-visual之外构建微服务</description>
<dependencies>
<!--必须:nacos注册中心客户端-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!--必须:nacos配置中心客户端-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<!--必备:spring-boot web 模块-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--是否需要:undertow容器,灵活的高性能Web服务器,提供包括阻塞和基于NIO的非阻塞机制-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-undertow</artifactId>
</dependency>
<!--mybatis-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
</dependency>
<!--数据库-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!--必备:pigx数据操作-->
<dependency>
<groupId>com.pig4cloud</groupId>
<artifactId>pigx-common-data</artifactId>
</dependency>
<!--必备:pigx common核心-->
<dependency>
<groupId>com.pig4cloud</groupId>
<artifactId>pigx-common-core</artifactId>
</dependency>
<!--必备:pigx安全模块-->
<dependency>
<groupId>com.pig4cloud</groupId>
<artifactId>pigx-common-security</artifactId>
</dependency>
<!--必备:pigx日志-->
<dependency>
<groupId>com.pig4cloud</groupId>
<artifactId>pigx-common-log</artifactId>
</dependency>
<!--推荐:pigx swagger api调试接入-->
<dependency>
<groupId>com.pig4cloud</groupId>
<artifactId>pigx-common-swagger</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2)pigx的pom.xml(父模块增加子模块配置)中增加my-demo
<modules>
<module>pigx-register</module>
<module>pigx-gateway</module>
<module>pigx-auth</module>
<module>pigx-upms</module>
<module>pigx-common</module>
<module>pigx-visual</module>
<module>my-demo</module>
</modules>
四、新增配置文件
1)my-demo/src/main/resources/bootstrap.yml
server:
*# 端口不能与其他模块冲突*
* *port: 5004
spring:
application:
name: @artifactId@
cloud:
nacos:
discovery:
server-addr: ${NACOS-HOST:pigx-register}:${NACOS-PORT:8848}
config:
server-addr: ${spring.cloud.nacos.discovery.server-addr}
file-extension: yml
shared-dataids: application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}
profiles:
active: dev
2)nacos配置中心新建 my-demo-dev.yml
## spring security 配置
security:
oauth2:
client:
# 配置后才能认证,生产环境建议设置独立的客户端信息
client-id: ENC(gPFcUOmJm8WqM3k3eSqS0Q==)
client-secret: ENC(gPFcUOmJm8WqM3k3eSqS0Q==)
scope: server
3)启动前端配置路由
五、修改main方法
package com.mycompany.mydemo;
import com.pig4cloud.pigx.common.security.annotation.EnablePigxFeignClients;
import com.pig4cloud.pigx.common.security.annotation.EnablePigxResourceServer;
import com.pig4cloud.pigx.common.swagger.annotation.EnablePigxSwagger2;
import org.springframework.boot.SpringApplication;
import org.springframework.cloud.client.SpringCloudApplication;
@EnablePigxSwagger2
@SpringCloudApplication
@EnablePigxFeignClients
@EnablePigxResourceServer
public class MyDemoApplication {
public static void main(String[] args) {
SpringApplication.run(MyDemoApplication.class, args);
}
}
六、创建Controller
package com.mycompany.mydemo.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/demo")
public class DemoController {
@GetMapping("/hello")
public String Hello() {
return "hello demo";
}
}
七、重启gateway,再启动MyDemoApplication
启动后使用swagger进行api测试
http://pigx-gateway:9999/swagger-ui.html?urls.primaryName=my-demo