Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize java Development under Docker

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains "how to achieve java development under Docker". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to develop java under Docker.

The code of the service provider is under the directory dubbo_service_provider, as shown in the red box in the following figure:

The code of the service consumer is under the directory dubbo_service_consumer, as shown in the red box below:

Next, let's analyze the actual combat development one by one.

# # Service provider development # # first of all, the dependencies in pom. In addition to the commonly used spring-related, we also need to add dubbo and zookeeper dependencies, as shown below:

Com.alibaba dubbo 2.5.3 spring org.springframework com.github.sgroschupf zkclient 0.1 org.apache.zookeeper zookeeper 3.3.6

Next, let's take a look at the code. This actual combat provides two services and implements the following two interfaces:

CalculateService defines the add method and executes the most basic int addition service, which is implemented as follows:

Public class CalculateServiceImpl implements CalculateService {@ Override public int add (int a, int b) {return a + b;}}

PlatformService defines the getRpcFrom method, which returns the value of the swap variable TOMCAT_SERVER_ID in the current environment system, which is passed in from docker-compose.yml when the docker container starts.

Public class PlatformServiceImpl implements PlatformService {@ Override public String getRpcFrom () {return System.getenv () .get ("TOMCAT_SERVER_ID");}

These are two service implementations for the demonstration. The logic is simple. Now let's see how to publish the service to the dubbo environment:

Spring-extends.xml is our custom spring configuration file. All services that you want to publish to the dubbo environment are declared here:

Dubbo:application defines the application of service ownership and defines the registry address for dubbo_service_provider; dubbo:registry. The actual zookeeper is configured as a stand-alone machine, so only one address "zookeeper://zkhost:2181" is filled in, where zkhost is the alias of the link attribute of the docker container (connecting to the zookeeper container). There is an annotated dubbo:registry configuration above the dubbo:registry configuration, which is the connection mode of the zookeeper cluster. The dubbo:protocol definition uses the dubbo protocol and uses port 20880; dubbo:service configures the service to be published, specifies the service interface and the corresponding bean

Using the above configuration, after the spring environment starts, the registry will perceive that mvn clean package-Dmaven.test.skip=true can be compiled and packaged by executing mvn clean package-Dmaven.test.skip=true in the directory where the pom.xml file is located. For the release and operation mode of the docker container, please refer to "dubbo Development under Docker, Trilogy II: local Environment Building".

# # Service Consumer Development # # Service Consumer Project has the same dependency as the service provider in dubbo_service_consumer,pom. Since you need to call the services provided by dubbo_service_provider, you need to introduce CalculateService and PlatformService into the project, usually through the jar package. Here is a convenient figure to copy the source codes of the two interfaces directly to the project, as shown below:

Let's take a look at the code that invokes the service. As shown in the following figure, it can be used directly through a normal Autowired, which is as simple as using a service in a normal spring environment:

[the outer link picture is being transferred. (img-uShHNkJu-1568769121752)]

The ability to obtain remote services from the dubbo environment is achieved through the following spring configuration:

The role of dubbo:application and dubbo:registry is the same as that of the service provider analyzed earlier. Dubbo:reference refers to obtaining remote services from the dubbo environment. Interface specifies the service type. After the application is started, the service of the service provider will be remotely invoked according to the information of the registry when it is used by the server.

At this point, I believe you have a deeper understanding of "how to achieve java development under Docker". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report