dubbo集成spring-boot
dubbo需要一个注册中心,用的是zookeeper。集成spring-boot是为了方便使用注解开发。
1. 安装zoopkker
hadoop01:2181, hadoop02:2181, hadodp03:2181
2. 编写dubbo公共接口
HelloService
package com.dubbo.service; import com.dubbo.model.User; public interface HelloService { String sayHello(String name); }
3. 编写provider 并注册到zookeeper
这里需要注意的是:接口实现类上面标的注解@Service是 com.alibaba.dubbo.config.annotation.Service;
还要在启动类上面加上一个注解:@DubboComponentScan
3.1 HelloService实现类
package com.dubbo.provider.service; import java.util.UUID; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.alibaba.dubbo.config.annotation.Service; import com.dubbo.service.HelloService; @Service public class HelloServiceImpl implements HelloService { private static Logger LOGGER = LoggerFactory.getLogger(HelloServiceImpl.class); @Override public String sayHello(String name) { LOGGER.info("request from consumer...param...{}", name); return "hello " + name; } }
3.2 dubbo配置类
package com.dubbo.provider.conf; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import com.alibaba.dubbo.config.ApplicationConfig; import com.alibaba.dubbo.config.ProtocolConfig; import com.alibaba.dubbo.config.RegistryConfig; @Configuration public class ProviderConf { // <dubbo:registry // address="zookeeper://10.20.153.10:2181?backup=10.20.153.11:2181,10.20.153.12:2181" // /> // <dubbo:registry protocol="zookeeper" // address="10.20.153.10:2181,10.20.153.11:2181,10.20.153.12:2181" /> @Bean public ApplicationConfig applicationConfig() { ApplicationConfig applicationConfig = new ApplicationConfig(); applicationConfig.setName("provider-test"); return applicationConfig; } @Bean public RegistryConfig registryConfig() { RegistryConfig registryConfig = new RegistryConfig(); registryConfig.setAddress("zookeeper://hadoop01:2181?backup=hadoop02:2181,hadoop03:2181"); registryConfig.setClient("zkclient"); return registryConfig; } @Bean public ProtocolConfig protocolConfig() { ProtocolConfig protocolConfig = new ProtocolConfig(); protocolConfig.setName("dubbo"); protocolConfig.setPort(20880); return protocolConfig; } }
3.3 provider启动类
package com.dubbo.provider; import java.io.IOException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import com.alibaba.dubbo.config.spring.context.annotation.DubboComponentScan; @SpringBootApplication @DubboComponentScan(basePackages = { "com.dubbo.provider.service" }) public class Provider { private static Logger LOGGER = LoggerFactory.getLogger(Provider.class); public static void main(String[] args) throws IOException { LOGGER.debug("startup provider..."); SpringApplication.run(Provider.class, args); System.in.read(); } }
4. 编写consumer调用服务
这里需要注意:注入远程HelloService对象的时候,要用@Reference,不是用@Autowired,因为dubbo目前是使用@Reference来做的,底层用了ReferenceAnnotationBeanPostProcessor来注入@Reference标注的属性。
4.1 controller调用远程HelloService服务
package com.dubbo.consumer.controller; import org.springframework.http.MediaType; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import com.alibaba.dubbo.config.annotation.Reference; import com.dubbo.service.HelloService; @RestController @RequestMapping(path = "/hello", produces = MediaType.APPLICATION_JSON_UTF8_VALUE) public class HelloController { @Reference private HelloService helloService; @RequestMapping(path = "/say", method = RequestMethod.GET) public String sayHello(String name) { return helloService.sayHello(name); } }
4.2 consumer配置类
package com.dubbo.consumer.conf; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import com.alibaba.dubbo.config.ApplicationConfig; import com.alibaba.dubbo.config.ConsumerConfig; import com.alibaba.dubbo.config.RegistryConfig; @Configuration public class ConsumerConf { @Bean public ApplicationConfig applicationConfig() { ApplicationConfig applicationConfig = new ApplicationConfig(); applicationConfig.setName("consumer-test"); return applicationConfig; } @Bean public ConsumerConfig consumerConfig() { ConsumerConfig consumerConfig = new ConsumerConfig(); consumerConfig.setTimeout(3000); return consumerConfig; } @Bean public RegistryConfig registryConfig() { RegistryConfig registryConfig = new RegistryConfig(); registryConfig.setAddress("zookeeper://hadoop01:2181?backup=hadoop02:2181,hadoop03:2181"); registryConfig.setClient("zkclient"); return registryConfig; } }
4.3 consumer启动类
package com.dubbo.consumer; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import com.alibaba.dubbo.config.spring.context.annotation.DubboComponentScan; @SpringBootApplication @DubboComponentScan(basePackages = { "com.dubbo.consumer.controller" }) public class Consumer { public static void main(String[] args) { SpringApplication.run(Consumer.class, args); } }
上一篇: windows安装tensorflow-gpu开发环境
下一篇: 更新web项目,当前项目下所有的jsp文件报错(“The superclass "javax.servlet.http.HttpServlet" was”) n
推荐阅读
-
Spring Boot集成springfox-swagger2构建restful API的方法教程
-
Spring MVC集成springfox-swagger2构建restful API的方法详解
-
浅析Yii2集成富文本编辑器redactor实例教程
-
spring boot集成shiro详细教程(小结)
-
Dubbo无法访问远程Zookeeper已注册服务的问题解决方案
-
详解spring-boot下如何满足多生产环境中个性化定制功能
-
Springboot-dubbo-fescar 阿里分布式事务的实现方法
-
Spring集成Redis详解代码示例
-
iOS Crash常规跟踪方法及Bugly集成运用详细介绍
-
详解iOS集成GoogleMap(定位、搜索)