Three Docker under dubbo Development Trilogy: java development

The previous two chapters , "Docker dubbo under development, one of the Rings: The speed experience" and "dubbo development at Docker, the trilogy of the two: the local environment to build," we experience the publishing and consumer dubbo environment to build and service of dubbo have a preliminary understanding, this chapter is written after the combat we publish our services to combat dubbo code can publish and consume their services to dubbo environment for people to call, but also make their own project to call dubbo environment of existing services;

Source download

The combat a total of two projects, namely service providers and consumers, are web project, code on github, address https://github.com/zq2599/blog_demos, there are a number of projects, chapter desired code is as follows:

  1. Service provider code directory dubbo_service_provider, as shown in FIG red box:

Write pictures described here

  1. Service consumer code directory dubbo_service_consumer, as shown in FIG red box:

Write pictures described here

Next, we analyzed one by one, combat development;

Service providers to develop

The first is the dependency pom in, addition to the usual spring relevant, we have to rely dubbo and join zookeeper, as follows:

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

Then look at the code, this combat provides two services, were realized about two interfaces:

  1. CalculateService add method is defined, performing the most basic services int type adder, to achieve the following:
public class CalculateServiceImpl implements CalculateService{

    @Override
    public int add(int a, int b) {
        return a + b;
    }
}
  1. PlatformService defined getRpcFrom method returns the current value of the environment variable TOMCAT_SERVER_ID exchange system, and this value is activated when the container docker passed from the docker-compose.yml;
public class PlatformServiceImpl implements PlatformService {

    @Override
    public String getRpcFrom() {
        return System.getenv().get("TOMCAT_SERVER_ID");
    }
}

These are two demo with service implementation, the logic is very simple, and now look at how to publish services to dubbo environment:

spring-extends.xml is our custom spring configuration file and want to publish to dubbo environmental services are here to declare:

<dubbo:application name="dubbo_service_provider" />
    <!-- 本机 伪集群 测试 -->
    <!--
    <dubbo:registry  protocol="zookeeper"  address="192.9.145.19:2181,192.9.145.19:2182,192.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" />
    <!-- 和本地bean一样实现服务 -->
    <bean id="calculateService" class="com.bolingcavalry.service.impl.CalculateServiceImpl" />
    <bean id="platformService" class="com.bolingcavalry.service.impl.PlatformServiceImpl" />

dubbo: application defines the application service belongs to dubbo_service_provider;
Dubbo: Registry defines the registry address, this combat zookeeper configured as stand-alone, so only fill out an address "zookeeper: // zkhost: 2181" , which docker is zkhost alias link attribute of the container (the container is connected to the zookeeper), in dubbo: the above configuration has a registry commented out dubbo: registry configuration, which is zookeeper cluster connection time;
dubbo: protocol using Dubbo defined protocol, using 20880 port;
Dubbo: service configured to publish services, specify the service interface and the corresponding bean;

Using the above configuration, after the spring environment startup registry will be perceived to be run in the directory where the pom.xml file mvn clean package -Dmaven.test.skip = true
to compile the package, publishing operation mode, please refer to the docker container " Docker under dubbo development, the trilogy of the two: the local environment to build " ;

Consumer Services Development

Consumer service project is dubbo_service_consumer, like the pom dependencies and service providers, due to dubbo_service_provider call the service provided, so should CalculateService and PlatformService two interfaces introduced to the project, usually introduced by way of the jar package here it is more convenient simply copy the source code two interfaces to the project, as shown below:

Write pictures described here

Let's look at the code to call the service, as shown by ordinary Autowired can be used directly, just like a normal spring environment using the service as simple as:

[Image dump in the chain ... (img-uShHNkJu-1568769121752)]

From dubbo ability to access remote services environment, is achieved by the following spring configuration:

<!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->
    <dubbo:application name="dubbo_service_consumer" />
    <!-- 使用multicast广播注册中心暴露发现服务地址 -->
    <!--
    <dubbo:registry  protocol="zookeeper"  address="192.9.145.19:2181,192.9.145.19:2182,192.9.145.19:2183" />
    -->
    <dubbo:registry address="zookeeper://172.28.0.3:2181" />
    <!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
    <dubbo:reference id="calculateService" interface="com.bolingcavalry.service.CalculateService" />
    <dubbo:reference id="platformService" interface="com.bolingcavalry.service.PlatformService" />

dubbo: application and dubbo: the service provider's analysis of the role and the same registry earlier, dubbo: reference means to gain remote service from dubbo environment, interface specifies the type of service, the application starts, it will use the server according to the time registration information center, remote call services of service providers;

These are the service provider and the consumer dubbo actual source code, the actual production environment, will involve more detailed and complex to configure and use, please readers attention to the development of the manual dubbo official website.

I welcome the attention of the public numbers: programmer Chen Xin

Guess you like

Origin www.cnblogs.com/bolingcavalry/p/11539600.html