dubbo入门例子

有问题欢迎评论

1.服务提供方


1.1新建maven工程(war)

略........

1.2 写配置文件 applicationContextService.xml

<dubbo:application name="" > 设置当前应用名称

<dubbo:registry address=""> 设置注册中心地址

<dubbo:annotation package = "" > 扫描服务(扫描service注解)

<?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:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

  
	<dubbo:application name="dubbo-demo"/>

	<dubbo:registry address="zookeeper://127.0.0.1:2181" />

	<dubbo:annotation package="com.alibaba.service.impl"/>   
</beans>

1.3 新建服务

新建一个接口

public interface UserService {
	
	public String getName();
}

实现类 @Service注解使用alibaba的

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.service.UserService;

@Service
public class UserServiceImpl implements UserService {
	
	@Override
	public String getName() {
		return "我的眼睛望着窗外,不知如何对你表白";
	}
}

web.xml

加载spring容器

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	version="2.5">	

	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:applicationContext*.xml</param-value>
	</context-param>
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	
</web-app>

2.服务消费方


2.1 新建maven工程(war)

略...

2.2 写配置文件 springmvc.xml

<dubbo:annotation package="com.alibaba.controller" /> 这里扫描contorller类

        <dubbo:application name="dubbo-web"/>

	<dubbo:registry address="zookeeper://127.0.0.1:2181" />

	<dubbo:annotation package="com.alibaba.controller" />

web.xml

<servlet>
  	<servlet-name>springmvc</servlet-name>
  	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  	<!-- 指定加载的配置文件 ,通过参数contextConfigLocation加载-->
  	<init-param>
  		<param-name>contextConfigLocation</param-name>
  		<param-value>classpath:springmvc.xml</param-value>
  	</init-param>
  </servlet>
  
  <servlet-mapping>
  	<servlet-name>springmvc</servlet-name>
  	<url-pattern>*.do</url-pattern>
  </servlet-mapping>

2.3 新建类

把上面的service接口复制过来(不是移动)

public interface UserService {
	
	public String getName();
}
@RestController
public class UserController {

	//远程注入
	@Reference
	private UserService userService;
	
	@RequestMapping("/getName")
	public String getName(){
		return userService.getName();
	}
}

3.测试


工程代码

链接: https://pan.baidu.com/s/1B1vMruGPFEqhxNEiHMJdag 提取码: 4vjs 


猜你喜欢

转载自blog.csdn.net/weixin_42195284/article/details/84034397