程序员潇然 发表于 2022-7-7 12:43:09

[若依]微服务springcloud版新建增添加一个业务子模块微服务

若依框架是一个比较出名的后台管理系统,有多个不同版本。
本文介绍微服务版本如何新增一个微服务模块,类似System模块,对外提供服务。
阅读使用本文的前提是已经成功搭建起来了ruoyi的springcloud版本的环境。

### 新建模块

在modules模块下新建module
!(data/attachment/forum/202207/07/122333ww5188p77p78peuw.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/300 "image.png")
根据实际需要填写即可
!(data/attachment/forum/202207/07/122354en87eu82wtb0ojch.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/300 "image.png")

创建完成之后,删除无用的内容,比如resource下的template,按需删除即可

### 设置pom

直接复制ruoyi-modules-system的系统模块的pom.xml,然后对其内容进行修改,仅仅需要修改两个点,一个是artifactId一个是description
!(data/attachment/forum/202207/07/122721orlmt4ld55ushzrs.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/300 "image.png")
`本文作者:程序员潇然 疯狂的字节X https://crazybytex.com/`

### 设置resource内容

复制ruoyi-system的resources文件夹下的banner.txt,bootstrap.yml,logback.xml文件到ruoyi-xxx模块(刚才新建的模块)

#### 修改banner

banner动画,自行设置即可,有很多生成网站

#### 修改bootstrap.yml

端口号修改为你需要的,比如9205
修改应用名称,如ruoyi-xxx

```
# Tomcat
server:
port: 9205

# Spring
spring:
application:
    # 应用名称
    name: ruoyi-xxx
```

#### 修改logback.xml中日志存放路径

```
<!-- 日志存放路径 -->
<property name="log.path" value="logs/ruoyi-xxx" />
```

### 配置nacos

#### 创建配置

登录nacos,根据system配置文件,克隆一个
!(data/attachment/forum/202207/07/123326tnyk3kyvp399nazp.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/300 "image.png")
注意dataID为 ruoyi-xxx-dev   dev是环境
!(data/attachment/forum/202207/07/123500m68myz6s5s5l4s88.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/300 "image.png")

修改mybatis部分配置

```
mybatis:
    # 搜索指定包别名
    typeAliasesPackage: com.ruoyi.xxx
    # 配置mapper的扫描,找到所有的mapper.xml映射文件
    mapperLocations: classpath:mapper/**/*.xml
```

#### 配置网关

修改ruoyi-gateway-dev.yml网管的配置文件,仿照上面system的配置复制出来一份,并修改为新模块所对应的。

```
# xxx模块
      - id: ruoyi-xxx
          uri: lb://ruoyi-xxx
          predicates:
            - Path=/xxx/**
          filters:
            - StripPrefix=1
```

#### 配置sentinel

修改配置文件 sentinel-ruoyi-gateway

```
{
      "resource": "ruoyi-xxx",
      "count": 1000,
      "grade": 1,
      "limitApp": "default",
      "strategy": 0,
      "controlBehavior": 0
    },
```

### 调整启动类

参照RuoYiSystemApplication,具体地说就是直接复制几个注解即可,参考代码如下

```java
@EnableCustomConfig
@EnableCustomSwagger2
@EnableRyFeignClients
@SpringBootApplication
public class RuoYiSystemApplication
{
    public static void main(String[] args)
    {
      SpringApplication.run(RuoYiSystemApplication.class, args);
      System.out.println("(♥◠‿◠)ノ゙系统模块启动成功   ლ(´ڡ`ლ)゙\n" +
                " .-------.       ____   __      \n" +
                " |_ _   \\      \\   \\   //    \n" +
                " | ( ' )|       \\_. /'       \n" +
                " |(_ o _) /      _( )_ .'         \n" +
                " | (_,_).' _____(_ o _)'          \n" +
                " ||\\ \\|||   |(_,_)'         \n" +
                " || \\ `'   /|   `-'/         \n" +
                " ||\\    /\\      /         \n" +
                " ''-'   `'-'    `-..-'            ");
    }
}
```

### 配置完成,启动

```
Spring Boot Version: 2.6.7
Spring Application Name: ruoyi-xxx
                            _
                           (_)
_ ___   _   ___   _   ____________   ____
| '__|| | | | / _ \ | | | || ||______|/ __|| | | |/ __
| |   | |_| || (_) || |_| || |      \__ \| |_| |\__
|_|    \__,_| \___/\__, ||_|      |___/ \__, ||___
                      __/ |                  __/ |
                     |___/                  |___/
11:41:40.279 INFOc.a.n.c.r.client - - create a new rpc client of b51ae82a-d313-44e9-b3c5-5c5d744fd091_config-0
11:41:40.311 INFOo.r.Reflections - - Reflections took 20 ms to scan 1 urls, producing 3 keys and 6 values
11:41:40.333 INFOo.r.Reflections - - Reflections took 10 ms to scan 1 urls, producing 4 keys and 9 values
11:41:40.346 INFOo.r.Reflections - - Reflections took 11 ms to scan 1 urls, producing 3 keys and 10 values
11:41:40.492 INFOo.r.Reflections - - Reflections took 144 ms to scan 246 urls, producing 0 keys and 0 values
11:41:40.503 INFOo.r.Reflections - - Reflections took 10 ms to scan 1 urls, producing 1 keys and 5 values
11:41:40.514 INFOo.r.Reflections - - Reflections took 9 ms to scan 1 urls, producing 1 keys and 7 values
11:41:40.524 INFOo.r.Reflections - - Reflections took 7 ms to scan 1 urls, producing 2 keys and 8 values
11:41:40.666 INFOo.r.Reflections - - Reflections took 140 ms to scan 246 urls, producing 0 keys and 0 values
11:41:40.667 INFOc.a.n.c.r.client - - RpcClient init label, labels = {module=config, Vipserver-Tag=null, source=sdk, Amory-Tag=null, Location-Tag=null, taskId=0, AppName=unknown}
11:41:40.668 INFOc.a.n.c.r.client - - Register server push request handler:com.alibaba.nacos.client.config.impl.ClientWorker$ConfigRpcTransportClient$$Lambda$332/1716411886
11:41:40.668 INFOc.a.n.c.r.client - - Register server push request handler:com.alibaba.nacos.client.config.impl.ClientWorker$ConfigRpcTransportClient$$Lambda$333/1126112943
11:41:40.668 INFOc.a.n.c.r.client - - Registry connection listener to current client:com.alibaba.nacos.client.config.impl.ClientWorker$ConfigRpcTransportClient$1
11:41:40.669 INFOc.a.n.c.r.client - - RpcClient init, ServerListFactory = com.alibaba.nacos.client.config.impl.ClientWorker$ConfigRpcTransportClient$2
11:41:40.675 INFOc.a.n.c.r.client - - Try to connect to server on start up, server: {serverIp = '127.0.0.1', server main port = 8848}
11:41:41.791 INFOc.a.n.c.r.client - - Success to connect to server on start up, connectionId = 1657165301628_127.0.0.1_58242
11:41:41.792 INFOc.a.n.c.r.client - - Notify connected event to listeners.
11:41:41.792 INFOc.a.n.c.r.client - - Register server push request handler:com.alibaba.nacos.common.remote.client.RpcClient$ConnectResetRequestHandler
11:41:41.792 INFOc.a.n.c.r.client - - Register server push request handler:com.alibaba.nacos.common.remote.client.RpcClient$$Lambda$342/554510956
11:41:41.831 WARNc.a.c.n.c.NacosPropertySourceBuilder - - Ignore the empty nacos configuration and get it based on dataId & group
11:41:41.834 WARNc.a.c.n.c.NacosPropertySourceBuilder - - Ignore the empty nacos configuration and get it based on dataId & group
11:41:41.847 INFOc.u.z.d.ZeusDeviceApplication - - The following 1 profile is active: "dev"
11:41:42.948 WARNo.m.s.m.ClassPathMapperScanner - - No MyBatis mapper was found in '' package. Please check your configuration.
11:41:43.590 INFOo.a.c.h.Http11NioProtocol - - Initializing ProtocolHandler ["http-nio-9205"]
11:41:43.591 INFOo.a.c.c.StandardService - - Starting service
11:41:43.591 INFOo.a.c.c.StandardEngine - - Starting Servlet engine:
11:41:43.728 INFOo.a.c.c.C.[.[.[/] - - Initializing Spring embedded WebApplicationContext
11:41:44.260 INFOc.a.c.s.SentinelWebMvcConfigurer - - register SentinelWebInterceptor with urlPatterns: [/**].
11:41:45.304 INFOc.a.n.c.r.client - - create a new rpc client of 6a322d33-20ea-4493-9563-8de7319312da
11:41:45.305 INFOc.a.n.c.r.client - - RpcClient init label, labels = {module=naming, source=sdk}
11:41:45.306 INFOc.a.n.c.r.client - - RpcClient init, ServerListFactory = com.alibaba.nacos.client.naming.core.ServerListManager
11:41:45.306 INFOc.a.n.c.r.client - - Registry connection listener to current client:com.alibaba.nacos.client.naming.remote.gprc.redo.NamingGrpcRedoService
11:41:45.307 INFOc.a.n.c.r.client - - Register server push request handler:com.alibaba.nacos.client.naming.remote.gprc.NamingPushRequestHandler
11:41:45.307 INFOc.a.n.c.r.client - - Try to connect to server on start up, server: {serverIp = '127.0.0.1', server main port = 8848}
11:41:45.420 INFOc.a.n.c.r.client - - Success to connect to server on start up, connectionId = 1657165305310_127.0.0.1_58246
11:41:45.420 INFOc.a.n.c.r.client - - Notify connected event to listeners.
11:41:45.420 INFOc.a.n.c.r.client - - Register server push request handler:com.alibaba.nacos.common.remote.client.RpcClient$ConnectResetRequestHandler
11:41:45.420 INFOc.a.n.c.r.client - - Register server push request handler:com.alibaba.nacos.common.remote.client.RpcClient$$Lambda$342/554510956
11:41:45.964 INFOc.a.d.p.DruidDataSource - - {dataSource-1,master} inited
11:41:45.965 INFOc.b.d.d.DynamicRoutingDataSource - - dynamic-datasource - add a datasource named success
11:41:45.965 INFOc.b.d.d.DynamicRoutingDataSource - - dynamic-datasource initial loaded datasource,primary datasource named
11:41:47.481 INFOo.a.c.h.Http11NioProtocol - - Starting ProtocolHandler ["http-nio-9205"]
11:41:47.500 INFOc.a.c.n.r.NacosServiceRegistry - - nacos registry, DEFAULT_GROUP ruoyi-xxx 192.168.1.115:9205 register finished
11:41:47.800 INFOc.u.z.d.ZeusDeviceApplication - - Started ZeusDeviceApplication in 8.858 seconds (JVM running for 9.67)
11:41:47.806 INFOc.a.c.n.r.NacosContextRefresher - - listening config: dataId=ruoyi-xxx, group=DEFAULT_GROUP
11:41:47.807 INFOc.a.c.n.r.NacosContextRefresher - - listening config: dataId=ruoyi-xxx-dev.yml, group=DEFAULT_GROUP
11:41:47.808 INFOc.a.c.n.r.NacosContextRefresher - - listening config: dataId=ruoyi-xxx.yml, group=DEFAULT_GROUP
11:41:47.991 INFOc.a.n.c.r.client - - Receive server push request, request = NotifySubscriberRequest, requestId = 9
11:41:47.994 INFOc.a.n.c.r.client - - Ack server push request, request = NotifySubscriberRequest, requestId = 9
11:41:48.485 INFOo.a.c.c.C.[.[.[/] - - Initializing Spring DispatcherServlet 'dispatcherServlet'
INFO: Sentinel log output type is: file
INFO: Sentinel log charset is: utf-8
INFO: Sentinel log base directory is: C:\Users\PC\logs\csp\
INFO: Sentinel log name use pid is: false
```

查看nacos
!(data/attachment/forum/202207/07/152834nozz1z99v3je9ov6.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/300 "image.png")

!(data/attachment/forum/202206/16/141330jha7st9soow8772i.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/300 "common_log.png")
`转载务必注明出处:程序员潇然,疯狂的字节X,https://www.crazybytex.com/thread-40-1-1.html `

页: [1]
查看完整版本: [若依]微服务springcloud版新建增添加一个业务子模块微服务