In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)05/31 Report--
This article introduces the knowledge of "how to use Spring Cloud and Docker to build a micro-service platform". Many people will encounter this dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
What is Spring Cloud?
Spring Cloud is a toolset provided by Pivotal to simplify the construction of distributed systems. Spring Cloud introduces the concepts of Cloud platform Connector (Cloud Connector) and Service Connector (Service Connector). The cloud platform connector is an interface that needs to be implemented by the cloud platform provider so that other modules in the library can work with the cloud platform.
In the solutions provided by Spring Cloud, you will find the following:
Configuration Service
Discovery Service
Circuit breakers
Distributed sessions
Spring Boot
The most important thing about Spring Cloud is that it works with Spring Boot, and Spring Boot can help developers create Spring-based applications and services more easily.
As you can see from the Boot in the Spring Boot project name, the role of Spring Boot is to create and launch a new project based on the Spring framework. Spring Boot will select the most suitable Spring subproject to integrate with third-party open source libraries. Most Spring Boot applications can run quickly with very little configuration. Spring Boot includes the following features:
Create Spring applications that can run independently.
Directly embed the Tomcat or Jetty server without the need to deploy the WAR file.
The recommended base POM file is provided to simplify Apache Maven configuration.
Automatically configure the Spring framework based on project dependencies as much as possible.
Provides functions that can be used directly in a production environment, such as performance metrics, application information, and application health checks.
There is no code generation and no XML configuration file.
Service Discovery and Intelligent routing
Each service contains a micro-service architecture with a specific meaning. When you build a micro-service architecture on Spring Cloud, there are a few basic concepts that need to be clarified first. First, you need to create two basic services, Configuration Service and Discovery Service. As shown in the following figure:
The picture above illustrates four microservices and their dependencies:
Configuration service is at the top, marked in yellow, and is dependent on other microservices
Discovery service is at the lowest end, marked in blue, and is also dependent on other services
The two micro-services of the green logo are two application cases used in this series of blog posts: movies and movie-watching suggestions.
Configuration service
Configuration Service (configuration Service) is a very important component in the micro-service architecture. As stated in the 12-element theory, the configuration of microservice applications should be stored in the environment, not in local projects. The reason why Configuration service is an essential basic component is that it can manage all basic services through peer-to-peer and retrieval.
Suppose we have multiple deployment environments. For example, we have a temporary environment and a production environment, and the configuration for each environment will be different. Each configuration service will be stored in a separate Git repository for the environment configuration. No other environment can access this configuration repository, it just provides configuration services running in that environment.
When Configuration service starts, it will point to those paths configured according to the configuration file and start the corresponding service. Each microservice runs by reading the specific environment in its own configuration file. In this process, the configuration is managed internally and centrally through version management, and changing the configuration does not require a restart of the service.
Through the service terminal provided by Spring Cloud, you can change the environment configuration and send a refresh signal to Discovery service (Discovery Service), and all users will be notified of the new configuration.
Discovery service
Discovery Service (Discovery Service) is another important component of the microservice architecture. Discovery Service manages a number of service instances running in containers that work in a clustered environment. In these applications, the way we use the client is called from service to service. For example, I use Spring Cloud Feign, a client-side open source project based on Restful-style microservices that is derived from the Netflix OSS project project.
[java] view plain copy looks at the code chip derived from my code chip on CODE
@ FeignClient ("movie")
Public interface MovieClient {
@ RequestMapping (method = RequestMethod.GET, value = "/ movies")
PagedResources findAll ()
@ RequestMapping (method = RequestMethod.GET, value = "/ movies/ {id}")
Movie findById (@ RequestParam ("id") String id)
@ RequestMapping (method = RequestMethod.POST, value = "/ movies"
Produces = MediaType.APPLICATION_JSON_VALUE)
Void createMovie (@ RequestBody Movie movie)
}
In the above example, I created a Feign client and mapped a REST API method to expose the movie service. Using the @ FeignClient annotation, I can declare the client API I want to create for the moviemicro service. Next I declare a service mapping that I want to implement. Describe a URL routing rule by declaring a REST API rule on the method.
Even more exciting, it's all easy in Spring Cloud, and all I have to do is know service ID to create my Feign client. The URL address of the service is automatically configured in the runtime environment, because each microservice in the cluster will be registered by binding serviceid at startup.
Other services in the microservices architecture also run in the manner mentioned above. I just need to know the serviceid for the communication service, and all operations are automatically bound through Spring.
API Gateway
API Gateway services are another important component of Spring Cloud. It can be used to manage domain entities in cluster services. The green hexagon in the following figure is a data-driven service we provide, which is mainly used to manage your own entity classes and databases. By adding an API Gateway service, we can create a proxy exposure interface for each API route through the green service below.
Suppose that both the recommendation service and the movie service expose their own REST API on the domain entities they manage. API gataway is a proxy routing-based API method injected through discovery service and from other services. In this way, including recommendation services and movie services, will have a fully defined route to obtain local micro-services through the exposed REST API. API Gateway will redefine routing requests to the service instance, which are based on HTTP.
Sample project
I have been in GitHub (https://github.com/kbastani/sp... An instance project has been created on ample, an end-to-end native cloud platform that uses Spring Cloud to build an actual micro-service architecture.
Basic concepts:
Using Docker for integration testing
Mixed persistence
Micro-service architecture
Service discovery
API Gateway
Docker
Use Docker to build and deploy each service. Use Docker Compose for end-to-end integration testing on a development machine.
Mixed persistence
Mixed persistence actually means using multiple databases to store it. Different microservice instances use their own databases and communicate through REST services or message buses. For example, you can build microservices based on the following databases:
Neo4j (graphical)
MongoDB (documented)
MySQL (Associated)
Micro-service architecture
This example demonstrates how to create a new application using microservices. Because each microservice in the project has only a single parent project. The benefit for developers is that they can run and develop every microservice on the local machine. Adding a new microservice is very simple, and when a microservice is found, it will be automatically discovered on the runtime cluster environment.
Discovery service
The project contains two discovery services, one is Netflix Eureka and the other uses Consul from Hashicorp. A variety of discovery services provide a variety of options, one is to use (Consul) to cluster DNS services, and the other is (Eureka) a proxy-based API gateway.
API Gateway
Each microservice is associated with an Eureka to retrieve API routes throughout the cluster. Using this strategy, each micro-service running on the cluster only needs to load balance and expose the interface through a common API gateway, and each service will automatically discover and forward routing requests to its own routing service. This agent technology helps to develop the user interface, and the complete API as a platform is mapped to agent service through its own host.
Docker instance
The following example will be built through Maven, using Docker to build a container image for each micro-service. We can elegantly use Docker Compose to build all the micro-service clusters on our own hosts.
Start building
Before you do that, please move to the project's GitHub warehouse.
[html] view plain copy looks at the code chip derived from my code chip on CODE
Https://github.com/kbastani/sp... MpleX24X clone or fork this project and download the source code to your computer. After downloading, you need to use Maven and Docker to compile and build the local container image.
Download Docker
First of all, if you don't have Docker, please download it first. You can follow this guide (https://docs.docker.com/compose/install/) to get Docker, and then install and run it on the development machine.
Of course, you also need to install Docker Compose, and this https://docs.docker.com/compose/install/ will help you. If you are using Mac OSX or boot2docker, make sure that the memory you provide to boot2docker on the virtual machine is at least 5GB. The following command will help you do this:
[html] view plain copy looks at the code chip derived from my code chip on CODE
$boot2docker init-memory=5000
Environmental requirements
To be able to run the example program, you need to install the following software on your development machine:
Maven 3
Java 8
Docker
Docker Compose
Build a project
To build the current project from the command line, run the following command in the root directory of the project:
[html] view plain copy looks at the code chip derived from my code chip on CODE
$mvn clean install
The project will download the corresponding dependent jar package according to each project declaration in pom.xml. Each service will be built, and Maven's Docker plug-in will automatically build each container image from the local Docker Registry. Docker will run mvn clean install according to the command line to clear the appropriate resources after the build is successful.
After the project is successfully built, you will see the following output:
[html] view plain copy looks at the code chip derived from my code chip on CODE
[INFO]-[INFO] Reactor Summary:
[INFO]
[INFO] spring-cloud-microservice-example-parent. SUCCESS [0.268s]
[INFO] users-microservice.. SUCCESS [11.929s]
[INFO] discovery-microservice.. SUCCESS [5.640s]
[INFO] api-gateway-microservice.. SUCCESS [5.156s]
[INFO] recommendation-microservice... SUCCESS [7.732s]
[INFO] config-microservice.. SUCCESS [4.711s]
[INFO] hystrix-dashboard.. SUCCESS [4.251s]
[INFO] consul-microservice.. SUCCESS [6.763s]
[INFO] movie-microservice.. SUCCESS [8.359s]
[INFO] movies-ui.. SUCCESS [15.833s]
[INFO]-[INFO] BUILD SUCCESS
[INFO]-
Start the cluster through Docker compose
Now that each image has been successfully built, we use Docker Compose to speed up the startup of our cluster. I have included the yaml file of Docker Compose in the project, which you can get from GitHub.
Now let's start the micro-service cluster with the following command line:
[html] view plain copy looks at the code chip derived from my code chip on CODE
$docker-compose up
If everything is configured correctly, each container image will be run through a virtual container on the Docker and an automatically discovered network service. When they start starting sequentially, you will see a series of log outputs. This may take some time to complete, depending on the performance of the machine running your example program.
Once the container starts successfully, you will see the application service registered through Discovery service through the Eureka host, and copy and paste the following command into the $DOCKER_HOST environment variable defined in Docker through the command line terminal.
[html] view plain copy looks at the code chip derived from my code chip on CODE
$open $(echo\ "$(echo $DOCKER_HOST)\" |
\ sed 's/tcp:\ / http:\ /\ / / g' |
\ sed's / [0-9] {4,} / 8761 Universe g' |
\ sed's /\ "/ / g')
If Eureka starts correctly, the browser will launch and open the dashboard of the Eureka service, as shown in the following figure:
We will see each running service instance and status. Use the following command to get a data-driven service, such as a movie service.
[html] view plain copy looks at the code chip derived from my code chip on CODE
$open $(echo\ "$(echo $DOCKER_HOST) / movie\" |
\ sed 's/tcp:\ / http:\ /\ / / g' |
\ sed's / [0-9] {4,} / 10000 lapg'|
\ sed's /\ "/ / g')
This command will access the REST API terminal that accesses the movie service according to the proxy provided by the navigation gateway terminal. These REST API are configured using HATEOAS, which is an interface that supports auto-discovery of services through embedded links.
[html] view plain copy looks at the code chip derived from my code chip on CODE
{
"_ links": {
"self": {
"href": "http://192.168.59.103:10000/movie"X63X},"
"resume": {
"href": "http://192.168.59.103:10000/movie/resume"X66X},"
.
"autoconfig": {
"href": "http://192.168.59.103:10000/mo... t TX X70X}
}
}
That's all for "how to use Spring Cloud and Docker to build a micro-service platform". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.