In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the relevant knowledge of "dubbo and demo case analysis". The editor shows you the operation process through the actual case. The operation method is simple, fast and practical. I hope this "dubbo and demo case analysis" article can help you solve the problem.
1. What is Dubbo?
Dubbo is a distributed services framework dedicated to providing high-performance and transparent RPC remote service invocation solutions, as well as SOA service governance solutions. To put it simply, dubbo is a service framework. If there is no distributed requirement, it does not need to be used. Only when it is distributed, there is a demand for a distributed service framework such as dubbo, and it is essentially a service invocation. To put it bluntly, it is a distributed framework for remote service invocation (bid farewell to WSdl in the Web Service pattern and register on dubbo as a server and consumer).
Its core part includes:
1. Remote communication: provides abstract encapsulation of a variety of long-connection-based NIO frameworks, including multiple thread models, serialization, and information exchange in the "request-response" pattern.
two。 Cluster fault tolerance: provides transparent remote procedure calls based on interface methods, including multi-protocol support, as well as soft load balancing, failure fault tolerance, address routing, dynamic configuration and other cluster support.
3. Automatic discovery: based on the registry directory service, the service consumer can dynamically find the service provider, make the address transparent, and enable the service provider to smoothly increase or decrease the number of machines.
2. What can Dubbo do?
1. Transparent remote method invocation, calling remote methods as if they were local methods, with simple configuration without any API intrusion.
two。 Soft load balancing and fault-tolerant mechanism can replace F5 and other hardware load balancers in the intranet to reduce costs and reduce a single point.
3. Automatic service registration and discovery eliminates the need to write down the address of the service provider. The registry queries the IP address of the service provider based on the interface name, and can smoothly add or delete service providers.
Dubbo adopts full spring configuration mode and accesses the application transparently without any API intrusion to the application. You only need to load the configuration of Dubbo with Spring, and Dubbo is loaded based on Schema extension of Spring.
Before using Web Service, I thought the test interface could do functional testing or performance testing through soapui or LR by simulating messages. But now using Dubbo, interfaces can not interact directly, I try to pass the simulated consumer address test, the result is ugly, and then use jmeter to test through junit, but still need to register with dubbo, if you do not provide the source code, this test case is not easy to write. .
3. Architecture of dubbo
The dubbo architecture diagram is as follows:
Node role description:
Provider: the provider of the exposed service.
Consumer: the service consumer that invokes the remote service.
Registry: the registry for service registration and discovery.
Monitor: the monitoring center that calculates the call time and call time of the service.
Container: the service running container.
I think this is very good, the role is clear, and you can determine whether the service is normal according to the status of each node role.
Description of invocation relationship:
1. The service container is responsible for starting, loading, and running the service provider.
1. Upon startup, the service provider registers the services it provides with the registry.
two。 Service consumers subscribe to the services they need from the registry when they start up.
3. The registry returns a list of service provider addresses to the consumer, and if there is a change, the registry will push the change data to the consumer based on the persistent connection.
4. The service consumer, from the provider address list, chooses one provider to call based on the soft load balancing algorithm, and then chooses another one if the call fails.
5. Service consumers and providers accumulate the number of calls and call time in memory and regularly send statistics to the monitoring center every minute.
The fault tolerance of dubbo is obvious, and the performance has not yet been tested. We want to suggest a cache for a page that needs to be dropped 5 times, but the business relationship can not be adopted. We also need to study the performance tuning of dubbo.
4. How to use dubbo.
Dubbo adopts full Spring configuration mode and accesses the application transparently without any API intrusion to the application. You only need to load the configuration of Dubbo with Spring, and Dubbo is loaded based on Schema extension of Spring. If you do not want to use Spring configuration, but want to call through API (not recommended)
Let's take a look at how to configure spring:
Service provider:
1. Download the zookeeper registry at:
After downloading http://www.apache.org/dyn/closer.cgi/zookeeper/, decompress it. Enter D:apach-zookeeper-3.4.5in and double-click zkServer.cmd to start the registry service.
two。 Define the service interface: (this interface needs to be packaged separately and shared between service providers and consumers)
The following example is good, it is written in great detail and can be used as a model.
Package com.unj.dubbotest.provider; import java.util.List; public interface DemoService {String sayHello (String name); public List getUsers ();}
Implement the interface at the service provider: (hide the implementation from the service consumer)
Package com.unj.dubbotest.provider; import java.util.ArrayList; import java.util.LinkedList; import java.util.List; public class DemoServiceImpl implements DemoService {public String sayHello (String name) {return "Hello" + name;} public List getUsers () {List list = new ArrayList (); User U1 = new User (); u1.setName ("jack") U1.setAge (20); u1.setSex ("male"); User U2 = new User (); u2.setName ("tom"); u2.setAge (21); u2.setSex ("female"); User U3 = new User (); u3.setName ("rose"); u3.setAge (19); u3.setSex ("female") List.add (U1); list.add (U2); list.add (U3); return list;}
Expose the service with the Spring configuration declaration:
Load the Spring configuration and start the service:
Package com.unj.dubbotest.provider; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Provider {public static void main (String [] args) throws Exception {ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext (new String [] {"applicationContext.xml"}); context.start (); System.in.read (); / / to ensure that the service remains open, use the blocking of the input stream to simulate}} service consumers:
ApplicationContext-dubbo.xml registers the interfaces I need to call. When I first started testing, I needed a lot of interfaces, so I wrote this file full. Later, I became familiar with separating interfaces by business type, and writing more than N applicationContext-dubbo-*.xml is much more concise.
two。 Load the Spring configuration and invoke the remote service:
Package com.alibaba.dubbo.demo.pp; import java.util.List; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.unj.dubbotest.provider.DemoService; public class Consumer {public static void main (String [] args) throws Exception {ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext (new String [] {"applicationContext.xml"}); context.start (); DemoService demoService = (DemoService) context.getBean ("demoService") / String hello = demoService.sayHello ("tom"); / /? System.out.println (hello); / List list = demoService.getUsers (); if (list! = null & & list.size () > 0) {for (int I = 0; I)
< list.size(); i++) { System.out.println(list.get(i)); } } // System.out.println(demoService.hehe()); System.in.read(); } } 调用结果为:Dubbo Administration Page:
This management page also needs to deploy an environment, at first I thought it was brought by dubbo, but I couldn't find it after looking for it for a long time. .
Application page:
Provider page:
Consumer page:
Service page:
This is the end of the introduction of "dubbo and demo case Analysis". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.