# Developing Microservice-Based Applications Using the Silvera Domain-Specific Language
## 3 Silvera
### Silvera Concrete Syntax
#### 3.3.2 Service Registry Declaration
```
1 api-gatewayEntryGateway{
2
3 service_registry=ServiceRegistry
4
5 deployment{
6 version=\"0.0.1\"
7 port=9095
8 url=\"[[http://entry.example.com](http://entry.example.com)]([http://entry.example.com](http://entry.example.com))\"
9
10
11 communication_style=rpc
12
13 gateway-for{
14 Useras/api/u
15
16}
```
**Listing 5** Defining dependency between Order and User microservices. #### 3.3.4 Declaration of Microservice Dependency

Listing 5 shows how to define a dependency between Order and User microservices. In this particular example, the Order microservice requires userExists and userEmail methods from the User microservice. For each requirement, a failure recovery strategy is defined (a fallback_static strategy for the userExists method and a fail_silent strategy for the userEmail method). ```
1 dependencyOrder->User{
2 userExists[fallback_static]
3 userEmail[fail_silent]
4}
```
**Listing 6** Defining dependency between Order and User microservices. #### 3.3.5 Switching from RPC to Messaging Communication

So far, we have shown how to define microservices that use the RPC mechanism to communicate. In the text that follows, we will show how to change communication style from RPC to messaging. First, the message pool and a message broker need to be defined. Listing 6 shows how to define the message pool with one message group --UserMsGroup. This message group contains three message types: UserAdded, UserUpdated, and UserDeleted. Each message has two fields: userId and userEmail. ```
1 msg-pool{
2 groupUserMsgGroup[
3 msgUserAdded[
4 struserId
5 struserEmail
6]
7...
8]
9}
```
Listing 7 shows how to define a message broker named Broker. This message broker has three typed message channels: EV_USER_ADDED_CHANNEL channel for the UserAdded message, EV_USER_UPDATED_CHANNEL channel for the UserUpdated message, and EV_USER_DELETED_CHANNEL channel for the UserDeleted message. When instantiating a channel, the FQN of a message must be used. ```
1 msg-brokerBroker{
2
3 channelEV_USER_ADDED_CHANNEL(UserMsgGroup.UserAdded)
4 channelEV_USER_UPDATED_CHANNEL(UserMsgGroup.UserUpdated)
5 channelEV_USER_DELETED_CHANNEL(UserMsgGroup.UserDeleted)
6}
```
Listing 8 shows how the User microservices should be changed to use the messaging communication style. In the example, the User microservice publishes a message every time a user is added, updated, or deleted. Not only CRUD methods can produce or consume messages; regular API functions can use @producer and @consumer annotations that use the same syntax as shown in the example. ```
1 serviceUser{
2...
3 communication_style=messaging
4...
5
6 api{
7@create(UserMsgGroup.UserAdded->Broker.EV_USER_ADDED_CHANNEL)
8@read
9@update(UserMsgGroup.UserUpdated->
10 Broker.EV_USER_UPDATED_CHANNEL)
11@delete(UserMsgGroup.UserDeleted->
12 Broker.EV_DELETED_DELETED_CHANNEL)
13 typedefUser[
14...
15]
16...
17}
18}
```
Listing 9: User microservices that uses message communication style. ### Compiler

Silvera compiler consists of two logically separated parts: _front-end_ and _back-end_. The _front-end_ further consists of modules for analysis and evaluation, whereas the _back-end_ is comprised of a set of language-specific code generators. #### 3.4.1 Front-End

The compiler's _front-end_ performs lexical analysis, parsing, semantic analysis, and translation to an intermediate representation. The parser is produced by _textX_[52] based on the Silvera grammar. The textX is an open-source tool for fast DSL development in Python that is IDE agnostic and provides a fast round-trip from grammar change to testing [52]. Since DSLs are susceptible to changes [48], we chose _textX_ because it provides easy language evolution. The parser created by _textX_ parses Silvera programs and creates a graph of Python objects (model) where each object is an instance of a corresponding class from the metamodel. This way, instead of an _Abstract Syntax Tree_ (AST), textX returns a _Model_ object (Section 3.2). The front-end detects both syntax and semantic errors.