项目provicderandconsumer 结构如下:
pom.xml (我的springboot 版本是2.0.4.RELEASE)
<?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">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>provicderandconsumer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>provicderandconsumer</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>com.alibaba.spring.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.14</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</version>
<scope>compile</scope>
</dependency>
<!--web-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
公共服务接口 UserService
package com.example.springbootapi.dubbo.service;
/**
* @author
*/
public interface UserService {
public String print(String s);
}
UserServiceImpl
package com.example.provicderandconsumer.service;
import com.example.springbootapi.dubbo.service.UserService;
//提供者提供的接口服务 service 采用xml配置
//@Service(interfaceClass = UserService.class)
//@Component
public class UserServiceImpl implements UserService {
@Override
public String print(String s) {
return "say2:" + s;
}
}
UserController
package com.example.provicderandconsumer.controller;
import com.example.springbootapi.dubbo.service.UserService;
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;
/**
* @author
*/
@Controller
public class UserController {
//reference 在xml配置,这里需要用注解Autowired
@Autowired
public UserService userService;
@RequestMapping("/test")
@ResponseBody
public String test(){
String s = userService.print("123sssssssss");
return s;
}
}
dubbo.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">
<dubbo:registry address="zookeeper://127.0.0.1:2181" timeout="60000"/>
<!-- port值-1 表示让dubbo自行找一个可用的port -->
<dubbo:protocol port="-1" name="dubbo"/>
<!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->
<dubbo:application name="dubbo"/>
<!--消费者-->
<!--关闭服务消费方所有服务的启动检查。dubbo缺省会在启动时检查依赖的服务是否可用,不可用时会抛出异常,阻止Spring初始化完成。url="dubbo//172.16.1.112:20880"-->
<dubbo:consumer check="false" />
<!--reference 采用xml配置实现,在代码中获取远程服务需要加注解@Autowired-->
<dubbo:reference id="userService" check="false" interface="com.example.springbootapi.dubbo.service.UserService"/>
<dubbo:annotation package="com.example.provicderandconsumer.*"/>
<!--提供者-->
<dubbo:service protocol="dubbo" ref="userService" interface="com.example.springbootapi.dubbo.service.UserService"/>
<bean id="userService" class="com.example.provicderandconsumer.service.UserServiceImpl"/>
</beans>
启动类 ProvicderandconsumerApplication
package com.example.provicderandconsumer;
import com.alibaba.dubbo.spring.boot.annotation.EnableDubboConfiguration;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ImportResource;
@EnableDubboConfiguration
@ImportResource(locations = {"classpath:dubbo.xml"})
@ComponentScan(basePackages = "com.example.provicderandconsumer.*")
@SpringBootApplication
public class ProvicderandconsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ProvicderandconsumerApplication.class, args);
}
}
application.properties
server.port=9993
启动springboot程序,如下:
登录 dubbo管理平台 , 这里显示虽然没有消费者(这个暂时不知道原因),实际是有的,下面测试运行正常。
浏览器输入: http://127.0.0.1:9993/test
源码
链接:https://pan.baidu.com/s/1hlGIkmFHpvBs-1uv2eahXQ
提取码:qwfj
注意:需要先启动 zookeeper 和 dubbo-admin(管理平台) ,如果没有安装看下我之前的文章:https://blog.youkuaiyun.com/a704397849/article/details/91904085
更多的springboot源码看下我的博客分类dubbo