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 implement demo based on dubbo protocol

2025-01-19 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 demo based on dubbo protocol". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to achieve demo based on dubbo protocol".

Description: this article is based on redhat6 to do demo,jdk and other environments are ready. It is recommended that java developers understand the linux system.

The demo project is shown in the figure:

The running result is shown in the figure:

2. Create a new API service: com.enson.demo.service.DubboService

Code:

Package com.enson.demo.service;public interface DubboService {public String printWord (String word);}

3. Implementation API: com.enson.demo.service.impl.DubboServiceImpl

Code:

Package com.enson.demo.service.impl;import java.text.SimpleDateFormat;import java.util.Date;import com.enson.demo.service.DubboService;public class DubboServiceImpl implements DubboService {@ Override public String printWord (String word) {String outWord = new SimpleDateFormat ("[yyyy-MM-dd HH:mm:ss]") .format (new Date ()) + word; System.err.println (outWord) Return outWord;}}

4. Create provider.xml

5. Create a test.Provider class as the entry for service registration:

Package test;import java.io.IOException;import org.springframework.context.support.ClassPathXmlApplicationContext;public class Provider {public static void main (String [] args) throws IOException {ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext (new String [] {"provider.xml"}); context.start (); System.in.read (); / / press any key to exit}}

The above completes the writing of the service provider.

6. Create com.enson.custom.action.DubboAction

Package com.enson.custom.action;import com.enson.demo.service.DubboService;public class DubboAction {public DubboService dubboService; / / Spring method injects public void setDubboService (DubboService dubboService) {this.dubboService = dubboService;} public String printWord () {System.out.println ("action printWord is executed!") ; return this.dubboService.printWord ("Hello World"); / / execute remote method}}

7. Create cstom.xml

8. Create a consumer class: test.Custom

Package test;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.enson.custom.action.DubboAction;public class Custom {public static void main (String [] args) {ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext (new String [] {"consumer.xml"}); context.start (); DubboAction dubboAction = (DubboAction) context.getBean ("dubboAction") / / get the remote service agent String word = dubboAction.printWord (); / / execute the remote method System.out.println (word); / / display the call result}}

Test:

1. Start zookeeper.

2. Execute Provider. Run as java application

3. Execute Custom. Run as java application

Just check the console.

Thank you for your reading, the above is the content of "how to achieve demo based on dubbo protocol". After the study of this article, I believe you have a deeper understanding of how to achieve demo based on dubbo protocol, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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