42

First Steps with GCP SQL

 5 years ago
source link: https://www.tuicool.com/articles/hit/A7ZBjiI
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

In this post, we will take a look at how we can use Google Cloud Platform (GCP) SQL as a database for our Spring Boot application. We will investigate how we can use the Cloud database from our development machine and how we can use it from GCP itself.

1. Create a GCP Account

First, you will need to create a GCP account. Check out the first paragraph of aprevious post how to do so.

2. Create the Example Application

We will create a Spring Boot MVC web application which uses a PostgreSQL database in the Cloud. Go to start.spring.io and create a Java 11 Web MVC project. The application will simply return a list of Shows when accessing a shows URL. The sources are available at GitHub .

The only domain object is Show , which has an id and a title . The getters and setters are left out for brevity.

public class Show {
    private Long id;
    private String title;
    ...
}

For accessing the database, we will use JdbcTemplate . We only define a findAll method in the ShowDao which retrieves all Shows from the database. We use a ShowRowMapper in order to map the table columns to the Show domain object.

@Repository
public class ShowDaoImpl implements ShowDao {

    @Autowired
    JdbcTemplate jdbcTemplate;

    @Override
    public List findAll() {
        return jdbcTemplate.query("select * from show", new ShowRowMapper());
    }

}

class ShowRowMapper implements RowMapper {
    @Override
    public Show mapRow(ResultSet rs, int rowNum) throws SQLException {
        Show show = new Show();

        show.setId(rs.getLong("ID"));
        show.setTitle(rs.getString("TITLE"));

        return show;
    }
}

The ShowService will retrieve the data from the database via the ShowDao object.

@Component
public class ShowServiceImpl implements ShowService {

    @Resource
    ShowDao showDao;

    @Override
    public List findAll() {
        return showDao.findAll();
    }

}

And finally, we define a shows URL which routes to the shows method from the ShowController and which will return the values from the database as JSON.

@RestController
public class ShowController {

    @Resource
    ShowService showService;

    @RequestMapping("/shows")
    public List shows() {
        return showService.findAll();
    }
}

In file src/main/resources/schema.sql we define the Show table:

CREATE TABLE show (
    id SERIAL NOT NULL,
    title VARCHAR(200) NOT NULL,
    PRIMARY KEY (id)
);

In file src/main/resources/data.sql we define some contents for the Show table:

INSERT INTO show VALUES (1, 'My Developer Planet Show 1');
INSERT INTO show VALUES (2, 'My Developer Planet Show 2');
INSERT INTO show VALUES (3, 'My Developer Planet Show 3');
INSERT INTO show VALUES (4, 'My Developer Planet Show 4');
INSERT INTO show VALUES (5, 'My Developer Planet Show 5');

In the pom.xml file, we need a dependency for Spring Web MVC and a dependency for Spring Cloud GCP PostgreSQL.

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-web</artifactId>
  </dependency>
<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-gcp-starter-sql-postgresql</artifactId>
  <version>1.1.1.RELEASE</version>
</dependency>

3. Use GCP SQL from Your Local Development Machine

Now that we have created our application, it is time to set up the database and the necessary configuration in our application in order to run it from our local development machine.

3.1 Create Instance

In the GCP console, go to SQL and create a SQL instance.

ENrMneN.png!web

We have the choice between MySQL and PostgreSQL. We choose PostgreSQL.

zyMj6nV.png!web

Fill in the necessary configuration options. We set the Instance ID to myspringcloud , set up a password for the postgres user, choose a region and click the Create button.

jYJBvqA.png!web

After a minute or so, the SQL instance is created.

6jiaMzJ.png!web

3.2 Create Database

Click the Instance ID myspringcloud and select the Databases tab.

6r2ARbm.png!web

Click the Create database button and create a myspringclouddb database.

beeY3aq.png!web

3.3 Allow Connection from Local Machine

By default, it is not allowed to connect to the database from outside your GCP account. We therefore need to allow the IP address from our local development machine. Go to the Instance Details and the Connections tab. Ensure that Public IP is enabled.

yQFvUzn.png!web

Click the Add network button, fill in a name for your connection, e.g. mydevelopermachine and fill in your public IP address which can be retrieved via whatismyip.com . At last, click the Done button and the Save button.

zMR7vu6.png!web

3.4 Create Service Account

Last thing to do is to create a service account for our local development machine. Go to IAM & admin - Service accounts and click the Create Service Account button.

r6RVNvb.png!web

Enter a name for your service account, in our case mydevelopermachine and click the Create button.

FVjUVbr.png!web

Grant the service account the Owner role and click the Continue button.

nYjQNbF.png!web

In the last step, we click the Create key button.

UV3EBv6.png!web

Choose JSON as Key type and click the Create button.

emMrUjY.png!web

A JSON file is downloaded, keep it safe somewhere, we will need it later on.

3.5 Spring Database Configuration

Now that we have set up the database, it is time to return to the Spring application. We will need to configure some properties in the application.properties file.

spring.datasource.username=postgres
spring.datasource.password=[password]
spring.cloud.gcp.sql.database-name=myspringclouddb
spring.cloud.gcp.sql.instance-connection-name=mysqlcloud-236114:europe-west1:myspringcloud
spring.datasource.continue-on-error=true
spring.datasource.initialization-mode=always
spring.datasource.schema=file:src/main/resources/schema.sql
spring.datasource.data=file:src/main/resources/data.sql
spring.cloud.gcp.project-id=mysqlcloud-236114
spring.cloud.gcp.credentials.location=file:src/main/resources/service-account.json

Let’s explain the different properties:

spring.datasource.username : this is the database user we will use. It is also possible of course to create a user specifically for your application, but for simplicity, we use the postgres user for it.

spring.datasource.password : the password of the postgres user, replace [password] with your own password.

spring.cloud.gcp.sql.database-name : the GCP Cloud database name which we defined previously.

spring.cloud.gcp.sql.instance-connection-name : the connection name of the GCP SQL instance. This must be of the form [gcp-project-id]:[region]:[instance-name]

spring.datasource.continue-on-error : we will create the tables on startup, this property will prevent failure when the table already exists.

spring.datasource.initialization-mode : this property will create the table and fill the table with data during startup.

spring.datasource.schema : we indicate where the schema for our database can be found.

spring.datasource.data : we indicate where the data for our database can be found.

spring.cloud.gcp.project-id : this property is necessary for accessing the Cloud database from our local development machine and must contain the GCP project ID.

spring.cloud.gcp.credentials.location : this property is necessary for accessing the Cloud database from our local development machine and must refer to the JSON file we downloaded for our service account.

3.6 Run the Application

Run the Spring Boot application from our local development machine.

$ mvn spring-boot:run

Go to URL http://localhost:8080/shows when the application has started successfully. The following response is returned.

[
  {"id":1,"title":"My Developer Planet Show 1"},
  {"id":2,"title":"My Developer Planet Show 2"},
  {"id":3,"title":"My Developer Planet Show 3"},
  {"id":4,"title":"My Developer Planet Show 4"},
  {"id":5,"title":"My Developer Planet Show 5"}
]

4. Use GCP SQL from the GCP Console

In this paragraph, we will adapt our previously created application and configuration in order to run it from the GCP console. The sources can be found in branch feature/gcpconsole available at GitHub .

We need to adapt the Java version in our pom.xml file (Java 1.8 is the default on GCP console):

<properties>
  <java.version>1.8</java.version>
</properties>

In the application.properties file, we remove the entries which were necessary for access from our local development machine: spring.cloud.gcp.project-id and spring.cloud.gcp.credentials.location .

We also don’t need the service account mydevelopermachine and it is not necessary to enable the Public IP connection.

Go to the GCP console and clone the git repository:

$ git clone --branch feature/gcpconsole https://github.com/mydeveloperplanet/myspringcloudgcpplanet.git

Enter the myspringcloudgcpplanet directory and run the application:

$ mvn spring-boot:run

Start the Web Preview which will open a browser tab to the following URL:

https://8080-dot-6340638-dot-devshell.appspot.com/?authuser=0

This wil return a 404 error. Adapt the URL in order to retrieve the shows:

https://8080-dot-6340638-dot-devshell.appspot.com/shows/?authuser=0

The response is identical to the one issued from our local machine.

5. Conclusion

In this post, we created a Spring Boot MVC web application with a Cloud PostgreSQL database. We ran the application from our local development machine and from within the GCP console. It was fairly easy to use the GCP SQL database. In only a few minutes you have an up-and-running database in the Cloud which can be accessed from a local machine or from within your GCP account. Don’t forget to remove the database instances after experimenting!


Recommend

  • 57

    除非特别声明,此文章内容采用知识共享署名 3.0许可,代码示例采用Apache 2.0许可。更多细节请查看我们的服务条款。

  • 78
    • chinagdg.org 6 years ago
    • Cache

    GCP grows in the Netherlands region

    除非特别声明,此文章内容采用知识共享署名 3.0许可,代码示例采用Apache 2.0许可。更多细节请查看我们的服务条款。

  • 76
    • chinagdg.org 5 years ago
    • Cache

    GCP is building a region in Zürich

    除非特别声明,此文章内容采用知识共享署名 3.0许可,代码示例采用Apache 2.0许可。更多细节请查看我们的服务条款。

  • 56
    • chinagdg.org 5 years ago
    • Cache

    On GCP, your database your way

    On GCP, your database your way 2018-07-26...

  • 63
    • www.v2ex.com 5 years ago
    • Cache

    gcp 香港速度不错

    宽带症候群 - @kljsandjb - ![V2er]( https://i.loli.net/2018/10/23/5bce55996c0d5.png)<br><br>上一次看到这样速度还是 lightsail 日本?

  • 28
    • www.tuicool.com 4 years ago
    • Cache

    Searching for ET using AI on GCP

    A project playing with open data from SETI They say that the best way to learn data science is to create something. Once you’ve covering the basics of data manipulation, coding and statistics, using text...

  • 9
    • jkjung-avt.github.io 3 years ago
    • Cache

    Setting Up a NGINX + Flask Server on GCP

    Setting Up a NGINX + Flask Server on GCP Jul 4, 2020 I have always been wanting to set up a dashboard on the cloud which I could use to monitor IoT products and devices on the field. I was aware that AWS, Azure and GCP...

  • 3
    • mydeveloperplanet.com 3 years ago
    • Cache

    First Steps with GCP Kubernetes Engine

    The past year, we wrote some articles using Minikube as Kubernetes cluster in order to experiment with. In this post, we will take our first steps into Google Cloud Platform (GCP) and more specifically of Kubernetes Engine. Let’s see whether...

  • 1

    SQL Agent Jobs: Checking for failed steps at the end of a job 2009-04-18sql I use the SQL agent a lot, and it is handy for a lot o...

  • 4

    NAKIVO Blog > Data Protection > How to Configure MS SQL Server Replication...

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK