首先使用Heroku + Spring Boot进行部署
我试着将Spring Boot部署在Heroku上。
这是我为自己做的备忘录。
环境
- 
- Mac BigSur 11.1
 
 
- 
- Eclipse/maven
 
 
- 
- Spring Boot 2.4.5
 
 
- Java 1.8
 
Heroku是一个云平台。
- Heroku cliインストール
 
brew tap heroku/brew && brew install heroku
- git/commit
 
git init
git add xxxxxx
git commit -m "first commit."
- 
- heroku app
 
 
Herokuアプリ作成
ログインを問われるのでHerokuにログイン
heroku create <appname>
春季啟動
在Eclipse中创建项目。
- file構成はこんな感じ
 

- 
- Procfile
 
 
[app-name]は、pom.xml記載の下記部分。[artifactId]-[version].jarになる
web: java -Dserver.port=$PORT -jar target/[app-name].jar
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
- 
- system.properties
 
 
使用するruntimeバージョン
java.runtime.version=1.8
- 
- pom.xml
 
 
Webapp Runner をダウンロードするための Maven の設定
建议在构建工具中自动下载 Webapp Runner,虽然使用 Webapp Runner 并不是必需的,但由于应用程序的执行需要,这是必要的。
- maven-compiler-pluginは、java実行環境バージョンを合わせるための記載
 
  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-dependency-plugin</artifactId>
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>copy</goal>
            </goals>
            <configuration>
              <artifactItems>
                <artifactItem>
                  <groupId>com.heroku</groupId>
                  <artifactId>webapp-runner</artifactId>
                  <version>9.0.30.0</version>
                  <destFileName>webapp-runner.jar</destFileName>
                </artifactItem>
              </artifactItems>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
将应用程序部署到Heroku。
git push heroku master
- 既にheroku上にアプリが存在している場合
 
heroku git:remote --app=<アプリ名>
Heroku PostgreSQL -> Heroku的PostgreSQL


- pomに追加
 
 <dependency>
   <groupId>org.postgresql</groupId>
   <artifactId>postgresql</artifactId>
   <scope>runtime</scope>
 </dependency>
- Heroku上でCredential生成
 

- Heroku CLIからDBを操作可能
 
heroku pg:psql [DB名] --app [アプリ名]
- 
- Heroku上とローカルでDBを切り替える
 
 
application.propertiesをローカル用とHeroku用に用意
Procfileにて、”-Dspring.profiles.active=heroku”を指定
web: java -Dspring.profiles.active=heroku -Dserver.port=$PORT -jar target/[アプリ名].jar
# デフォルトlocalに
spring.profiles.active=local
# for Local Deploy
spring.mvc.view.prefix=/WEB-INF/views/
spring.mvc.view.suffix=.jsp
spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.url=jdbc:h2:[DBパス]
spring.datasource.username=[]
spring.datasource.password=[]
spring.h2.console.enabled=true
## for Heroku Deploy
spring.mvc.view.prefix=/WEB-INF/views/
spring.mvc.view.suffix=.jsp
“spring.profiles.include无法使用了吗?”
其他备忘录
- 下記エラーが発生が発生した場合
 
javax.management.InstanceAlreadyExistsException: org.springframework.boot:type=Admin,name=SpringApplication
- 
- Ecpliseで実行の構成で「JMXを使用可能にする」のチェックをオフに。
 
 
JMXの仕組みがよくわかってないので改めて追記

- Starter
 
Spring Boot的机制是将实现各项功能所需的库依赖集成在一起。
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
在上述示例中,Web相关的依赖关系被汇总,并包含了以下内容。
- 
- Spring Boot
 
 
- 
- Spring MVC
 
 
- 
- Tomcat
 
 
- 
- Bean Validation
 
 
- 
- Jackson
 
 
- SLF4J + Logback
 
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.4.3</version>
    <relativePath />
  </parent>
- 
- アプリで使用するSpring Bootのバージョン
 
 
- 上記は各依存ライブラリのバージョンを解決するparentの定義。