直接上代码 把 我用的是xml的配置方式 一直想用配置类的方式 没找到教程 ,无奈 可能xml的形式更受大众欢迎把 用到了引入配置文件的注解 @ImportResource
项目结构:
dubbo_interf 是公共接口类
公共接口类代码:
package cn.dubbo.interf;
import java.util.List;
public interface StuService {
public List<String> findStuByClassName(String className);
}
提供者:
package cn.dubbo.serviceImpl;
import cn.dubbo.interf.StuService;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
@Service("stuService")
public class StuServiceImpl implements StuService {
@Override
public List<String> findStuByClassName(String className) {
List<String> stuList = new ArrayList<String>();
if("1".equals(className)){
stuList.add("何睿");stuList.add("张三");
}else{
stuList.add("李四");
}
return stuList;
}
}
消费者:
package cn.dubbo.controller;
import cn.dubbo.interf.StuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.List;
@Controller
public class StuController {
@Autowired
private StuService stuService;
@RequestMapping("/name")
@ResponseBody
public List find(){
return stuService.findStuByClassName("1");
}
}
附上 配置文件:
provider的:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
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-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/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!-- 定义服务端的应用名 所属用户 -->
<dubbo:application name="hr-provider" owner="hr" />
<!-- 向zookeeper注册 -->
<dubbo:registry address="zookeeper://localhost:2181" />
<!-- 暴露服务 端口 -->
<dubbo:protocol name="dubbo" port="20880" />
<!-- 定义服务 实现定义好的公共接口 -->
<dubbo:service interface="cn.dubbo.interf.StuService" ref="stuService" protocol="dubbo" />
</beans>
消费者的:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
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-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/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!-- 定义客户端的应用名 所属用户 -->
<dubbo:application name="hr-consumer" owner="hr" />
<!-- 向zookeeper注册 -->
<dubbo:registry address="zookeeper://localhost:2181" />
<dubbo:reference id="stuService" interface="cn.dubbo.interf.StuService" check="false" />
</beans>
最重要的 SpringBoot启动类: yml端口两个工程要修改不一致 我的为9000 和 9001
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;
@SpringBootApplication(scanBasePackages = "cn.dubbo")
@ImportResource(locations = {"classpath:applicationContext-dubbo.xml"})
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class,args);
}
}
dubbo的maven:
<!-- Dubbo -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.3</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.3.6</version>
<exclusions>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.10</version>
</dependency>
</dependencies>
//接下来 整整SpringBoot 开个数据结构专题 再好好把ELK 日志分析系统 整一下