Building microservices from the monolith

Our monolith deals with User and UserAddress. So, we will break it down into three microservices— a user microservice, a user address microservice, and a gateway microservice. Let's see how:

  • The user microserviceThe User entity, UserBean, and UserService will remain exactly as they are in the monolith. Only, now, they will be delivered as a separate unit of deployment.
  • The user address microservice: The UserAddress classes will suffer just a single change from the monolith version, but keep their original APIs (that is great for the client).

    Here is the UserAddress entity:

@Entity
public class UserAddress implements Serializable {

private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;

@Column
private Long idUser;

@Column
private String street;

@Column
private String number;

@Column
private String city;

@Column
private String zip;

public UserAddress(){

}

public UserAddress(Long user, String street, String number,
String city, String zip) {
this.idUser = user;
this.street = street;
this.number = number;
this.city = city;
this.zip = zip;
}

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public Long getIdUser() {
return idUser;
}

public void setIdUser(Long user) {
this.idUser = user;
}

public String getStreet() {
return street;
}

public void setStreet(String street) {
this.street = street;
}

public String getNumber() {
return number;
}

public void setNumber(String number) {
this.number = number;
}

public String getCity() {
return city;
}

public void setCity(String city) {
this.city = city;
}

public String getZip() {
return zip;
}

public void setZip(String zip) {
this.zip = zip;
}
}

Note that User is no longer a property/field in the UserAddress entity, but only a number (idUser). We will get into more details about it in the following section.

  • The gateway microservice: The gateway service is an API between the application client and the services. Using it allows you to simplify this communication, also giving you the freedom of doing whatever you like with your services without breaking the API contracts (or at least minimizing them).
  1. First, we create a class that helps us to deal with the responses:
public class GatewayResponse {

private String response;
private String from;

public String getResponse() {
return response;
}

public void setResponse(String response) {
this.response = response;
}

public String getFrom() {
return from;
}

public void setFrom(String from) {
this.from = from;
}
}
  1. Then, we create our gateway service:
@Consumes(MediaType.APPLICATION_JSON)
@Path("gatewayResource")
@RequestScoped
public class GatewayResource {

private final String hostURI = "http://localhost:8080/";
private Client client;
private WebTarget targetUser;
private WebTarget targetAddress;

@PostConstruct
public void init() {
client = ClientBuilder.newClient();
targetUser = client.target(hostURI +
"ch08-micro_x_mono-micro-user/");
targetAddress = client.target(hostURI +
"ch08-micro_x_mono-micro-address/");
}

@PreDestroy
public void destroy(){
client.close();
}

@GET
@Path("getUsers")
@Produces(MediaType.APPLICATION_JSON)
public Response getUsers() {
WebTarget service =
targetUser.path("webresources/userService/get");

Response response;
try {
response = service.request().get();
} catch (ProcessingException e) {
return Response.status(408).build();
}

GatewayResponse gatewayResponse = new GatewayResponse();
gatewayResponse.setResponse(response.readEntity(String.class));
gatewayResponse.setFrom(targetUser.getUri().toString());

return Response.ok(gatewayResponse).build();
}

@POST
@Path("addAddress")
@Produces(MediaType.APPLICATION_JSON)
public Response addAddress(UserAddress address) {
WebTarget service =
targetAddress.path("webresources/userAddressService/add");

Response response;
try {
response = service.request().post(Entity.json(address));
} catch (ProcessingException e) {
return Response.status(408).build();
}

return Response.fromResponse(response).build();
}

}

As we receive the UserAddress entity in the gateway, we have to have a version of it in the gateway project too. For brevity, we will omit the code, as it is the same as in the UserAddress project.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset