通过application.properties配置文件配置dubbo信息:
服务提供者:
配置文件:
spring.dubbo.application.name=provider spring.dubbo.registry.address=zookeeper://127.0.0.1:2181 spring.dubbo.protocol.name=dubbo spring.dubbo.protocol.port=20880 spring.dubbo.scan=com.springboot.springdubboprovider.serviceImpl
pom文件(使用配置文件时添加,xml不添加):
<!-- Spring Boot Dubbo 依赖 --> <dependency> <groupId>io.dubbo.springboot</groupId> <artifactId>spring-boot-starter-dubbo</artifactId> <version>1.0.0</version> </dependency>
提供者业务层实现:
import com.alibaba.dubbo.config.annotation.Service; import com.alibaba.dubbo.demo.DemoService; import com.springboot.springdubboprovider.mapper.test1.User1Mapper; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import java.util.HashMap; import java.util.Map; /** * 城市业务 Dubbo 服务层实现层 * * Created by bysocket on 28/02/2017. */ // 注册为 Dubbo 服务 //@Service @Service public class DubboServiceImpl implements DemoService { Logger log = LoggerFactory.getLogger(getClass()); @Autowired private User1Mapper user1Mapper; @Override public Map getPermissions(String name, int id) { log.info("接收到消费者请求"); Map map = new HashMap(); int row = user1Mapper.getAll(); map.put("msg","springboot-dubbo提供者"); map.put("row",row); // JSONObject jsonObject = new JSONObject(); // JSONObject res = HttpResponse.response("aaa",false,jsonObject); // System.out.println("aaaaaaaaaa:"+res); // map.put("res",res); return map; }
spring boot 启动文件:
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.annotation.ImportResource; import org.springframework.scheduling.annotation.EnableAsync; import org.springframework.scheduling.annotation.EnableScheduling; import com.alibaba.dubbo.spring.boot.annotation.EnableDubboConfiguration; import javax.annotation.PreDestroy; @EnableScheduling //@EnableDubboConfiguration @SpringBootApplication //@ImportResource({"classpath:dubbo-provider.xml"}) public class SpringDubboProviderApplication { public static void main(String[] args) { SpringApplication.run(SpringDubboProviderApplication.class, args); } }
消费者:
配置文件:
## Dubbo 服务消费者配置 spring.dubbo.application.name=consumer spring.dubbo.registry.address=zookeeper://localhost:2181 spring.dubbo.scan=com.springboot.springdubboconsumer.serviceImpl
pom文件和提供者一样(使用配置文件添加,xml不添加)
调用类(@reference是alibaba.dubbo的):
import com.alibaba.dubbo.config.annotation.Reference; import com.alibaba.dubbo.demo.DemoService; import com.alibaba.fastjson.JSONObject; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import java.util.Map; @Component public class DubboImpl { Logger log = LoggerFactory.getLogger(getClass()); @Reference private DemoService demoService; public JSONObject test1(){ Map map = demoService.getPermissions("xulei",1); log.info("调用dubbo接口返回:{}",map); JSONObject jsonObject = new JSONObject(); jsonObject.put("结果",map); return jsonObject; } }
启动类:
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.annotation.ImportResource; import org.springframework.scheduling.annotation.EnableAsync; import org.springframework.scheduling.annotation.EnableScheduling; import com.alibaba.dubbo.spring.boot.annotation.EnableDubboConfiguration; import javax.annotation.PreDestroy; @EnableScheduling //@EnableDubboConfiguration @SpringBootApplication //@ImportResource({"classpath:dubbo-consumer.xml"}) public class SpringDubboConsumerApplication { public static void main(String[] args) { SpringApplication.run(SpringDubboConsumerApplication.class, args); } }
通过xml方式配置:
提供者:
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-admin 或 dubbo-monitor 会显示这个名字,方便辨识--> <dubbo:application name="demotest-provider" owner="programmer" organization="dubbox"/> <!--使用 zookeeper 注册中心暴露服务,注意要先开启 zookeeper--> <dubbo:registry address="zookeeper://localhost:2181"/> <!-- 用dubbo协议在20880端口暴露服务 --> <dubbo:protocol name="dubbo" port="20880" /> <!--使用 dubbo 协议实现定义好的 api.PermissionService 接口--> <dubbo:service interface="com.alibaba.dubbo.demo.DemoService" ref="demoService" protocol="dubbo" /> <!--<dubbo:reference id="demoService" check="false" interface="com.alibaba.dubbo.demo.DemoService" />--> <!--具体实现该接口的 bean--> <bean id="demoService" class="com.springboot.springdubboprovider.serviceImpl.DubboServiceImpl"/> </beans>
启动文件:
@EnableScheduling //@EnableDubboConfiguration @SpringBootApplication @ImportResource({"classpath:dubbo-provider.xml"}) public class SpringDubboProviderApplication { public static void main(String[] args) { SpringApplication.run(SpringDubboProviderApplication.class, args); } }
消费者:
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:application name="demotest-consumer" owner="programmer" organization="dubbox"/> <!--向 zookeeper 订阅 provider 的地址,由 zookeeper 定时推送--> <dubbo:registry address="zookeeper://localhost:2181"/> <!--使用 dubbo 协议调用定义好的 api.PermissionService 接口--> <dubbo:reference id="demoService" interface="com.alibaba.dubbo.demo.DemoService"/> </beans>
实现类:
import com.alibaba.dubbo.config.annotation.Reference; import com.alibaba.dubbo.demo.DemoService; import com.alibaba.fastjson.JSONObject; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import java.util.Map; @Component public class DubboImpl { Logger log = LoggerFactory.getLogger(getClass()); @Autowired private DemoService demoService; public JSONObject test1(){ Map map = demoService.getPermissions("xulei",1); log.info("调用dubbo接口返回:{}",map); JSONObject jsonObject = new JSONObject(); jsonObject.put("结果",map); return jsonObject; } }
启动文件:
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.annotation.ImportResource; import org.springframework.scheduling.annotation.EnableAsync; import org.springframework.scheduling.annotation.EnableScheduling; import com.alibaba.dubbo.spring.boot.annotation.EnableDubboConfiguration; import javax.annotation.PreDestroy; @EnableScheduling //@EnableDubboConfiguration @SpringBootApplication @ImportResource({"classpath:dubbo-consumer.xml"}) public class SpringDubboConsumerApplication { public static void main(String[] args) { SpringApplication.run(SpringDubboConsumerApplication.class, args); } }