分布式項目首先想到的問題是如何再多個服務之間進行數據傳遞和接口調用
1、創建兩個服務,一個controller,一個service
service層的實現類使用的service注解,是alibaba的,不是jdk的
//service層的一個類
package com.ghh.service; import com.alibaba.dubbo.config.annotation.Service; @Service public class TestServiceImpl implements TestService { @Override public String getName() { return "張三"; } }
//接口
public interface TestService {
public String getName();
}
//service服務的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: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="dubbodemo-service"/> <!--配置連接的端口號--> <dubbo:registry address="zookeeper://192.168.200.128:2181"/> //虛擬機上的zookeeper的地址 <!-- 掃描帶有service的注解,注冊到zeekooper的注冊中心--> <dubbo:annotation package="com.ghh" /> </beans>
//service服務的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"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<!-- 加載spring容器 -->
<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、controller服務的
@Reference //使用的是alibaba的注解,不是jdk的
@RestController @RequestMapping("/test") public class TestController { @Reference private TestService testService; @RequestMapping("/getName") public String getName(){ System.out.println("123"); String name = testService.getName(); return name; } @RequestMapping("/me") public String name(){ return "張三"; } }
此處,我沒有導入service服務的jar包,我直接將service服務的接口導到controller服務中,直接調用
controller服務的applicationContext-web.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: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"> <!-- 注解驅動 --> <mvc:annotation-driven > <!-- 將傳輸的字符串強制轉換成utf-8編碼, 防止中文亂碼 --> <mvc:message-converters register-defaults="false"> <bean class="org.springframework.http.converter.StringHttpMessageConverter"> <constructor-arg value="UTF-8" /> </bean> </mvc:message-converters> </mvc:annotation-driven> <!-- 給當前項目服務起個名--> <dubbo:application name="dubboxdemo-web" /> <!-- 配置連接zookepper --> <dubbo:registry address="zookeeper://192.168.200.128:2181"/> <!-- 配置包掃描, 只有在這個包下面才可以注入service --> <dubbo:annotation package="com.ghh" /> </beans>
//controller服務的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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> <!-- 解決post亂碼 --> <filter> <filter-name>CharacterEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>utf-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- 配置springMvc前端控制器 --> <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:applicationContext-web.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
3、總結
dubbo+zookeeper的實現,是將各服務注冊到zookeeper,controller通過@Reference注解,從zookeeper中拿到service的代理類對象,service中通過alibaba的@service注解注冊到zookeeper中
實現了兩個服務之間的跨服調用,接口調用。