Leveraging Alibaba Cloud Container Service to develop Spring Cloud Microservice Application (2)

This article demonstrates how Spring Cloud applications use HTTP for synchronous communication. Also, we look at the database access stage and ways to use Docker Compose file configurations to access MySQL or RDS.

Synchronous Communication

Services communicate with each other through the HTTP+JSON protocol. HTTP consumes higher overhead costs than local calling, so make sure you carefully set the inter-service API granularity.
Synchronous service calling (HTTP)
Earlier posts discussed how to use Eureka for discovering services. The foobar service must use HTTP/JSON to call the foo and bar services. Spring Cloud supports HTTP and JSON resolutions. For details, refer to the following sample code:

    @Autowired 
@LoadBalanced
private RestTemplate restTemplate;

...
private BarMessage getMessageFromBarService(){
BarMessage bar = restTemplate.getForObject("http://bar/message", BarMessage.class);
log.debug("From bar service : {}.", bar);
return bar;
}
...
The @Autowired and @LoadBalanced annotations must be added to the RestTemplate statement. In subsequent restTemplate.getForObject calls, input the service URL:
http://bar/message
This includes the bar service name and /message indicates the URL path to call. The function restTemplate.getForObject retrieves all bar service instances from Eureka and selects an instance to call and return results based on the built-in load balancing algorithm.
By using RestTemplate, you can discover and call services (HTTP protocol).

WebSocket
WebSocket is a lightweight protocol based on TCP. Applications that call for interactive responses use the WebSocket protocol for communication between servers and clients. For example, the protocol is used for session-style interaction between browsers and services. For details on the use of WebSocket in Spring Cloud applications, refer to Using WebSocket to build an interactive web application.

Asynchronous Communication

Asynchronous communication mechanisms decouple service callers and providers, so message and event-based mechanisms fit scenarios where real-time data is not required. Spring Cloud encapsulates a great deal of messaging middleware, such as Messaging with RabbitMQ and Messaging with Redis.

Database Access

Configure MYSQL dependencies in build.gradle
Add the following content in build.gradle to import a Java project dependency package for MySQL:

dependencies { 
...
compile('org.springframework.boot:spring-boot-starter-data-jpa')
runtime('com.h2database:h2')
runtime('mysql:mysql-connector-java')
...
}

Database access code
Create a JPA database access interface. For the code, refer to the foo service InfoRepository.java file.

public interface InfoRepository extends CrudRepository { 
List findByName(String name);
}
In the code, use the interface defined to access the database. For the code, refer to the foo service FooController.java file.
private String getInfoFromDatabase(){ 
List infoList = infoRepository.findByName(KEY);
for(Info info : infoRepository.findByName(KEY)) {
return info.toString();
}
return "(no database info)";
}

Configure database access in application.yml
In application.yml, configure access information for MySQL:

```yaml 
spring:
...
datasource:
url: jdbc:mysql://mysql:3306/${{MYSQL_DATABASE}:foodb}
username: ${{MYSQL_USERNAME}:user1}
password: ${{MYSQL_PASSWORD}:passw0rd}
testWhileIdle: true
validationQuery: SELECT 1
jpa:
show-sql: false
hibernate:
ddl-auto: update
naming-strategy: org.hibernate.cfg.ImprovedNamingStrategy
properties:
hibernate:
dialect: org.hibernate.dialect.MySQL5Dialect
In jdbc:mysql://mysql:3306/, mysql:3306 indicates that the access address is MySQL port 3306.
When using docker-compose to start a service, use the Docker Compose file to specify MySQL's response address. This can be a containerized MySQL image or RDS service on Alibaba Cloud.

Create or specify a MySQL instance in the Docker Compose file
MySQL image access is used in development and testing environments. The file is docker-compose.yml:

mysql: 
image: registry.aliyuncs.com/acs-sample/mysql:5.7
environment:
- MYSQL_ROOT_PASSWORD=passw0rd
- MYSQL_DATABASE=foodb
- MYSQL_USER=user1
- MYSQL_PASSWORD=passw0rd
foo:
image: xxx
links:
- mysql:mysql
environment:
...
- SPRING_PROFILES_ACTIVE=cloud
- MYSQL_DATABASE=foodb
- MYSQL_USERNAME=user1
- MYSQL_PASSWORD=passw0rd
For docker-compose, download the registry.aliyuncs.com/acs-sample/mysql:5.7 image and create a MySQL container; foo uses links to associate the MySQL it wants to access and the actual MySQL.

Access Alibaba Cloud RDS
To access RDS, change the MySQL image to external. The foo service configuration does not need a change in the Docker Compose file. This part is contained in docker-compose.acs.yml:

mysql: 
external:
host: ${rdsname}.mysql.rds.aliyuncs.com
port: 3306
environment:
- MYSQL_ROOT_PASSWORD=passw0rd
- MYSQL_DATABASE=foodb
- MYSQL_USER=user1
- MYSQL_PASSWORD=passw0rd

Summary

This article demonstrated how Spring Cloud applications use HTTP for synchronous communication. At the database access stage, we discussed how to use Docker Compose file configurations to access MySQL or RDS.