In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "how to use Ignite+SpringCloud+Docker to create REST applications". In daily operation, I believe many people have doubts about how to use Ignite+SpringCloud+Docker to create REST applications. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the questions of "how to use Ignite+SpringCloud+Docker to create REST applications". Next, please follow the editor to study!
This article will explain the process of creating RESTful Web services based on Spring Cloud and Ignite. The service uses Ignite as a containerized application of high-performance in-memory database, uses HashiCorp Consul for service discovery, and interacts with Ignite clusters through Spring Data repository abstraction, containerization is implemented through Docker.
Configure and start configuration Consul service registration
For convenience, you can use the official Docker image of Consul (you need to install Docker first):
Start the Docker image of Consul:
Docker run-d-p 8500 docker run-p 8600:8600/udp-- name=consul consul agent-server- ui-node=server-1-bootstrap-expect=1-client=0.0.0.0 to create a basic Spring Cloud application
You can start with https://start.spring.io to create the application.
Spring Web and Spring Consul Discovery modules are needed here. Spring Web is one of the simplest frameworks for creating REST applications. Spring Consul Discovery can combine multiple Spring application instances into a single service.
! [] (https://www.gridgain.com/sites/default/files/inline-images/image1.pn
Configure the project through Spring Initializr (as shown in the figure above)
Click the Generate button, and then download the zip file containing the project project
Use an IDE to open the package
(optional) replace the application.properties file with application.yml, and of course you can continue to use the properties file format, but the YAML format will be used later in this article.
In application.yml, basic parameters are added to Sprint Cloud, the application is named and Consul service discovery is enabled:
Spring: application: name: myapp instance_id: 1 cloud: consul: enabled: true service-registry: enabled: true add Ignite dependency
You can obtain the latest version of Ignite from the Maven central repository and add it to the build configuration file:
Implementation 'org.apache.ignite:ignite-spring-data_2.2:2.8.1'compile group:' org.apache.ignite', name: 'ignite-core', version:' 2.8.1'implementation 'org.apache.ignite:ignite-spring-boot-autoconfigure-ext:1.0.0' configure Ignite node discovery
How do you make Ignite nodes discover each other? Ignite has an IpFinder component that handles node registration and discovery. See the Ignite documentation for more details.
To integrate IpFinder and Consul service registrations, you need to use the Spring Cloud Discovery module, which can be used with a large number of discovery services (for example, Zookeeper).
For HashiCorp Consul, there is a simple IpFinder implementation, but there are many third-party implementations of IpFinder on GitHub.
Configure Spring Cloud and Ignite
Because the automatic configuration module of Ignite is used, configuration is a relatively simple process, putting the following into the application.yml file:
Ignite: workDirectory: / opt/ignite/
Ignite stores its data in the working directory.
Now you need to add support for the Ignite Spring Data repository. One of the ways to add support is through Java's Configuration: simply add an EnableIgniteRepositories annotation and take the repository package as a parameter:
@ Configuration@EnableIgniteRepositories (value = "com.github.sammyvimes.bootnite.repo") public class IgniteConfig {}
There are some problems that need to be solved before compiling the application. The H2 version of Spring Data is not supported by Ignite. Therefore, the version of H2 (whether Gradle or Maven) must be reset in the build configuration:
Ext {set ('h3.versionproof,' 1.4.197')}
In addition, Spring Cloud Consul and hystrix have known problems, so you need to troubleshoot them:
Implementation ('org.springframework.cloud:spring-cloud-starter-consul-discovery') {exclude group:' org.springframework.cloud', module: 'spring-cloud-netflix-hystrix'}
Finally, there is one more problem with Spring Data BeanFactory: BeanFactory looks for a bean named igniteInstance, while autoconfiguration provides a bean named ignite, which will be resolved later.
For now, however, you need to make the following changes to the configuration class to solve the BeanFactory problem:
@ Configuration@EnableIgniteRepositories (value = "com.github.sammyvimes.bootnite.repo") public class IgniteConfig {@ Bean (name = "igniteInstance") public Ignite igniteInstance (Ignite ignite) {ignite.active (true); return ignite;}} create Ignite's Spring Data repository and service
Next, you will create a CRUD service based on the Ignite Spring Data repository.
The following starts with the most common example, the Employee class:
Public class Employee implements Serializable {private UUID id; private String name; private boolean isEmployed; / / constructor, getters & setters}
After defining the data model, add the configurator bean:
@ Bean public IgniteConfigurer configurer () {return igniteConfiguration-> {CacheConfiguration cache = new CacheConfiguration ("employeeCache"); cache.setIndexedTypes (UUID.class, Employee.class); igniteConfiguration.setCacheConfiguration (cache);};}
This way, when the application starts, the employeeCache cache is deployed, which contains indexes that speed up queries on Employee entities.
Next, like other Spring Data storage services, you need to create a repository and a service:
@ Repository@RepositoryConfig (cacheName = "employeeCache") public interface EmployeeRepository extends IgniteRepository {Employee getEmployeeById (UUID id);}
Notice the RepositoryConfig annotation, which links the repository to the cache to be used, which has previously been created using the employeeCache string as the name.
Here is a simple service that uses the repository:
@ Service@Transactionalpublic class EmployeeService {private final EmployeeRepository repository; / / constructor injection FTW public EmpoyeeService (final EmployeeRepository repository) {this.repository = repository;} public List findAll () {return StreamSupport.stream (repository.findAll (). Spliterator (), false) .requests (Collectors.toList ());} public Employee create (final String name) {final Employee employee = new Employee () Final UUID id = UUID.randomUUID (); employee.setId (id); employee.setEmployed (true); employee.setName (name); return repository.save (id, employee);}}
Here are two methods, create and findAll, to demonstrate the integration of Ignite and Spring Data.
Configure the REST controller
The following endpoints will be configured so that data can be accessed and modified, and a simple controller will do:
@ RestController@RequestMapping ("/ employee") public class EmployeeController {private final EmpoyeeService service; public EmployeeController (final EmployeeService service) {this.service = service;} @ GetMapping public ResponseEntity employees () {return ResponseEntity.ok (service.findAll ());} @ PostMapping public ResponseEntity create (@ RequestParam final String name) {return ResponseEntity.ok (service.create (name));}}
In this way, the application is finished.
You can now launch the application, create data by sending an POST request to / employee, and query the data by sending an GET request.
Although this application has high performance, what are the advantages of distributed storage?
Deploy applications in Docker
Application nodes can be created and managed through Docker.
First, create a Dockerfile for the application in the root directory of the project:
FROM adoptopenjdk:8-jre-hotspotRUN mkdir / opt/app & & mkdir / opt/igniteCOPY build/libs/bootnite-0.0.1-SNAPSHOT.jar / opt/app/app.jarCMD ["java", "- jar", "/ opt/app/app.jar"]
To put it simply: get OpenJDK, create a directory for the application, copy the application binaries to the image, and create a default command to start the application.
Now, in the root directory of the project, run gradle build and docker build-t ignite-test-app:0.1-f. Dockerfile.
Next, start two instances of the application at the same time through Docker.
You can help start the node by writing the following shell script:
MacOS:
Export HOST_IP=$ (ipconfig getifaddr en0) docker run-P-e HOST_IP=$ {HOST_IP}-e DISCO_PORT=$2-p $2pur47,500-- name $1 ignition
Linux:
Export HOST_IP=$ (ip-4 addr show docker0 | grep-Po 'inet\ K [\ d.] +') docker run-P-e HOST_IP=$ {HOST_IP}-e DISCO_PORT=$2-p $2addr show docker0 47500-name $1 ignition
What is done here is to obtain the host IP address so that port forwarding can be used and nodes can communicate with each other. Use the-p parameter to create a port forwarding rule for the Docker container and use the-e parameter to save the values of the external port in the container's environment so that these values can be used later in the following configuration:
Spring: application: name: myapp instance_id: 1 cloud: consul: enabled: true host: ${HOST_IP} service-registry: enabled: trueignition: disco: host: ${HOST_IP} port: ${DISCO_PORT}
The custom configuration parameters ignition.disco.host and ignition.disco.port are added here, which will be used in the custom IP probe. Note that the configuration of the Consul is also changed by adding the host IP address.
Now, after changing the Ignite Java configuration, you are done:
@ Bean public TcpDiscoveryConsulIpFinder finder (final ConsulDiscoveryClient client, final ConsulServiceRegistry registry, final ConsulDiscoveryProperties properties, @ Value ("${ignition.disco.host}") final String host @ Value ("${ignition.disco.port}") final int port) {return new TcpDiscoveryConsulIpFinder (client, registry, properties, host, port) } @ Bean public IgniteConfigurer configurer (final TcpDiscoveryConsulIpFinder finder) {return igniteConfiguration-> {CacheConfiguration cache = new CacheConfiguration ("employeeCache"); cache.setIndexedTypes (UUID.class, Employee.class); igniteConfiguration.setCacheConfiguration (cache); final TcpDiscoverySpi tcpDiscoverySpi = new TcpDiscoverySpi (); tcpDiscoverySpi.setIpFinder (finder); igniteConfiguration.setDiscoverySpi (tcpDiscoverySpi);};}
Perform the following tests to see if everything is all right:
Start an application instance:. / starter/start.sh test1 30000
Submit some data to the instance; for example, curl-d'name = admin'http:// localhost:32784/employee, different containers use different ports
Determine the port forwarding rules of the container by executing docker ps in the terminal (the rule is similar to 0.0.0.0 32784-> 8080/tcp), and determine which port to use
Start another node:. / starter/start.sh test2 30001
Stop the first node (if you are starting the container from a terminal, you can use Ctrl + c)
Execute the GET request to the second node and verify that the data obtained from this request is the same as the data obtained from the first request: curl http://localhost:32785/employee.
Note that the port in this curl command is different from the port in the previous curl command because the two application instances occupy different ports.
At this point, the study on "how to use Ignite+SpringCloud+Docker to create REST applications" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.