<!--dubbo--> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.8.4</version> <exclusions> <exclusion> <artifactId>spring</artifactId> <groupId>org.springframework</groupId> </exclusion> </exclusions> </dependency> <!--zookeeper--> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4.6</version> </dependency> <!-- https://mvnrepository.com/artifact/com.101tec/zkclient --> <dependency> <groupId>com.101tec</groupId> <artifactId>zkclient</artifactId> <version>0.7</version> </dependency>
1)工程結構以下java
2)pom.xmlweb
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>demo</artifactId> <groupId>com.company</groupId> <version>1.0.0</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>demo-provider</artifactId> <packaging>war</packaging> <dependencies> <dependency> <groupId>com.company</groupId> <artifactId>demo-service</artifactId> <version>1.0.0</version> </dependency> <dependency> <groupId>com.company</groupId> <artifactId>demo-api</artifactId> <version>1.0.0</version> </dependency> </dependencies> </project>
3)UserApiImpl.javaredis
package com.company.provider.impl; import com.alibaba.dubbo.config.annotation.Service; import com.company.api.dto.UserDTO; import com.company.api.iapi.UserApi; import com.company.data.model.User; import com.company.service.UserService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; /** * Created by wsyi on 2016/11/29. */ @Service public class UserApiImpl implements UserApi { private static final Logger LOGGER = LoggerFactory.getLogger(UserApiImpl.class); @Autowired private UserService userService; @Override public UserDTO qryById(Long id) { UserDTO userDTO = new UserDTO(); User user = userService.qryUserById(id); BeanUtils.copyProperties(user,userDTO); return userDTO; } }
注意:@Service爲com.alibaba.dubbo.config.annotation.Service,非Spring中註解Service。spring
4)spring-config.xmlexpress
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" 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:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd "> <!--掃描spring註解中bean對象--> <context:component-scan base-package="com.company"/> <!--spring最多加載一個context:property-placeholder--> <context:property-placeholder location="classpath*:jdbc.properties"/> <!--將其餘配置文件--> <import resource="classpath*:spring-mybatis.xml"/> </beans>
5)spring-provider.xml:apache
<?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">
<!--掃描dubbo註解類-->
<dubbo:annotation package="com.company.provider.impl" />
</beans>
6)dubbo.propertiesapi
## # Copyright 1999-2011 Alibaba Group. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. ## #dubbo.container=log4j,spring dubbo.application.name=demo-provider-server #dubbo.application.owner=william #dubbo.registry.address=multicast://224.5.6.7:1234 dubbo.registry.address=zookeeper://127.0.0.1:2181 #dubbo.registry.address=redis://127.0.0.1:6379 #dubbo.registry.address=dubbo://127.0.0.1:9090 #dubbo.monitor.protocol=registry dubbo.protocol.name=dubbo dubbo.protocol.port=20880 #dubbo.service.loadbalance=roundrobin #dubbo.log4j.file=logs/dubbo-demo-consumer.log #dubbo.log4j.level=WARN dubbo.service.version=wsy
7)web.xmlspring-mvc
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:META-INF/spring/spring-*.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> </web-app>
1)啓動zookeeper、dubbo-admin服務。tomcat
2)將demo-provider部署到tomcat上,啓動服務。mybatis
3)dubbo-admin服務