Dubbo and Spring MVC

Source: Internet
Author: User
Tags log4j

installationOne, local Service 1, define the Service interface: (the interface needs to be packaged separately, shared between the service provider and the consumer)
Public interface CustomerService {public String getName ();}
2, in the service Provider Implementation interface: (to the service consumer hidden implementation)
public class Customerserviceimpl implements customerservice{@Overridepublic String getName () {System.out.print ("I print") ; return "print Result";}}
  3, then introduce Dubbo package      dubbo-2.5.3.jar     log4j.jar      netty-3.5.7.final.jar     slf4j.jar     slf4j-log4j.jar      zkclient.jar     zookeeper.jar   4, using spring configuration claims Exposure service:         Create a new applicationprovider.xml with the following configuration:   
<?xml version= "1.0" encoding= "UTF-8"?> <beans xmlns= "Http://www.springframework.org/schema/beans" Xmlns:xs I= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo= "Http://code.alibabatech.com/schema/dubbo" xsi: schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans.xsd Http://code.alibabatech.com/schema/dubbo Http://code.alibabatech.com/schema/dubbo/dubbo . xsd > <!--specific implementation bean--<bean id= "Demoservice" class= "Com.jinbin.service.customer.CustomerServic Eimpl "/> <!--provider app information for computing dependencies--<dubbo:application name=" Xixi_provider "/> <!--use Multicast Broadcast Registration center Exposure service address <dubbo:registry address= "multicast://localhost:1234"/>--> <!--use zookeeper note  Book Center exposure service address--<dubbo:registry address= "zookeeper://192.168.1.3:2181"/> <!--exposed service with Dubbo protocol on 20880 Ports --<dubbo:protocol name= "Dubbo" port= "20880"/> <!--declaration needs to expose the service interface--<dubbo:service interface= "com.jinbin.service.customer.CustomerSe   Rvice "ref=" Demoservice "/> </beans>


Here I expose the address of the server to be managed by zookeeper, the user first to install the Zookeeper app to use this feature, the relevant installation steps see the relevant post 5, load the spring configuration, and invoke the remote service: (You can also use IOC injection)
public class Dubooprovider {public static void main (string[] args) {    Classpathxmlapplicationcontext context = new Clas Spathxmlapplicationcontext (                  new string[]{"Applicationprovider.xml"});          Context.start ();          System.out.println ("Press any key to exit.");          try {System.in.read ();} catch (IOException e) {//TODO auto-generated catch Blocke.printstacktrace ();}}}  

and start it into the boot state. The above is the full steps of the server provider, the function interface has been written, the following we began how to remotely invokesecond, service consumers 1, create a new configuration file Applicationconsumer.xml, the content is as follows: 
<?xml version= "1.0" encoding= "UTF-8"?> < Beans xmlns= "Http://www.springframework.org/schema/beans" xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo= "Http://code.alibabatech.com/schema/dubbo" xsi:schemalocation= "http://www.springframework.org/ Schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd HTTP://CODE.ALIBABATECH.COM/SC Hema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd "> <!--Consumer app name for calculating dependencies, not matching criteria, not with Provider--<dubbo:application name= "Consumer-of-helloworld-app"/> <!--exposing discovery services using the multicast broadcast registry ---<dubbo:registry protocol= "zookeeper" address= "zookeeper://192.168.1.3:2181"/> <!--generation Remote service proxy, which can be used like a local bean demoservice---<dubbo:reference id= "Demoservice" interface= " Com.jinbin.service.customer.CustomerService "/> </beans>  


In order to use it in the Web, we configure it in the spring start read process in XML
   <context-param><param-name>contextconfiglocation</param-name><param-value>/web-inf/ Application.xml/web-inf/applicationconsumer.xml</param-value>    </context-param>

2. Interface Invocation The invocation process is simple, the interface file is first made into a jar package, and then referenced in this project the SPRINGMVC calling program is as follows:
@Autowired CustomerService Demoservice;
  @RequestMapping (value= "duboo1") public  void Duboo1 () {   demoservice.getname ();}

You can perform a successful three, dubbo-admin use download Dubbo-admin-2.5.3.war put it under Tomcat, configure Dubbo.properties,
vi webapps/ROOT/WEB-INF/dubbo.properties
dubbo.properties
dubbo.registry.address=zookeeper://127.0.0.1:2181dubbo.admin.root.password=rootdubbo.admin.guest.password=guest
Modify the URL and port of the zookeeper

Start:

./bin/startup.sh

Dubbo and Spring MVC

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.