Creating a server
Table of contents
As the first step of the tutorial, we create a server instance and run a dummy service to check that the server and service are launched.
We'll use Armeria's ServerBuilder
for this task.
What you need
No preparation is required for this step. Do check that you've prepared the prerequisites.
1. Create a server instance
Let's create a server instance using Armeria's ServerBuilder
:
Create a
Main
class. You can see the full version of the file here.Main.javapackage example.armeria.server.blog; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public final class Main { private static final Logger logger = LoggerFactory.getLogger(Main.class); }
In the
Main
class, add the method,newServer()
. Since a server instance requires at least one service to run with, let's add a dummy service returning"Hello, Armeria!"
for now.Main.javaimport com.linecorp.armeria.common.HttpResponse; import com.linecorp.armeria.server.Server; import com.linecorp.armeria.server.ServerBuilder; public final class Main { ... static Server newServer(int port) { ServerBuilder sb = Server.builder(); return sb.http(port) .service("/", (ctx, req) -> HttpResponse.of("Hello, Armeria!")) .build(); } ...
To learn how to add the actual blog service, see Step 3. Add services to a server.
2. Start the server
Now that we have a server, have a go at starting the server with the dummy service.
- In the
Main
class, add themain()
method. - Call the
newServer()
method we implemented in step 2. Let's set the port to8080
as in line 2. - Start the server as in line 9.
1public static void main(String[] args) throws Exception {
2 Server server = newServer(8080);
3
4 server.closeOnJvmShutdown();
5
6 server.start().join();
7
8 logger.info("Server has been started. Serving dummy service at http://127.0.0.1:{}",
9 server.activeLocalPort());
10}
3. Run the server and service
As the last step, launch the server and run the service to check if you're all set to go. To run the server, run the main()
method in your IDE. The server and service are launched successfully if you see the following message.
Server has been started. Serving dummy service at http://127.0.0.1:8080
Open the URL http://127.0.0.1:8080
on a web browser and check the message Hello, Armeria!
is displayed in the page.
What's next
In this step, you've built a server, added a dummy service and launched a server.
Next, at Step 2. Prepare a data object, you'll write a data object to specify and contain blog post information.