Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.springframework.cloud.openfeign.EnableFeignClients;

@SpringBootApplication
@EnableFeignClients
public class AccountsMicroApplication {

public static void main(String[] args) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.ironhack.accounts_micro.clients;

import com.ironhack.accounts_micro.dto.UserDTO;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

@FeignClient(name = "users-micro")
public interface UserFeignClient {
@GetMapping("/api/user/{id}")
UserDTO getUserById(@PathVariable Long id);
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package com.ironhack.accounts_micro.service;

import com.ironhack.accounts_micro.clients.UserFeignClient;
import com.ironhack.accounts_micro.dto.AccountResponseDTO;
import com.ironhack.accounts_micro.dto.UserDTO;
import com.ironhack.accounts_micro.model.Account;
import com.ironhack.accounts_micro.repository.AccountRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.List;
import java.util.Optional;
Expand All @@ -16,6 +19,9 @@ public class AccountService {
@Autowired
private AccountRepository accountRepository;

@Autowired
private UserFeignClient userFeignClient;

public List<Account> getAllAccounts() {
return accountRepository.findAll();
}
Expand All @@ -27,7 +33,8 @@ public ResponseEntity<?> getAccountById(Long id) {
//UserDTO user = restTemplate.getForObject("http://users-micro/api/user/" + account.get().getOwnerId(), UserDTO.class);
// comentamos el código para usar feignClient

// INCLUIR AQUI EL FEIGN PARA HACER EL GET DE USER
UserDTO user = userFeignClient.getUserById(account.get().getOwnerId());

System.out.println("EL USUARIO ES: " + user);

AccountResponseDTO response = new AccountResponseDTO(account.get(), user);
Expand All @@ -36,8 +43,6 @@ public ResponseEntity<?> getAccountById(Long id) {
} else {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}


}

public Account createAccount(Account account) {
Expand All @@ -47,6 +52,4 @@ public Account createAccount(Account account) {
public void deleteAccount(Long id) {
accountRepository.deleteById(id);
}


}
2 changes: 1 addition & 1 deletion accounts_micro/src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ spring.application.name=accounts-micro
server.port=8081
spring.datasource.url=jdbc:mysql://localhost:3306/accounts_micro
spring.datasource.username=root
spring.datasource.password=admin
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL8Dialect
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,16 @@

@Configuration
public class GatewayConfig {
// ups! Alguien ha hackeado esto y ahora no tenemos la config

@Bean
public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {
return builder.routes()
.route("user", r -> r.path("/api/user/**")
.uri("lb://users-micro"))
.route("account", r -> r.path("/api/account/**")
.uri("lb://accounts-micro")
).build();
}
}

// ¿Qué es lb???
// LOAD BALANCER: Le estamos diciendo al gateway que use el load balancer para redirigir las peticiones a los microservicios repartiendo su carga.
2 changes: 1 addition & 1 deletion users_micro/src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
spring.application.name=users-micro
spring.datasource.url=jdbc:mysql://localhost:3306/users_micro
spring.datasource.username=root
spring.datasource.password=admin
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL8Dialect
spring.jpa.hibernate.ddl-auto=update
Expand Down