Professional Documents
Culture Documents
High Cohesion: The service should have high cohesion, meaning that all of its
components should work closely together to achieve a specific goal.
class Microservice {
// Method to get the list of users
public function getUsers() {
// Connect to the database
$db = new PDO('mysql:host=localhost;dbname=mydb', 'username', 'password');
// Prepare the query
$stmt = $db->prepare('SELECT * FROM users');
// Execute the query
$stmt->execute();
// Return the list of users
return $stmt->fetchAll();
}
}
2. Microservices In JAVA
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
@Path("/users")
public class Microservice {
@GET
@Produces(MediaType.APPLICATION_JSON)
public List<User> getUsers() {
// Connect to the database
Connection conn =
DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb", "username",
"password");
// Prepare the query
PreparedStatement stmt = conn.prepareStatement("SELECT * FROM users");
// Execute the query
ResultSet rs = stmt.executeQuery();
// Create a list to hold the users
List<User> users = new ArrayList<User>();
// Iterate through the results
while (rs.next()) {
// Create a new user object
User user = new User();
// Set the user's properties
user.setId(rs.getInt("id"));
user.setName(rs.getString("name"));
// Add the user to the list
users.add(user);
}
// Return the list of users
return users;
}