Microservice Resume
Microservice Resume
Key components:
@GetMapping("/{name}")
public String greet(@PathVariable String name) {
return "Hello, " + name + "!";
}
}
9. Learning Resources
● Spring Boot Documentation: Spring Boot Reference
● Spring Cloud Documentation: Spring Cloud Reference
● Books: "Spring Microservices in Action" by John Carnell is a good book to start with.
● Video Tutorials: There are various online platforms like Udemy or Coursera where
you can find courses dedicated to building microservices using Spring Boot.
Would you like to dive deeper into any specific topic or build a sample project?
4o
Dijiste:
I need to start learning microservices con spring boot java
ChatGPT dijo:
ChatGPT
Memoria actualizada
To start learning microservices with Spring Boot and Java, here's a structured approach that
can guide you step by step:
@GetMapping("/hello")
public String hello() {
return "Hello, Microservice!";
}
}
2.
3. Run the Application:
○ Use mvn spring-boot:run to start the application or run it from your IDE.
○ Access your API via http://localhost:8080/api/hello.
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
1.
2. Add Eureka Client dependency in the services.
3. Set up the services to register with Eureka by adding
@EnableEurekaClient in each service’s main class.
4. Configure application.yml or application.properties to connect
the services to Eureka.
● REST Communication:
○ Use Spring Cloud OpenFeign to make HTTP requests between services
easily.
Example:
java
Copiar código
@FeignClient(name = "order-service")
public interface OrderClient {
@GetMapping("/orders")
List<Order> getOrders();
}
○
● Load Balancing with Ribbon: When using multiple instances of the same service,
Ribbon can help distribute requests.
Example:
java
Copiar código
@CircuitBreaker(name = "orderService", fallbackMethod = "fallback")
public List<Order> getOrders() {
return orderClient.getOrders();
}
Learning Path:
1. Core Java → 2. Spring Boot Basics → 3. Create Microservices → 4. Service
Discovery → 5. Inter-service Communication → 6. API Gateway → 7. Fault
Tolerance → 8. Centralized Config → 9. Logging & Monitoring → 10. Docker &
Kubernetes.
You can use Spring Initializr (start.spring.io) to create the user-service project with the
following dependencies:
Application Class
java
Copiar código
@SpringBootApplication
public class UserServiceApplication {
User Entity
java
Copiar código
@Entity
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
User Service
java
Copiar código
@Service
public class UserService {
@Autowired
private UserRepository userRepository;
User Controller
java
Copiar código
@RestController
@RequestMapping("/users")
public class UserController {
@Autowired
private UserService userService;
@PostMapping
public ResponseEntity<User> createUser(@RequestBody User user) {
User createdUser = userService.createUser(user);
return ResponseEntity.ok(createdUser);
}
@GetMapping("/{id}")
public ResponseEntity<User> getUserById(@PathVariable Long id) {
return userService.getUserById(id)
.map(ResponseEntity::ok)
.orElse(ResponseEntity.notFound().build());
}
}
Application Properties
properties
Copiar código
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=password
spring.h2.console.enabled=true
spring.jpa.hibernate.ddl-auto=update
You can use Spring Initializr again for the order-service project with the following
dependencies:
● Spring Web
● Spring Data JPA
● OpenFeign: To communicate with the User Service.
Application Class
java
Copiar código
@SpringBootApplication
@EnableFeignClients // Enable Feign Client for communication with
User Service
public class OrderServiceApplication {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
Order Repository
java
Copiar código
@Repository
public interface OrderRepository extends JpaRepository<Order, Long>
{
}
Order Service
java
Copiar código
@Service
public class OrderService {
@Autowired
private OrderRepository orderRepository;
@Autowired
private UserClient userClient;
Order Controller
java
Copiar código
@RestController
@RequestMapping("/orders")
public class OrderController {
@Autowired
private OrderService orderService;
@PostMapping
public ResponseEntity<Order> createOrder(@RequestBody Order
order) {
Order createdOrder = orderService.createOrder(order);
return ResponseEntity.ok(createdOrder);
}
}
@GetMapping("/users/{id}")
User getUserById(@PathVariable Long id);
}
Application Properties
properties
Copiar código
# Feign configuration for the User Service communication
feign.client.config.default.connectTimeout=5000
feign.client.config.default.readTimeout=5000
●
● The Order Service will call the User Service to check if the user exists, and then save
the order if the user is valid.
This setup demonstrates how two microservices (User Service and Order Service) interact
using Feign for inter-service communication. You can expand this by adding features like
Eureka for service discovery, or implement circuit breakers for fault tolerance using
Resilience4j.