1. Overview
1.概述
In this article, we’ll start with a Spring Cloud Gateway application and a Spring Boot application. Then, we’ll update it to use Dapr (Distributed Application Runtime) instead. Finally, we’ll update the Dapr configuration to show the flexibility that Dapr provides when integrating with cloud-native components.
在这篇文章中,我们将从一个Spring Cloud Gateway应用程序和一个Spring Boot应用程序开始。然后,我们将更新它以使用Dapr(分布式应用程序运行时间)来代替。最后,我们将更新Dapr配置,以展示Dapr在与云原生组件集成时提供的灵活性。
2. Intro to Dapr
2.Dapr介绍
With Dapr, we can manage the deployment of a cloud-native application without any impact on the application itself. Dapr uses the sidecar pattern to off-load deployment concerns from the application, which allows us to deploy it into other environments (such as on-premise, different proprietary Cloud platforms, Kubernetes, and others) without any changes to the application itself. For more details, check out this overview on the Dapr website.
通过Dapr,我们可以管理云原生应用的部署,而不会对应用本身造成任何影响。Dapr使用sidecar模式来卸载应用程序的部署问题,这使我们能够将其部署到其他环境中(如内部部署、不同的专有云平台、Kubernetes和其他环境)而无需对应用程序本身进行任何更改。欲了解更多细节,请查看Dapr网站上的概述。
3. Create Sample Applications
3.创建示例应用程序
We’ll start by creating a sample Spring Cloud Gateway and Spring Boot application. In the great tradition of “Hello world” examples, the gateway will proxy requests to a back-end Spring Boot application for the standard “Hello world” greeting.
我们将首先创建一个Spring Cloud Gateway和Spring Boot应用程序的样本。按照 “Hello world “例子的伟大传统,该网关将把请求代理给后端Spring Boot应用程序,以获得标准的 “Hello world “问候语。
3.1. Greeting Service
3.1.问候服务
First, let’s create a Spring Boot application for the greeting service. This is a standard Spring Boot application with spring-boot-starter-web as the only dependency, the standard main class, and the server port configured as 3001.
首先,让我们为问候服务创建一个Spring Boot应用程序。这是一个标准的Spring Boot应用程序,spring-boot-starter-web是唯一的依赖,标准的主类,服务器端口配置为3001。
Let’s add a controller to respond to the hello endpoint:
让我们添加一个控制器来响应hello端点。
@RestController
public class GreetingController {
@GetMapping(value = "/hello")
public String getHello() {
return "Hello world!";
}
}
After building our greeting service app, we’ll start it up:
在建立我们的问候服务应用程序之后,我们将启动它:
java -jar greeting/target/greeting-1.0-SNAPSHOT.jar
We can test it out using curl to return the “Hello world!” message:
我们可以使用curl来测试它,以返回 “Hello world!”信息:。
curl http://localhost:3001/hello
3.2. Spring Cloud Gateway
3.2.Spring Cloud Gateway
Now, we’ll create a Spring Cloud Gateway on port 3000 as a standard Spring Boot application with spring-cloud-starter-gateway as the only dependency and the standard main class. We’ll also configure the routing to access the greeting service:
现在,我们将在3000端口创建一个Spring Cloud Gateway,作为一个标准的Spring Boot应用程序,将spring-cloud-starter-gateway作为唯一的依赖关系和标准主类。我们还将配置路由,以便访问问候服务:
spring:
cloud:
gateway:
routes:
- id: greeting-service
uri: http://localhost:3001/
predicates:
- Path=/**
filters:
- RewritePath=/?(?<segment>.*), /$\{segment}
Once we build the gateway app, we can start the gateway:
一旦我们建立了网关应用程序,我们就可以启动网关:。
java -Dspring.profiles.active=no-dapr -jar gateway/target/gateway-1.0-SNAPSHOT.jar
We can test it out using curl to return the “Hello world!” message from the greeting service:
我们可以使用curl来测试它,从问候服务中返回 “Hello world!”消息:
curl http://localhost:3000/hello
4. Add Dapr
4.添加Dapr
Now that we have a basic example in place, let’s add Dapr to the mix.
现在我们已经有了一个基本的例子,让我们把Dapr加入其中。
We do this by configuring the gateway to communicate with the Dapr sidecar instead of directly with the greeting service. Dapr will then be responsible for finding the greeting service and forwarding the request to it; the communication path will now be from the gateway, through the Dapr sidecars, and to the greeting service.
我们通过配置网关来与Dapr sidecar而不是直接与问候服务进行通信。然后,Dapr将负责寻找问候服务,并将请求转发给它;现在的通信路径将是从网关出发,通过Dapr sidecars,然后到达问候服务。
4.1. Deploy Dapr Sidecars
4.1.部署Dapr Sidecars
First, we need to deploy two instances of the Dapr sidecar – one for the gateway and one for the greeting service. We do this using the Dapr CLI.
首先,我们需要部署Dapr sidecar的两个实例–一个用于网关,一个用于问候服务。我们使用Dapr CLI完成这项工作。。
We’ll use a standard Dapr configuration file:
我们将使用一个标准的Dapr配置文件。
apiVersion: dapr.io/v1alpha1
kind: Configuration
metadata:
name: daprConfig
spec: {}
Let’s start the Dapr sidecar for the gateway on port 4000 using the dapr command:
让我们使用dapr命令为4000端口的网关启动Dapr sidecar:
dapr run --app-id gateway --dapr-http-port 4000 --app-port 3000 --config dapr-config/basic-config.yaml
Next, let’s start the Dapr sidecar for the greeting service on port 4001 using the dapr command:
接下来,让我们使用dapr命令为4001端口的问候服务启动Dapr sidecar。
dapr run --app-id greeting --dapr-http-port 4001 --app-port 3001 --config dapr-config/basic-config.yaml
Now that the sidecars are running, we can see how they take care of intercepting and forwarding requests to the greeting service. When we test it out using curl, it should return the “Hello world!” greeting:
现在侧线正在运行,我们可以看到它们如何负责拦截和转发问候服务的请求。当我们用curl测试它时,它应该返回 “Hello world!”的问候语。
curl http://localhost:4001/v1.0/invoke/greeting/method/hello
Let’s try the same test using the gateway sidecar to confirm that it also returns the “Hello world!” greeting:
让我们用gateway sidecar进行同样的测试,以确认它也返回 “Hello world!”的问候语。
curl http://localhost:4000/v1.0/invoke/greeting/method/hello
What’s going on here behind the scenes? The Dapr sidecar for the gateway uses service discovery (in this case, mDNS for a local environment) to find the Dapr sidecar for the greeting service. Then, it uses Service Invocation to call the specified endpoint on the greeting service.
这背后发生了什么?网关的Dapr sidecar使用服务发现(在这种情况下,本地环境的mDNS)来寻找问候服务的Dapr sidecar。然后,它使用服务调用来调用问候服务的指定端点。
4.2. Update Gateway Configuration
4.2.更新网关配置
The next step is to configure the gateway routing to use its Dapr sidecar instead:
下一步是配置网关路由,以使用其Dapr sidecar来代替:
spring:
cloud:
gateway:
routes:
- id: greeting-service
uri: http://localhost:4000/
predicates:
- Path=/**
filters:
- RewritePath=//?(?<segment>.*), /v1.0/invoke/greeting/method/$\{segment}
Then, we’ll restart the gateway with the updated routing:
然后,我们将用更新的路由重新启动网关:
java -Dspring.profiles.active=with-dapr -jar gateway/target/gateway-1.0-SNAPSHOT.jar
We can test it out using the curl command to once again get the “Hello world” greeting from the greeting service:
我们可以使用curl命令进行测试,再次从问候语服务中获得 “Hello world “问候语:。
curl http://localhost:3000/hello
When we look at what’s happening on the network using Wireshark, we can see that the traffic between the gateway and the service goes through the Dapr sidecars.
当我们使用Wireshark查看网络上发生的情况时,我们可以看到网关和服务之间的流量是通过Dapr侧线。
Congratulations! We have now successfully brought Dapr into the picture. Let’s review what this has gained us: The gateway no longer needs to be configured to find the greeting service (that is, the port number for the greeting service no longer needs to be specified in the routing configuration), and the gateway no longer needs to know the details of how the request is forwarded to the greeting service.
祝贺你!我们现在已经成功地将Dapr带入画面。让我们回顾一下这为我们带来了什么。网关不再需要配置来寻找问候服务(也就是说,问候服务的端口号不再需要在路由配置中指定),而且网关不再需要知道请求如何被转发到问候服务的细节。
5. Update Dapr Configuration
5.更新Dapr配置
Now that we have Dapr in place, we can configure Dapr to use other cloud-native components instead.
现在我们已经有了Dapr,我们可以配置Dapr来代替使用其他云原生组件。
5.1. Use Consul for Service Discovery
5.1.使用Consul进行服务发现
Let’s use Consul for Service Discovery instead of mDNS.
让我们使用Consul进行服务发现,而不是mDNS。
First, we need to install and start Consul on the default port of 8500, and then update the Dapr sidecar configuration to use Consul:
首先,我们需要在默认端口8500上安装并启动Consul,然后u更新Dapr sidecar配置以使用Consul:
apiVersion: dapr.io/v1alpha1
kind: Configuration
metadata:
name: daprConfig
spec:
nameResolution:
component: "consul"
configuration:
selfRegister: true
Then we’ll restart both Dapr sidecars with the new configuration:
然后我们将用新的配置重新启动两个Dapr侧车:。
dapr run --app-id greeting --dapr-http-port 4001 --app-port 3001 --config dapr-config/consul-config.yaml
dapr run --app-id gateway --dapr-http-port 4000 --app-port 3000 --config dapr-config/consul-config.yaml
Once the sidecars are restarted, we can access the Services page in the consul UI and see the gateway and greeting apps listed. Notice that we did not need to restart the application itself.
一旦重启侧车,我们可以访问consul UI中的服务页面,看到列出的网关和问候应用程序。注意,我们不需要重启应用程序本身。
See how easy that was? A simple configuration change for the Dapr sidecars now gives us support for Consul and, most importantly, with no impact on the underlying application. This differs from using Spring Cloud Consul, which requires updating the application itself.
看看这有多容易?Dapr 侧设备的简单配置更改现在使我们支持 Consul,最重要的是,对底层应用程序没有任何影响。这与使用Spring Cloud Consul不同,后者需要更新应用程序本身。
5.2. Use Zipkin for Tracing
5.2.使用Zipkin进行追踪
Dapr also supports integration with Zipkin for tracing calls across applications.
Dapr还支持与Zipkin的集成,以追踪跨应用程序的调用。
First, install and start up Zipkin on the default port of 9411, and then update the configuration for the Dapr sidecar to add Zipkin:
首先,在默认的9411端口上安装并启动Zipkin,然后更新Dapr sidecar的配置,添加Zipkin。
apiVersion: dapr.io/v1alpha1
kind: Configuration
metadata:
name: daprConfig
spec:
nameResolution:
component: "consul"
configuration:
selfRegister: true
tracing:
samplingRate: "1"
zipkin:
endpointAddress: "http://localhost:9411/api/v2/spans"
We’ll need to restart both Dapr sidecars to pick up the new configuration:
我们将需要重新启动两个Dapr侧车以接收新的配置:
dapr run --app-id greeting --dapr-http-port 4001 --app-port 3001 --config dapr-config/consul-zipkin-config.yaml
dapr run --app-id gateway --dapr-http-port 4000 --app-port 3000 --config dapr-config/consul-zipkin-config.yaml
Once Dapr is restarted, you can issue a curl command and check out the Zipkin UI to see the call trace.
一旦Dapr被重新启动,你可以发出curl命令并查看Zipkin用户界面以查看调用跟踪。
Once again, there’s no need to restart the gateway and greeting service. It requires only an easy update to the Dapr configuration. Compare this to using Spring Cloud Zipkin instead.
再次,不需要重新启动网关和问候服务。它只需要对Dapr配置的简单更新。将此与使用Spring Cloud Zipkin进行比较。
5.3. Other Components
5.3.其他组件
There are many components that Dapr supports to address other concerns such as security, monitoring, and reporting. Check out the Dapr documentation for a full list.
有许多组件,Dapr支持解决其他问题,如安全、监控和报告。请查看Dapr文档,了解完整的列表。。
6. Conclusion
6.结论
We have added Dapr to a simple example of a Spring Cloud Gateway communicating with a back-end Spring Boot service. We’ve shown how to configure and start the Dapr sidecar and how it then takes care of cloud-native concerns such as service discovery, communication, and tracing.
我们将Dapr添加到一个Spring Cloud Gateway与后端Spring Boot服务通信的简单例子中。我们展示了如何配置和启动Dapr sidecar,以及它如何处理服务发现、通信和跟踪等云原生问题。
Although this comes at the cost of deploying and managing a sidecar application, Dapr provides flexibility for deployment into different cloud-native environments and cloud-native concerns without requiring changes to the applications once the integration with Dapr is in place.
虽然这要以部署和管理挎包应用为代价,但Dapr为部署到不同的云原生环境和云原生关注点提供了灵活性,一旦与Dapr整合到位,不需要对应用进行修改。
This approach also means that developers don’t need to be encumbered with cloud-native concerns as they are writing the code, which frees them up to focus on business functionality. Once the application is configured to use the Dapr sidecar, different deployment concerns can be addressed without any impact on the application – no need for re-coding, re-building, or re-deploying applications. Dapr provides a clean separation between application and deployment concerns.
这种方法还意味着,开发人员在编写代码时不需要被云原生关注点所困扰,这使他们可以将精力放在业务功能上。一旦应用程序被配置为使用Dapr sidecar,不同的部署问题就可以得到解决,而不会对应用程序产生任何影响–不需要重新编码、重新构建或重新部署应用程序。Dapr在应用程序和部署问题之间提供了一个干净的分离。
As always, the complete code for this article can be found over on GitHub.
像往常一样,本文的完整代码可以在GitHub上找到over。