|
| 1 | += MicroProfile Tutorial - Developers Guide |
| 2 | +:toc: macro |
| 3 | +:toc-title: Table of Contents |
| 4 | +:toclevels: 3 |
| 5 | +:doctype: book |
| 6 | + |
| 7 | +This guide provides information for developers who want to download and work with the MicroProfile e-commerce application source code. Each chapter of the tutorial includes practical code examples that demonstrate various MicroProfile features. |
| 8 | + |
| 9 | +toc::[] |
| 10 | + |
| 11 | +## Overview |
| 12 | + |
| 13 | +The MicroProfile Tutorial includes a comprehensive e-commerce application built with Jakarta EE 10 and MicroProfile 7.1. The application demonstrates real-world use of MicroProfile features through a multi-service architecture that includes catalog, inventory, user, payment, and order services. |
| 14 | + |
| 15 | +## Code Organization |
| 16 | + |
| 17 | +The source code for the tutorial is organized by chapter in the `code/` directory. Each chapter builds upon the previous one, introducing new MicroProfile features and concepts: |
| 18 | + |
| 19 | +``` |
| 20 | +code/ |
| 21 | +├── chapter02/ # Getting Started |
| 22 | +├── chapter03/ # Jakarta EE 10 Core Profile |
| 23 | +├── chapter04/ # OpenAPI |
| 24 | +├── chapter05/ # Configuration |
| 25 | +├── chapter06/ # Health Checks |
| 26 | +├── chapter07/ # Metrics |
| 27 | +├── chapter08/ # Fault Tolerance |
| 28 | +├── chapter09/ # Telemetry |
| 29 | +├── chapter10/ # JWT Authentication |
| 30 | +├── chapter11/ # REST Client |
| 31 | +├── chapter12/ # Graph QL |
| 32 | +└── chapter13/ # Reactive Messaging |
| 33 | + |
| 34 | +``` |
| 35 | + |
| 36 | +### Chapter Structure |
| 37 | + |
| 38 | +Each chapter directory contains one or more microservices with the following structure: |
| 39 | + |
| 40 | +``` |
| 41 | +chapterXX/ |
| 42 | +├── <service-name>/ |
| 43 | +│ ├── src/ |
| 44 | +│ │ ├── main/ |
| 45 | +│ │ │ ├── java/ # Java source code |
| 46 | +│ │ │ ├── liberty/config/ # Open Liberty configuration |
| 47 | +│ │ │ └── resources/ # Application resources |
| 48 | +│ │ └── test/ # Unit and integration tests |
| 49 | +│ ├── pom.xml # Maven build configuration |
| 50 | +│ └── README.adoc # Service-specific documentation |
| 51 | +``` |
| 52 | + |
| 53 | +## Getting Started |
| 54 | + |
| 55 | +### Prerequisites |
| 56 | + |
| 57 | +Before working with the code, ensure you have the following installed: |
| 58 | + |
| 59 | +#### Required Software |
| 60 | + |
| 61 | +* **Java Development Kit (JDK) 21 or later** |
| 62 | +* **Maven 3.9.12 or later** - For building the applications |
| 63 | +* **Git** - For cloning the repository |
| 64 | +
|
| 65 | +#### Development Environment Setup |
| 66 | + |
| 67 | +We recommend using SDKMAN for managing Java versions, as it makes it easy to switch between different JDK versions. |
| 68 | + |
| 69 | +##### Installing SDKMAN |
| 70 | + |
| 71 | +If SDKMAN is not already installed, follow the official instructions at https://sdkman.io/install/ or run: |
| 72 | + |
| 73 | +[source,bash] |
| 74 | +---- |
| 75 | +curl -s "https://get.sdkman.io" | bash |
| 76 | +source "$HOME/.sdkman/bin/sdkman-init.sh" |
| 77 | +---- |
| 78 | + |
| 79 | +##### Installing and Managing Java with SDKMAN |
| 80 | + |
| 81 | +[source,bash] |
| 82 | +---- |
| 83 | +# List available Java versions |
| 84 | +sdk list java |
| 85 | +
|
| 86 | +# Install JDK 21 (Microsoft build) |
| 87 | +sdk install java 21.0.11-ms |
| 88 | +
|
| 89 | +# List installed Java versions |
| 90 | +sdk list java | grep -E ".*\s+installed" |
| 91 | +
|
| 92 | +# Switch to JDK 21 for the current shell session |
| 93 | +sdk use java 21.0.11-ms |
| 94 | +
|
| 95 | +# Make JDK 21 the default for all sessions |
| 96 | +sdk default java 21.0.11-ms |
| 97 | +
|
| 98 | +# Verify the current Java version |
| 99 | +java -version |
| 100 | +---- |
| 101 | + |
| 102 | +##### Recommended IDEs and Extensions |
| 103 | + |
| 104 | +**Visual Studio Code:** |
| 105 | + |
| 106 | +* Java Extension Pack |
| 107 | +* Language Support for Java by Red Hat |
| 108 | +* Liberty Tools for VS Code |
| 109 | +* AsciiDoc extension (for viewing documentation) |
| 110 | +
|
| 111 | +**IntelliJ IDEA:** |
| 112 | + |
| 113 | +* Built-in Java and Maven support |
| 114 | +* MicroProfile plugin (available in the marketplace) |
| 115 | +* Liberty plugin (available in the marketplace) |
| 116 | +
|
| 117 | +**Eclipse:** |
| 118 | + |
| 119 | +* Eclipse IDE for Enterprise Java and Web Developers |
| 120 | +* Liberty Developer Tools |
| 121 | +
|
| 122 | +## Downloading the Source Code |
| 123 | + |
| 124 | +### Clone the Repository |
| 125 | + |
| 126 | +To get all the code for the tutorial: |
| 127 | + |
| 128 | +[source,bash] |
| 129 | +---- |
| 130 | +git clone https://github.com/microprofile/microprofile-tutorial.git |
| 131 | +cd microprofile-tutorial |
| 132 | +---- |
| 133 | + |
| 134 | +### Navigate to a Specific Chapter |
| 135 | + |
| 136 | +[source,bash] |
| 137 | +---- |
| 138 | +# For example, to work with Chapter 3 code |
| 139 | +cd code/chapter03/catalog |
| 140 | +---- |
| 141 | + |
| 142 | +## Building and Running the Code |
| 143 | + |
| 144 | +The tutorial uses Maven and the Open Liberty Maven plugin to build and run the application. |
| 145 | + |
| 146 | +### Building an Application |
| 147 | + |
| 148 | +To build any microservice, navigate to its directory and run: |
| 149 | + |
| 150 | +[source,bash] |
| 151 | +---- |
| 152 | +mvn clean package |
| 153 | +---- |
| 154 | + |
| 155 | +This command: |
| 156 | +- Compiles the Java source code |
| 157 | +- Runs unit tests |
| 158 | +- Packages the application as a WAR file |
| 159 | + |
| 160 | +### Running an Application |
| 161 | + |
| 162 | +The easiest way to run an application is using the Liberty Maven plugin in development mode: |
| 163 | + |
| 164 | +[source,bash] |
| 165 | +---- |
| 166 | +mvn liberty:dev |
| 167 | +---- |
| 168 | + |
| 169 | +**Development mode features:** |
| 170 | + |
| 171 | +- Automatically rebuilds and redeploys on code changes |
| 172 | +- Runs tests on demand by pressing Enter |
| 173 | +- Supports hot reload for faster development |
| 174 | +- Provides debugging capabilities |
| 175 | +
|
| 176 | +To exit development mode, press `Ctrl+C` or type `q` and press Enter. |
| 177 | + |
| 178 | +### Accessing the Application |
| 179 | + |
| 180 | +Once the server is running, you can access the application endpoints. The default configuration typically uses: |
| 181 | + |
| 182 | +* **Host**: localhost (or your system hostname) |
| 183 | +* **Port**: Varies by service (commonly 9080) |
| 184 | +
|
| 185 | +For example, for the catalog service in Chapter 3: |
| 186 | +``` |
| 187 | +http://localhost:9080/catalog/api/products |
| 188 | +``` |
| 189 | + |
| 190 | +Replace `localhost` with your system hostname if accessing from a different machine. |
| 191 | + |
| 192 | +### Running Tests |
| 193 | + |
| 194 | +To run the unit and integration tests: |
| 195 | + |
| 196 | +[source,bash] |
| 197 | +---- |
| 198 | +# Run all tests |
| 199 | +mvn test |
| 200 | +
|
| 201 | +# Run a specific test class |
| 202 | +mvn test -Dtest=YourTestClassName |
| 203 | +---- |
| 204 | + |
| 205 | +## Working with Individual Chapters |
| 206 | + |
| 207 | +Each chapter introduces specific MicroProfile features. |
| 208 | + |
| 209 | +## Common Development Tasks |
| 210 | + |
| 211 | +### Changing Server Ports |
| 212 | + |
| 213 | +If you need to run multiple services simultaneously or avoid port conflicts, you can change the HTTP port in the Liberty server configuration. |
| 214 | + |
| 215 | +Edit `src/main/liberty/config/server.xml`: |
| 216 | + |
| 217 | +[source,xml] |
| 218 | +---- |
| 219 | +<httpEndpoint id="defaultHttpEndpoint" |
| 220 | + host="*" |
| 221 | + httpPort="9080" <!-- Change this port --> |
| 222 | + httpsPort="9443" /> |
| 223 | +---- |
| 224 | + |
| 225 | +### Configuring the Database |
| 226 | + |
| 227 | +Most chapters use the Derby embedded database for simplicity. The database configuration is in `src/main/resources/META-INF/persistence.xml`: |
| 228 | + |
| 229 | +[source,xml] |
| 230 | +---- |
| 231 | +<persistence-unit name="prod" transaction-type="JTA"> |
| 232 | + <jta-data-source>jdbc/DefaultDataSource</jta-data-source> |
| 233 | + <properties> |
| 234 | + <!-- JPA and database properties --> |
| 235 | + </properties> |
| 236 | +</persistence-unit> |
| 237 | +---- |
| 238 | + |
| 239 | +### Adding Dependencies |
| 240 | + |
| 241 | +To add new dependencies, edit the `pom.xml` file. The tutorial uses Maven for dependency management: |
| 242 | + |
| 243 | +[source,xml] |
| 244 | +---- |
| 245 | +<dependencies> |
| 246 | + <!-- Add your dependencies here --> |
| 247 | + <dependency> |
| 248 | + <groupId>org.example</groupId> |
| 249 | + <artifactId>example-library</artifactId> |
| 250 | + <version>1.0.0</version> |
| 251 | + </dependency> |
| 252 | +</dependencies> |
| 253 | +---- |
| 254 | + |
| 255 | +## Technology Stack |
| 256 | + |
| 257 | +The MicroProfile Tutorial applications use the following technology stack: |
| 258 | + |
| 259 | +### Core Technologies |
| 260 | + |
| 261 | +* **Jakarta EE 10** - Enterprise Java specifications |
| 262 | +* **MicroProfile 7.1** - Cloud-native microservices specifications |
| 263 | +* **Maven** - Build and dependency management |
| 264 | +
|
| 265 | +### Jakarta EE APIs Used |
| 266 | + |
| 267 | +* **Jakarta RESTful Web Services (JAX-RS)** - REST API development |
| 268 | +* **Jakarta Persistence (JPA)** - Database persistence |
| 269 | +* **Jakarta Contexts and Dependency Injection (CDI)** - Dependency injection |
| 270 | +* **Jakarta Bean Validation** - Input validation |
| 271 | +* **Jakarta JSON Binding (JSON-B)** - JSON serialization |
| 272 | +* **Jakarta JSON Processing (JSON-P)** - JSON processing |
| 273 | +
|
| 274 | +### MicroProfile APIs Used |
| 275 | + |
| 276 | +* **MicroProfile Config** - Configuration management |
| 277 | +* **MicroProfile Health** - Health check endpoints |
| 278 | +* **MicroProfile Metrics** - Application metrics |
| 279 | +* **MicroProfile OpenAPI** - API documentation |
| 280 | +* **MicroProfile JWT** - JWT authentication |
| 281 | +* **MicroProfile Fault Tolerance** - Resilience patterns |
| 282 | +* **MicroProfile REST Client** - Type-safe REST clients |
| 283 | +* **MicroProfile GraphQL** - GraphQL based calls |
| 284 | +* **MicroProfile Reactive Messaging** - Event Driven Architecture and Reactive Messaging using Asynchronous calls |
| 285 | +
|
| 286 | +### Additional Libraries |
| 287 | + |
| 288 | +* **Lombok** - Reduces boilerplate code with annotations |
| 289 | +* **JUnit 5** - Unit testing framework |
| 290 | +* **Derby** - Embedded database (for development) |
| 291 | +
|
| 292 | +## Best Practices |
| 293 | + |
| 294 | +When working with the tutorial code, consider these best practices: |
| 295 | + |
| 296 | +### Development Workflow |
| 297 | + |
| 298 | +1. **Start with Early Chapters**: If you're new to MicroProfile, start with Chapter 2 and progress sequentially. |
| 299 | +2. **Read the Documentation**: Each service includes a README.adoc with specific instructions and explanations. |
| 300 | +3. **Experiment**: The code is designed for learning - modify it, break it, and understand how it works. |
| 301 | +
|
| 302 | +### Code Organization |
| 303 | + |
| 304 | +1. **Follow the Layered Architecture**: The code uses a clear separation between resources, services, repositories, and entities. |
| 305 | +2. **Keep Services Small**: Each microservice focuses on a specific business capability. |
| 306 | +3. **Use DTOs When Appropriate**: Don't expose entity classes directly in REST APIs for complex scenarios. |
| 307 | +
|
| 308 | +### Testing |
| 309 | + |
| 310 | +1. **Write Tests**: Add unit tests for your changes to ensure they work correctly. |
| 311 | +2. **Test in Development Mode**: Use the interactive testing feature in `mvn liberty:dev` by pressing Enter. |
| 312 | +3. **Integration Tests**: Some chapters include integration tests that verify end-to-end functionality. |
| 313 | +
|
| 314 | +### Configuration |
| 315 | + |
| 316 | +1. **Externalize Configuration**: Use MicroProfile Config for values that may vary across environments. |
| 317 | +2. **Use Profiles**: Configure different settings for development, testing, and production. |
| 318 | +3. **Document Configuration**: Keep track of required configuration properties. |
| 319 | +
|
| 320 | +## Troubleshooting |
| 321 | + |
| 322 | +### Common Issues and Solutions |
| 323 | + |
| 324 | +#### Port Already in Use |
| 325 | + |
| 326 | +**Problem**: Error message indicates the port is already in use. |
| 327 | + |
| 328 | +**Solution**: |
| 329 | +```bash |
| 330 | +# Find process using the port (e.g., 9080) |
| 331 | +lsof -i :9080 |
| 332 | + |
| 333 | +# Kill the process |
| 334 | +kill <PID> |
| 335 | + |
| 336 | +# Or change the port in server.xml |
| 337 | +``` |
| 338 | + |
| 339 | +#### Maven Build Failures |
| 340 | + |
| 341 | +**Problem**: Maven fails to download dependencies or build the project. |
| 342 | + |
| 343 | +**Solution**: |
| 344 | +```bash |
| 345 | +# Clean Maven cache and rebuild |
| 346 | +mvn clean install -U |
| 347 | + |
| 348 | +# Verify Maven is properly configured |
| 349 | +mvn -version |
| 350 | + |
| 351 | +# Check your internet connection and proxy settings if behind a firewall |
| 352 | +``` |
| 353 | + |
| 354 | +#### Database Connection Errors |
| 355 | + |
| 356 | +**Problem**: Application cannot connect to the database. |
| 357 | + |
| 358 | +**Solution**: |
| 359 | + |
| 360 | +- Verify the database configuration in `persistence.xml` |
| 361 | +- Check that Derby is included in dependencies |
| 362 | +- Look for database lock files that might need deletion |
| 363 | +- Review server logs for detailed error messages |
| 364 | +
|
| 365 | +## Next Steps |
| 366 | + |
| 367 | +After setting up your development environment and exploring the code: |
| 368 | + |
| 369 | +1. **Run the Examples**: Start with Chapter 2 and work through each chapter sequentially |
| 370 | +2. **Modify the Code**: Try adding new features or changing existing functionality |
| 371 | +3. **Build Your Own Service**: Create a new microservice using the patterns learned |
| 372 | +4. **Integrate Services**: Practice calling one service from another using the REST Client |
| 373 | +5. **Deploy to Production**: Learn about deploying MicroProfile applications to cloud platforms |
| 374 | +
|
| 375 | +## Additional Resources |
| 376 | + |
| 377 | +### Official Documentation |
| 378 | + |
| 379 | +* **MicroProfile**: https://microprofile.io |
| 380 | +* **Jakarta EE**: https://jakarta.ee |
| 381 | +
|
| 382 | +## Contributing |
| 383 | + |
| 384 | +If you find issues with the code or have suggestions for improvements: |
| 385 | + |
| 386 | +1. Check existing issues in the GitHub repository |
| 387 | +2. Create a new issue with: |
| 388 | + - Clear description of the problem or suggestion |
| 389 | + - Steps to reproduce (for bugs) |
| 390 | + - Expected vs. actual behavior |
| 391 | +3. Consider submitting a pull request with fixes or enhancements |
| 392 | +
|
| 393 | +For documentation improvements, see the link:CONTRIBUTORS.adoc[Contributors Guide]. |
0 commit comments