Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add mysql support and docker compose example #255

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
142 changes: 142 additions & 0 deletions docs/mysql setting.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
# 修改config/application.yml
## 修改数据储存类型
``` yaml
mj:
task-store:
type: mysql
timeout: 30d
```

## 修改添加数据库配置

### 选择 mariadb 或者mysql

### mariadb

``` yaml
spring:
datasource:
url: jdbc:mariadb://localhost:3306/database
username: username
password: password
jpa:
hibernate:
ddl-auto: update
properties:
hibernate:
dialect: org.hibernate.dialect.MariaDB103Dialect
```

### MYSQL
ps: 以下MYSQL,没有测试

MYSQL 8+
``` yaml
spring:
datasource:
url: jdbc:mysql://localhost:3306/database
username: username
password: password
jpa:
hibernate:
ddl-auto: update
properties:
hibernate:
dialect: org.hibernate.dialect.MySQL5Dialect
```
MYSQL 5

``` yaml
dialect: org.hibernate.dialect.MySQL5Dialect
```

MYSQL 8
``` yaml
dialect: org.hibernate.dialect.MySQL8Dialect
```


附上我在使用的完成mariadb的application.yml 和docker-compose.yaml 配置
``` yaml
mj:
discord: xxxx
guild-id: xxx
channel-id: xxx
user-token: xxx
session-id: xxx
user-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36
botToken: xxx
user-wss: false
task-store:
type: mysql
timeout: 30d
translate-way: null
queue:
timeout-minutes: 5
core-size: 3
queue-size: 10
spring:
redis:
host: midjourney-redis
port: 6379
db: 2 ## no work,need fix TODO
datasource:
url: jdbc:mariadb://midjourney-mariadb:3306/midjourney-proxy
username: username
password: password
jpa:
hibernate:
ddl-auto: update
properties:
hibernate:
dialect: org.hibernate.dialect.MariaDB103Dialect
```


``` yaml

version: '3.7'

services:
midjourney-redis:
image: redis:alpine
container_name: midjourney-redis
hostname: midjourney-redis
ports:
- "6379:6379"
expose:
- "6379"
networks:
- default
midjourney-proxy:
build:
context: .
dockerfile: Dockerfile-dev
volumes:
- ./config:/home/spring/config
- ./logs:/home/spring/logs
ports:
- 8080:8080
- 9876:9876
environment:
- JAVA_OPTS=-XX:MaxRAMPercentage=85 -Djava.awt.headless=true -XX:+HeapDumpOnOutOfMemoryError -XX:MaxGCPauseMillis=20 -XX:InitiatingHeapOccupancyPercent=35 -Xlog:gc:file=/home/spring/logs/gc.log -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=9876 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false -Dlogging.file.path=/home/spring/logs -Dserver.port=8080 -Duser.timezone=Asia/Shanghai
networks:
- default
midjourney-mariadb:
image: mariadb:latest
container_name: midjourney-mariadb
hostname: midjourney-mariadb
networks:
- default
volumes:
- ./mariadb_data:/var/lib/mysql
ports:
- "3306:3306"
expose:
- "3306"
environment:
- MYSQL_ROOT_PASSWORD=root
- MYSQL_DATABASE=midjourney-proxy
- MYSQL_USER=midjourney-proxy
- MYSQL_PASSWORD=midjourney-proxy
```
16 changes: 15 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,21 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.mariadb.jdbc</groupId>
<artifactId>mariadb-java-client</artifactId>
<version>3.1.2</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-core</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ public static class TaskStore {
*/
private Duration timeout = Duration.ofDays(30);
/**
* 任务存储方式: redis(默认)、in_memory.
* 任务存储方式: redis(默认)、in_memory, redis.
*/
private Type type = Type.IN_MEMORY;

Expand All @@ -151,7 +151,11 @@ public enum Type {
/**
* in_memory.
*/
IN_MEMORY
IN_MEMORY,
/**
* in_memory.
*/
MYSQL
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.stereotype.Service;
import com.github.novicezk.midjourney.support.Task;
import com.github.novicezk.midjourney.support.TaskCondition;
import com.github.novicezk.midjourney.support.TaskRepository;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class MySQLTaskStoreServiceImpl implements TaskStoreService {

private final TaskRepository taskRepository;

@Autowired
public MySQLTaskStoreServiceImpl(TaskRepository taskRepository) {
this.taskRepository = taskRepository;
}

@Transactional
@Override
public void save(Task task) {
taskRepository.save(task);
}

@Override
public void delete(String id) {
taskRepository.deleteById(id);
}

@Override
public Task get(String id) {
return taskRepository.findById(id).orElse(null);
}

@Override
public List<Task> list() {
return taskRepository.findAll();
}

@Override
public List<Task> list(TaskCondition condition) {
return taskRepository.findAll().stream()
.filter(task -> condition.test(task))
.collect(Collectors.toList());
}

@Override
public Task findOne(TaskCondition condition) {
return taskRepository.findAll().stream()
.filter(task -> condition.test(task))
.findFirst().orElse(null);
}
}
14 changes: 14 additions & 0 deletions src/main/java/com/github/novicezk/midjourney/support/Task.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.github.novicezk.midjourney.support;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.novicezk.midjourney.enums.TaskAction;
import com.github.novicezk.midjourney.enums.TaskStatus;
import io.swagger.annotations.ApiModel;
Expand All @@ -11,14 +13,24 @@
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import javax.persistence.*;


@Entity
@Table(name = "tasks", indexes = {
@Index(name = "index_tasks_id", columnList = "id")
})
@Data
@ApiModel("任务")
public class Task implements Serializable {
@Serial
private static final long serialVersionUID = -674915748204390789L;

private TaskAction action;


@Id
@Column(name = "id")
@ApiModelProperty("任务ID")
private String id;
@ApiModelProperty("提示词")
Expand Down Expand Up @@ -46,8 +58,10 @@ public class Task implements Serializable {
private String failReason;

// 任务扩展属性,仅支持基本类型
@Transient
private Map<String, Object> properties;


@JsonIgnore
private final transient Object lock = new Object();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.github.novicezk.midjourney.support;

import org.springframework.data.jpa.repository.JpaRepository;
import com.github.novicezk.midjourney.support.Task;

public interface TaskRepository extends JpaRepository<Task, String> {
}
10 changes: 9 additions & 1 deletion src/main/java/spring/config/BeanConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
import com.github.novicezk.midjourney.service.TranslateService;
import com.github.novicezk.midjourney.service.store.InMemoryTaskStoreServiceImpl;
import com.github.novicezk.midjourney.service.store.RedisTaskStoreServiceImpl;
import com.github.novicezk.midjourney.service.store.MySQLTaskStoreServiceImpl;
import com.github.novicezk.midjourney.service.translate.BaiduTranslateServiceImpl;
import com.github.novicezk.midjourney.service.translate.GPTTranslateServiceImpl;
import com.github.novicezk.midjourney.support.Task;
import com.github.novicezk.midjourney.support.TaskRepository;
import com.github.novicezk.midjourney.support.TaskMixin;
import com.github.novicezk.midjourney.wss.WebSocketStarter;
import com.github.novicezk.midjourney.wss.bot.BotMessageListener;
Expand All @@ -23,12 +25,17 @@
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import org.springframework.beans.factory.annotation.Autowired;
import java.time.Duration;

@Configuration
public class BeanConfig {


@Autowired
private TaskRepository taskRepository;


@Bean
TranslateService translateService(ProxyProperties properties) {
return switch (properties.getTranslateWay()) {
Expand All @@ -45,6 +52,7 @@ TaskStoreService taskStoreService(ProxyProperties proxyProperties, RedisConnecti
return switch (type) {
case IN_MEMORY -> new InMemoryTaskStoreServiceImpl(timeout);
case REDIS -> new RedisTaskStoreServiceImpl(timeout, taskRedisTemplate(redisConnectionFactory));
case MYSQL -> new MySQLTaskStoreServiceImpl(taskRepository);
};
}

Expand Down