In the first two chapters, Dubbo Development under Docker, Trilogy ONE: The Extreme Experience and Dubbo Development under Docker, Trilogy Two: In local environment Building, we experienced dubbo environment building and service release and consumption, and had a preliminary understanding of Dubbo. In this chapter, we will write the actual dubbo service release and consumption code, and after the actual practice, we can release our services to the Dubbo environment for others to call. You can also have your projects call existing services in the Dubbo environment.

Download the source code

Github ([email protected]:zq2599/blog_demos. Git). There are two projects in this chapter: 1. The code for the service provider is in the directory dubbo_service_provider, as shown in the red box below:

  1. The code for the service consumer is in the directory dubbo_service_consumer, as shown in the red box below:

Next, we analyze one by one and develop it in practice.

Service provider development

In addition to the usual spring-related dependencies, we also need to add dubbo and ZooKeeper dependencies, as shown below:

<! -- dubbo --> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <exclusions> <exclusion> <artifactId>spring</artifactId> <groupId>org.springframework</groupId> </exclusion> </exclusions> </dependency> <! -- zkclient --> <dependency> <groupId>com.github.sgroschupf</groupId> <artifactId>zkclient</artifactId> The < version > 0.1 < / version > < / dependency > <! -- zookeeper --> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> The < version > 3.3.6 < / version > < / dependency >Copy the code

1. CalculateService defines add method to execute the most basic int addition service, as follows:

public class CalculateServiceImpl implements CalculateService{ @Override public int add(int a, int b) { return a + b; }}Copy the code
  1. PlatformService defines the getRpcFrom method, which returns the value of TOMCAT_SERVER_ID in the current environment system. This value is passed in from docker-comemage.yml when the docker container is started.
public class PlatformServiceImpl implements PlatformService { @Override public String getRpcFrom() { return System.getenv().get("TOMCAT_SERVER_ID"); }}Copy the code

These are two demo service implementations. The logic is very simple. Now let’s see how to publish the service to a Dubbo environment:

Spring-extends. XML is our custom Spring configuration file where services we want to publish to the Dubbo environment are declared:

<dubbo:application name="dubbo_service_provider" /> <! -- Native pseudo cluster test --> <! - "dubbo: registry protocol =" zookeeper "address =" 192.9.145.19:2181192.9. 145.19:2182192.9. 145.19:2183 "/ > -- > <dubbo:registry address="zookeeper://zkhost:2181" /> <dubbo:protocol name="dubbo" port="20880" /> <dubbo:service interface="com.bolingcavalry.service.CalculateService" ref="calculateService" /> <dubbo:service interface="com.bolingcavalry.service.PlatformService" ref="platformService" /> <! -- as well as the local bean implementation service - > < bean id = "calculateService" class = "com. Bolingcavalry. Service. Impl. CalculateServiceImpl" / > < bean id="platformService" class="com.bolingcavalry.service.impl.PlatformServiceImpl" />Copy the code

Dubbo: Application defines the application to which the service belongs as dubbo_service_provider. Dubbo :registry defines the address of the registry. In this case, zooKeeper is configured as a single server, so only one address “Zookeeper ://zkhost:2181” is entered. Where zkhost is the alias of the link attribute of docker container (connected to zooKeeper container), there is an annotated Dubbo: Registry configuration on the top of dubbo: Registry configuration, which is the connection mode of ZooKeeper cluster. Dubbo :protocol Indicates that the dubbo protocol is used and port 20880 is used. Dubbo :service configudes the service to be published, specifying the service interface and the corresponding bean;

With the above configuration, the registry will be aware after the Spring environment is started that MVN clean package-dmaven.test.skip =true in the directory where the POP.xml file is located will compile the package. Please refer to “Dubbo Development under Docker, Trilogy II: Local Environment Construction” for the operation mode of docker container release.

Service consumer development

The service consumer project is dubbo_service_consumer. The dependencies in the POM are the same as those of the service provider, and since the services provided by the Dubbo_Service_provider are invoked, Therefore, CalculateService and PlatformService interfaces should be introduced into the project, generally through JAR package. Here, for convenience, the source code of the two interfaces should be directly copied into the project, as shown below:

Take a look at the code that invokes the service, as shown in the figure below, which can be used directly from plain Autowired, just like using the service in a plain Spring environment:

The ability to obtain remote services from the Dubbo environment is implemented with the following Spring configuration:

<! <dubbo:application name="dubbo_service_consumer" /> <! Use multicast broadcast registries to expose discovery service addresses --> <! - "dubbo: registry protocol =" zookeeper "address =" 192.9.145.19:2181192.9. 145.19:2182192.9. 145.19:2183 "/ > -- > "Dubbo: registry address =" zookeeper: / / 172.28.0.3:2181 "/ > <! -- Generate a remote service proxy, You can use demoService as you would a local bean --> <dubbo: Reference ID ="calculateService" interface="com.bolingcavalry.service.CalculateService" /> <dubbo:reference id="platformService" interface="com.bolingcavalry.service.PlatformService" />Copy the code

Dubbo: Application and Dubbo: Registry serve the same functions as the service providers analyzed above. Dubbo: Reference refers to the remote service obtained from the Dubbo environment. Interface specifies the service type. When the server is used, the service provider’s service is called remotely based on the registry information.

The above is dubbo service providers and consumers of the actual source code, the actual production environment, will also involve more detailed and more complex configuration and use, please pay attention to the dubbo official website development manual.