Detailed explanation of the use of dubbo annotations

1. Background
With the development of the Internet, the scale of website applications continues to expand, and the conventional vertical application architecture can no longer cope with it. Distributed service architecture and mobile computing architecture are imperative.
When more and more interfaces and implementation classes are added, the xml configuration of duboo will increase. In order to prevent hundreds of thousands of lines of code and reduce the workload of developers to configure xml, use duboo's annotation mode to reduce There are more possibilities for configuration problems!

2. Dubbo use case
Duboo annotation
Interface class project: DubboServiceInterface




is just an interface class project! The interface is a normal interface!




Note: Package the interface class projects into jars and put them into the server project and the client project respectively!
Server project: DubboServiceProvider




implementation class fooserviceImpl.java




package com.alibaba.dubbo.demo.imp;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.dubbo.demo.DemoService;
@Service(version= "1.0")
public class FooServiceImpl implements DemoService {
@Override
public String sayHello(String name) {
        return "Hello " + name;
    }
}
web.xml 配置扫描内容




<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>DubboServiceProvider</display-name>
<servlet>
<servlet-name>spring</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>*</url-pattern>
</servlet-mapping>
  <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
    <context-param>
<param-name>contextConfigLocation</param-name> <param-value>classpath*:applicationContext.xml</param-value>
</context-param>
</web-app>
applicationContext.xml 配置






<?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/schema/dubbo 
        http://code.alibabatech.com/schema/dubbo/dubbo.xsd" > 
       
<!-- Server-Service Provider-->
  <!-- Public information, which can also be configured with dubbo.properties-->
  <dubbo:application name="test" />
<!-- Link zookeeper -- >
  <dubbo:registry address="zookeeper://127.0.0.1:2181/" group="test"/>
  <dubbo:consumer timeout="5000"/>
  <!-- Scan annotation package path, multiple packages use Comma-separated, leave out pacakge to scan all classes in the current ApplicationContext -->
<dubbo:annotation package="com.unj.dubbotest.serviceImp" ​​/>
 
<!-- xml configuration: declare the service interface that needs to be exposed -->
<!-- <dubbo: service interface="com.alibaba.dubbo.demo.DemoService" ref="demoService" /> -->
   <!-- xml配置 :和本地bean一样实现服务-->
<!--     <bean id="demoService" class="com.unj.dubbotest.serviceImp.FooServiceImpl" /> -->
</beans>
测试类Provider



package com.alibaba.dubbo.test;

import java.io.IOException;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Provider {
@Before
public void setUp() throws Exception {
}
@Test
public void testMain() throws IOException {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
new String[] { "applicationContext.xml" });
context.start();
System.in.read();// Press any key to exit
}
}
jar package under




lib Client project: DubboServiceConsumer




web.xml Configure scan content




applicationContext.xml file




<?xml version="1.0" encoding="UTF-8 "?>
<beans xmlns="http://www.springframework.org/schema/beans" 
xmlns:context="http://www.springframework.org/schema/context"
    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/schema/dubbo 
        http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> 
       
<!-- Client-Service Consumer-->
  <!-- Public information, you can also configure it with dubbo.properties-->
  < dubbo:application name="xx" />
  <dubbo:registry address="zookeeper://127.0.0.1:2181" />
  <dubbo:consumer timeout="5000"/>
<!-- Scan annotation package path, more The packages are separated by commas. If pacakge is not filled in, it means to scan all classes in the current ApplicationContext -->
    <dubbo:annotation package="com.unj.dubbotest.action" />
</beans>
Test class: Consumer





package com.unj. dubbotest.action;
import java.io.IOException;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.alibaba.dubbo. config.annotation.Reference;
import com.alibaba.dubbo.demo.DemoService;
public class Consumer{
@Reference(version = "1.0")
private DemoService demoService;

@Test
public void mainTest() throws IOException {
ClassPathXmlApplicationContext context =new ClassPathXmlApplicationContext(
new String[] {" applicationContext.xml"});
        context.start();
         demoService = (DemoService)context.getBean("demoService"); // Get remote service proxy
        String hello = demoService.sayHello("world"); // Execute remote method
        System.out.println( hello ); // Display the call result
    }
}
The jar package under lib




Get massive video



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326133173&siteId=291194637