2022-08-13 00:07:12 +08:00
# Managing Java Deployments
2022-08-12 17:09:40 -07:00
Java is a mainstream programming language for production services. Ray Serve offers a native Java API for creating, updating, and managing deployments. You can create Ray Serve deployments using Java and call them via Python, or vice versa.
2022-08-13 00:07:12 +08:00
This section helps you to:
2022-08-12 17:09:40 -07:00
- create, query, and update Java deployments
- configure Java deployment resources
- manage Python deployments using the Java API
2022-08-13 00:07:12 +08:00
```{contents}
```
## Creating a Deployment
2022-08-12 17:09:40 -07:00
By specifying the full name of the class as an argument to the `Serve.deployment()` method, as shown in the code below, you can create and deploy a deployment of the class.
2022-08-13 00:07:12 +08:00
```{literalinclude} ../../../java/serve/src/test/java/io/ray/serve/docdemo/ManageDeployment.java
2022-08-12 17:09:40 -07:00
:start-after: docs-create-start
2022-08-13 00:07:12 +08:00
:end-before: docs-create-end
:language: java
```
## Accessing a Deployment
Once a deployment is deployed, you can fetch its instance by name.
```{literalinclude} ../../../java/serve/src/test/java/io/ray/serve/docdemo/ManageDeployment.java
2022-08-12 17:09:40 -07:00
:start-after: docs-query-start
2022-08-13 00:07:12 +08:00
:end-before: docs-query-end
:language: java
```
## Updating a Deployment
2022-08-12 17:09:40 -07:00
You can update a deployment's code and configuration and then redeploy it. The following example updates the `"counter"` deployment's initial value to 2.
2022-08-13 00:07:12 +08:00
```{literalinclude} ../../../java/serve/src/test/java/io/ray/serve/docdemo/ManageDeployment.java
2022-08-12 17:09:40 -07:00
:start-after: docs-update-start
2022-08-13 00:07:12 +08:00
:end-before: docs-update-end
:language: java
```
## Configuring a Deployment
2022-08-12 17:09:40 -07:00
Ray Serve lets you configure your deployments to:
2022-08-13 00:07:12 +08:00
2022-08-12 17:09:40 -07:00
- scale out by increasing the number of [deployment replicas ](serve-architecture-high-level-view )
- assign [replica resources ](serve-cpus-gpus ) such as CPUs and GPUs.
2022-08-13 00:07:12 +08:00
The next two sections describe how to configure your deployments.
### Scaling Out
By specifying the `numReplicas` parameter, you can change the number of deployment replicas:
```{literalinclude} ../../../java/serve/src/test/java/io/ray/serve/docdemo/ManageDeployment.java
2022-08-12 17:09:40 -07:00
:start-after: docs-scale-start
2022-08-13 00:07:12 +08:00
:end-before: docs-scale-end
:language: java
```
### Resource Management (CPUs, GPUs)
2022-08-12 17:09:40 -07:00
Through the `rayActorOptions` parameter, you can reserve resources for each deployment replica, such as one GPU:
2022-08-13 00:07:12 +08:00
```{literalinclude} ../../../java/serve/src/test/java/io/ray/serve/docdemo/ManageDeployment.java
2022-08-12 17:09:40 -07:00
:start-after: docs-resource-start
2022-08-13 00:07:12 +08:00
:end-before: docs-resource-end
:language: java
```
## Managing a Python Deployment
2022-08-12 17:09:40 -07:00
A Python deployment can also be managed and called by the Java API. Suppose you have a Python file `counter.py` in the `/path/to/code/` directory:
2022-08-13 00:07:12 +08:00
```python
from ray import serve
@serve .deployment
class Counter(object):
def __init__ (self, value):
self.value = int(value)
def increase(self, delta):
self.value += int(delta)
return str(self.value)
```
2022-08-12 17:09:40 -07:00
You can deploy it through the Java API and call it through a `RayServeHandle` :
2022-08-13 00:07:12 +08:00
```java
import io.ray.api.Ray;
import io.ray.serve.api.Serve;
import io.ray.serve.deployment.Deployment;
import io.ray.serve.generated.DeploymentLanguage;
import java.io.File;
public class ManagePythonDeployment {
public static void main(String[] args) {
System.setProperty(
"ray.job.code-search-path",
System.getProperty("java.class.path") + File.pathSeparator + "/path/to/code/");
Serve.start(true, false, null);
Deployment deployment =
Serve.deployment()
.setDeploymentLanguage(DeploymentLanguage.PYTHON)
.setName("counter")
.setDeploymentDef("counter.Counter")
.setNumReplicas(1)
.setInitArgs(new Object[] {"1"})
.create();
deployment.deploy(true);
System.out.println(Ray.get(deployment.getHandle().method("increase").remote("2")));
}
}
```
2022-08-12 17:09:40 -07:00
:::{note}
Before `Ray.init` or `Serve.start` , you need to specify a directory to find the Python code. For details, please refer to [Cross-Language Programming ](cross_language ).
:::
2022-08-13 00:07:12 +08:00
## Future Roadmap
2022-08-12 17:09:40 -07:00
In the future, Ray Serve plans to provide more Java features, such as:
- an improved Java API that matches the Python version
2022-08-13 00:07:12 +08:00
- HTTP ingress support
2022-08-12 17:09:40 -07:00
- bring-your-own Java Spring project as a deployment