dubbo集成springboot
1.项目依赖
<dependencies>
<dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo</artifactId> <version>${dubbo.version}</version> </dependency>
<dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-dependencies-zookeeper</artifactId> <version>${dubbo.version}</version> <exclusions> <exclusion> <artifactId>slf4j-log4j12</artifactId> <groupId>org.slf4j</groupId> </exclusion> </exclusions> <type>pom</type> </dependency>
<!-- Spring Boot dependencies --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency>
</dependencies>
<dependencyManagement> <dependencies> <!-- Spring Boot --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>${spring-boot.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
|
2.服务提供者
resource/spring目录下添加dubbo-provider.xml:
通过 Spring 配置引用远程服务
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://dubbo.apache.org/schema/dubbo" xmlns="http://www.springframework.org/schema/beans" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
<dubbo:application metadata-type="remote" name="demo-springboot-provider"/> <dubbo:metadata-report address="zookeeper://127.0.0.1:2181"/>
<dubbo:registry address="zookeeper://127.0.0.1:2181"/>
<dubbo:protocol name="dubbo"/> <!-- dubbo注解扫描 --> <dubbo:annotation package="cn.wjs"/> <!-- 生成远程服务代理,可以和本地bean一样使用demoService --> <!-- <dubbo:service interface="cn.wjs.DemoService" ref="demoService"/>-->
</beans>
|
DemoServiceImpl:
import org.apache.dubbo.config.annotation.Service;
@Service public class DemoServiceImpl implements DemoService {
public String sayHello(String name) { return "Hello " + name; }
}
|
如果上述配置文件里面使用<dubbo:service>方式,这里可以不用加@Service
<dubbo:annotation>方式,需加上@Service
Application:
@SpringBootApplication @ImportResource(value = {"classpath:spring/dubbo-provider.xml"}) public class Application { public static void main(String[] args) throws Exception{ SpringApplication.run(Application.class, args);
System.in.read();
} }
|
3.服务消费者
resource/spring目录下添加dubbo-consumer.xml
通过 Spring 配置引用远程服务
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://dubbo.apache.org/schema/dubbo" xmlns="http://www.springframework.org/schema/beans" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
<dubbo:application name="dubbo-springboot-xml-consumerr"/>
<dubbo:registry address="zookeeper://127.0.0.1:2181"/>
<!-- 使用注解方式创建远程服务代理--> <dubbo:annotation package="cn.wjs"/>
<!-- <dubbo:reference id="demoService" check="false" interface="cn.wjs.DemoService"/>-->
</beans>
|
SayService:
import org.apache.dubbo.config.annotation.Reference; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component;
@Component public class SayService {
@Reference DemoService demoService;
public String say (String name) {
return demoService.sayHello(name); }
}
|
若上述配置采用<dubbo:annotation>方式,此处使用duboo提供@Reference注解
若上述配置采用<dubbo:reference>方式,此处使用@Autowired原生注解
Application:
@SpringBootApplication @RestController @ImportResource("classpath:spring/dubbo-consumer.xml") public class Application {
@Autowired SayService sayService;
@RequestMapping("/hello") public String say(@RequestParam("name") String name) { return sayService.say(name); }
public static void main(String[] args) throws Exception{ SpringApplication.run(Application.class, args);
} }
|
访问localhost:8081/hello?name=zhangsan, 返回 hello zhangsan, 消费成功
github地址:
服务端: dubbo-springboot-xml-provider
消费端: dubbo-springboot-xml-consumer
接口: dubbo-xml-interface
https://github.com/wujinsen/dubbo-learning
dubbo集成springboot
原文:https://www.cnblogs.com/wujinsen/p/12591655.html