JavaRush /Java Blog /Random EN /How I Hosted a Spring-Boot Application on Heroku
Павел
Level 11

How I Hosted a Spring-Boot Application on Heroku

Published in the Random EN group
CONTENTS OF THE ARTICLE CYCLE I decided to try to run a simple application on the cloud. I took as a basis this articleHow to host a Spring-Boot application on heroku.com. Step-by-step instructions If you read it and everything worked without questions, then the text below is not for you. Personally, I had a few additions: 1. Points 5, 6 and 7 show how to use a cloud database in an application, it's all interesting to read. But if, like me, it seemed superfluous to you, then you can usethe H2 DBMS (turn on Google Traslit). Here is an example of how to fill the database using the data.sql file and scripts. I did not use scripts, but theCommandLineRunner, which provides the run method. You just need to implement a class, for example MyRunner, attach a java configuration to it and implement an interface. Below is an example of usage:
@Component
public class MyRunner implements CommandLineRunner {

    @Autowired
    private CityRepository repository;

    @Override
    public void run(String... args) throws Exception {
        repository.deleteAll();

        repository.save(new City("Bratislava", 432000));
        repository.save(new City("Budapest", 1759000));
        repository.save(new City("Prague", 1280000));
    }
}
The method will be executed at the moment of application launch and save the entities in the database. Important: from paragraph 7 of the main article, in application.properties (or application.yaml) you need to add the setting server.port=${PORT:8080} - the default port will get rid of a common problem: heroku[router]: at=error code=H20 desc="App boot timeout" method=GET 2. Item 8 shows how to publish the application. It is written quite interestingly about the peculiarities of using applications on the Java 11 version, as an Old Believer I use the eighth Java, so I don’t need the system.properties file. If you have Java 9+ don't forget to create the mentioned file and add the Java version. The author suggests the following publishing order: <samp>To do this, perform the following steps: • Open the folder with the project repository in the Git Bash terminal. • Find a link to the remote heroku repository for your application in Settings - in the App Information → Heroku git URL section. • Add a new remote repository to your Git.</samp> I used the order described on the Heroku site. In the application created on the site, you need to find the Deploy tab, read everything that is written there. How I Hosted a Spring-Boot Application on Heroku - 1 An IDEA project structure must contain a . gitignore and the target folder should be excluded from Git tracking. I used Terminal IDEA as a console, just in the same project that I want to launch to the cloud, I opened the Terminal tab, this is how it looks: Therefore, I do How I Hosted a Spring-Boot Application on Heroku - 2 n’t have to look for the right directory. ( cd my-project/ ) heroku login - we already did (if not, now is the time). Then I copy and execute git commands in turn: First I create a git repository git init heroku git:remote -a from-java-rush Now I commit and deploy git add . git commit -am "make it better" git push heroku master Logs will appear in the console: that the application has started the assembly, that Maven is pumping out dependencies, and at the end:
remote:        https://from-java-rush.herokuapp.com/ deployed to Heroku
remote:
remote: Verifying deploy... done.
To https://git.heroku.com/from-java-rush.git
 * [new branch]      master -> master
Where the link is https://from-java-rush.herokuapp.com/, you will have your own link, so click on it. Of course, if you do not have a graphical interface in the application, but only a set of rest methods, you need to specify the name of the method through a slash after the link, for example: https://from-java-rush.herokuapp.com/heroku/city (you have to wait until the container wakes up, if it doesn't, then I deleted the application) Now you can make changes to the deployed application on heroku, directly from IDEA, using the Git console commands git add . git commit -am "make it better" git push heroku master Or the familiar but less informative interface buttons How I Hosted a Spring-Boot Application on Heroku - 3
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION