Build Microservices with Spring Cloud Alibaba
One-stop solution for all your cloud needs.
Currently, Spring Framework and its modules is one of the most popular frameworks for development in the Java world. According to a recent developer survey conducted by JetBrains, Spring Boot has become the most popular web framework, adding 14% since last year.
In the Java world, there is also another trend — microservices. According to the survey conducted by Vanson Bourne, under increasing pressure to innovate faster, a majority (84%) of organizations have embraced microservices.
The vast majority of companies (84 percent) are already using microservices. Thirty percent of public companies have transitioned entirely to distributed architectures, including microservices and serverless, which is slightly more than privately held companies (28 percent). Notably, 43 percent of organizations with between 3,000 to 4,999 employees are using entirely distributed architectures.
Due to the popularization of microservice architecture, tools are needed to manage microservices and solve emerging problems in distributed systems by implementing popular distributed system patterns: service discovery, distributed configuration management, circuit breaking, etc.
One such solution is Spring Cloud, which solves all the problems mentioned above using all of the benefits provided by Spring ecosystem. I will not cover in detail Spring Cloud (it’s a big topic itself), leaving the reader to familiarize himself with the framework in more detail here.
Since many of the big companies are developing their solutions for managing the microservice architecture, you can find Spring Cloud distributives from such big IT-companies as Netflix, Microsoft, Amazon. As well as Alibaba, whose solution we will dwell on in more detail in this article.
Spring Cloud Alibaba currently is one of the most popular Spring Cloud implementations (if not the most popular already), which can be seen in the graph below.
Requirement
In this article, we will write two microservices that will interact with each other in the Spring Cloud Alibaba ecosystem.
Imagine an Enterprise Post office (yes, Enterprise with a capital letter):
PostOffice
microservice will interact with PostalWorkers
microservice using the Apache Dubbo RPC-based framework for communication, Nacos for service discovery and Sentinel for throttling configuration and circuit breaking pattern implementation.
PostalWorker service
We’ll define a simple PostalWorker
interface, which only has one method deliverParcel
by trackNumber
.
And the client will call the PostOffice
interface, which also only has one method pickupParcel
by the same trackNumber
. For ease of implementation, in this example, the “real client” will call PostOffice
via HTTP
and in the rest of this article, we will refer to PostalWorker
as the server and PostOffice
as the client, because technically, they will be composed of each other with a client-server
relationship.
Let’s implement the PostalWorker
interface and annotate it with Dubbo annotation @Service
which means the same as Service
annotation in Spring Boot.
PostalWorker
YAML configuration (application.service.version
property describes version of our service which is also one of the great features in Dubbo):
Code for server-side service start:
Now let’s write the client side.
PostOffice service
Use @Reference
to annotate PostalWorker
as the remote service:
PostOffice
configuration:
Code for client-side start logic:
So we created and configured our client and server. But they cannot be initialized yet, because they need to be registered in the configuration center and be able to be found through service discovery.
Nacos
As already mentioned, for this purpose we will use Nacos, dynamic service discovery, configuration and service management platform for building cloud-native applications in Spring Cloud Alibaba ecosystem.
We will use it in out example mainly for its support for services to register themselves and to discover other services via a DNS or HTTP interface and also for its ability of real-time healthchecks of services.
So let’s change our diagram to include Nacos service discovery abilities that it brings with itself:
Let’s start Nacos server by using simple command:
sh ./bin/startup.sh -m standalone
where standalone
means non-cluster mode.
Now let’s start our microservices:
By visiting localhost:8848/nacos
we can see our successfully registered microservices in Nacos console.
Actually, there are a lot of more things that you can do with Nacos console but I will not focus on this in the article. You can find all the information on Nacos official web-site.
Because we made an agreement in the beginning of the article that for testing purposes our PostOffice
service will be called via HTTP, let’s test our current functionality in the browser:
Sentinel
So, our microservices work successfully and we also monitor them successfully. An inexperienced reader may ask: what could go wrong? Almost anything. There are a lot of things that can go wrong in the microservices world. For example, one of your microservices may quit unexpectedly or lose the network or could not be able to handle high loads. To deal with such situations in Alibaba was developed Sentinel which ensure flow control, circuit breaking and system adaptive protection, to guarantee the reliability of microservices.
Let’s start Sentinel and check our microservices in the console.
You can see that there are numerous options to configure your application rules. Let’s use one of them — Flow control rule. We will create a new resource with name postal_worker
and configure its threshold by the number of QPS (queries per second). For testing purposes, we will put value 1 which means that only 1 QPS is allowed (our postal workers are too lazy).
But that’s not all. We need to rewrite our service to successfully support the Sentinel ability to control requests flow.
Therefore we are providing a fallback method for our microservice that is responding to a user to take a look at some suggestions if our single postal worker is busy.
Let’s test our functionality by sending two requests simultaneously in the same second:
For more complicated scenarios Sentinel also provides a simple dashboard, on which you can monitor the clients in real-time:
Summary
Spring Cloud Alibaba is gaining popularity among Java developers and the Spring community. It’s a good time to get acquainted with cloud frameworks from such a big company as Alibaba whose technical solutions are serving millions of users daily.