Spring Microservices
- Create SpringBoot Banner
- A Maven Archetype is like a blueprint for the project you want to build - contains libraries, APIs, dependencies, version, etc.
- https://maven.apache.org/guides/mini/guide-creating-archetypes.html
- Install Maven
brew install maven
- Create Archetype
mvn archetype:generate -DgroupId=com.kwebdev -DartifactId=kwebdevservices -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=1.4 -DinteractiveMode=false
docker compose up -d
- Terminal: spring-microservices % docker compose up -d
- [+] Running 29/2
- ✔ postgres 13 layers [⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿] 0B/0B Pulled 26.5s
- ✔ pgadmin 14 layers [⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿] 0B/0B Pulled 29.3s
- [+] Running 5/5
- ✔ Network spring-microservices_postgres Created 0.0s
- ✔ Volume "spring-microservices_pgadmin" Created 0.0s
- ✔ Volume "spring-microservices_postgres" Created 0.0s
- ✔ Container postgres Started 0.7s
- ✔ Container pgadmin Started
- Verify containers running
-
docker compose ps
-
NAME IMAGE COMMAND SERVICE CREATED STATUS PORTS pgadmin dpage/pgadmin4 "/entrypoint.sh" pgadmin 3 minutes ago Up 3 minutes 443/tcp, 0.0.0.0:5050->80/tcp postgres postgres "docker-entrypoint.s…" postgres 3 minutes ago Up 3 minutes 0.0.0.0:5432->5432/tcp -
in a new browser window go to
localhost:5050
and it should pull up apgAdmin
page to log in to ourpostgres database
- add a new server
- Hostname / address is
postgres
because that is the name of the network we defined in ourdocker-compose
file forpgAdmin
and ourpostgres DB
to talk to each other, otherwise it would belocalhost
-
- Once the Controller, Service and JPA Repository are up and running, ensure
postgres
is configured on the controller underresources/application.yml
- server: port: 8080 spring: application: name: customer datasource: password: password url: jdbc:postgresql://localhost:5432/customer username: amigoscode jpa: hibernate: ddl-auto: update properties: hibernate: dialect: org.hibernate.dialect.PostgreSQLDialect format_sql: true show-sql: true
- Create the database in the
pgAdmin
atlocalhost:5050
- In Postman send a
POST
request to insert an object into the DB and verify the insert is successful
-
-
The process of automatically detecting devices and services on a network so that each microservice does not have to manually keep track and constantly register new IP addresses
-
A Eureka Server manages all of this and acts as a middleman between all the microservices
-
As the network scales and the number of APIs and microservices scale, including duplicates, the Eureka Server can quickly become a Single Point of Failure that must be kept running at all times
-
Eureka Dashboard at
localhost:8761
-
- Eureka Server is running on
port: 8761
--> Dashboardlocalhost:8761
- Customer Service is running on
port: 8080
- Fraud Service is running on
port: 8081
- pgAdmin is running on
port: 5050
--> Dashboardlocalhost:5050
-
OpenFeign is a declarative web service client library for Java, which simplifies the process of making HTTP requests to RESTful web services. It is often used in microservices architectures and other distributed systems where one service needs to communicate with another over HTTP. OpenFeign provides a higher-level, more intuitive way to define and work with HTTP APIs compared to using lower-level HTTP client libraries.
-
Here are some key purposes and benefits of OpenFeign:
-
Declarative API Definition: OpenFeign allows you to define your API interfaces using annotations and Java interfaces. These interfaces define the structure of the HTTP requests and responses in a clean and human-readable way, making it easier to understand and work with APIs.
-
Integration with Existing Code: OpenFeign integrates seamlessly with popular Java frameworks like Spring and provides convenient ways to use HTTP-based microservices without writing boilerplate code.
-
Dynamic Proxy Generation: OpenFeign generates proxy classes at runtime based on your API interface definitions. These proxies handle the HTTP communication details, including serialization/deserialization of JSON or XML data, headers, and error handling.
-
Load Balancing: OpenFeign often works in conjunction with service discovery and load balancing tools (e.g., Netflix Eureka, Ribbon) to distribute requests across multiple instances of a service. This can improve the reliability and scalability of your microservices architecture.
-
Request and Response Mapping: You can use annotations like @RequestMapping, @PathVariable, and @RequestParam to map Java objects to HTTP requests and responses, simplifying the conversion process between Java objects and JSON/XML data.
-
Error Handling: OpenFeign provides mechanisms for handling errors and exceptions that may occur during API requests. You can define custom error-handling logic to handle different HTTP status codes or error responses.
-
Interceptors: You can use interceptors to modify or log requests and responses at runtime, which can be useful for debugging and monitoring purposes.
-
Request and Response Compression: OpenFeign supports compression of request and response payloads, reducing the amount of data transferred over the network.
-
Overall, OpenFeign simplifies the process of working with HTTP APIs in Java applications, making it a valuable tool for building and maintaining microservices, client applications, or any system that relies on HTTP-based communication.
-