<!-- 如果dubbo的配置来自于单独的xml文件,不是来自于spring boot的application配置文件 那么,导入dubbo相关包,必须单独导入,不能用spring boot和dubbo的jar包-->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.8</version>
<exclusions>
<!--如果你用的logback日志,该包会引起jar包冲突-->
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.3</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.3</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
</exclusion>
</exclusions>
</dependency>
dubbo-provider.xml
(名字随便起),内容如下:<!-- 提供方应用名称信息,这个相当于起一个名字,我们dubbo管理页面比较清晰是哪个应用暴露出来的 -->
<dubbo:application name="dubbo-provider-qssj"/>
<!-- 使用zookeeper注册中心暴露服务地址 -->
<dubbo:registry protocol="zookeeper" address="192.168.1.160:2181" />
<dubbo:protocol name="dubbo" port="31001" />
<dubbo:service interface="test.spring.dubboService.TestDubboService" ref="testDubboService" timeout="1800000" version="1.0.0.1"/>
<bean id="testDubboService" class="test.spring.dubboService.impl.TestDubboServiceImpl"/>
@Service
注解,但是这个注解就变成了org.springframework.stereotype.Service
。具体如下。package test.spring.dubboService.impl;
import org.springframework.stereotype.Service;
import test.spring.dubboService.TestDubboService;
@Service
public class TestDubboServiceImpl implements TestDubboService {
@Override
public String getName(String name) {
// TODO Auto-generated method stub
return "姓名:"+name;
}
}
dubbo-provider.xml
文件,如下:package test.spring;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;
@SpringBootApplication //spring boot启动必须引入的注解
@ImportResource({"classpath:dubbo-provider.xml"})
public class SpringBootTest {
public static void main(String[] args) {
SpringApplication.run(SpringBootTest.class, args);
}
}
结构与上一篇那种springboot 的properties配置dubbo没什么区别。
dubbo-consumer.xml
配置文件,内容如下:<!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->
<dubbo:application name="dubbo-consumer-qssj" />
<dubbo:consumer timeout="1800000" retries="0" />
<dubbo:registry protocol="zookeeper" address="192.168.1.160:2181" />
<dubbo:reference id="testDubboService" interface="test.spring.dubboService.TestDubboService" check="false" version="1.0.0.1"/>
test.spring.dubboService.TestDubboService
接口放到消费者这边,如下:@Autowired
注解,如下:package test.spring.dubboService.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import test.spring.dubboService.TestDubboService;
@RestController
public class TestController {
@Autowired
TestDubboService testDubboService;
@RequestMapping(value="abc/akf",method=RequestMethod.GET)
public String abc(String name){
return testDubboService.getName(name);
}
}
dubbo-consumer.xml
,具体如下:package test.spring;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;
@SpringBootApplication //spring boot启动必须引入的注解
@ImportResource({"classpath:dubbo-consumer.xml"})
public class SpringBootTest {
public static void main(String[] args) {
SpringApplication.run(SpringBootTest.class, args);
}
}
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://blog.csdn.net/qq_43842093/article/details/121546949
内容来源于网络,如有侵权,请联系作者删除!
dubbo-provider.xml
(名字随便起),内容如下:@Service
注解,但是这个注解就变成了org.springframework.stereotype.Service
。具体如下。dubbo-provider.xml
文件,如下:dubbo-consumer.xml
配置文件,内容如下:test.spring.dubboService.TestDubboService
接口放到消费者这边,如下:@Autowired
注解,如下:dubbo-consumer.xml
,具体如下: