SpringBoot下的Dubbo和Zookeeper整合
最近一直在学各种分布式的内容,学到了dubbo分布式服务框架就想写个小demo使用一下,但是由于我要整合在SpringBoot框架中,SpringBoot框架毕竟提倡的是java注解配置,XML文件不提倡,所以只能利用@ImportResource这个注解来引入dubbo配置的XML文件。
首先是Maven项目的Pom文件:
除了基本的SpringBoot的基本web配置jar包需要添加以下依赖(服务提供方和消费方一样):
<properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>1.3.6.RELEASE</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> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4.6</version> <exclusions> <exclusion> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> </exclusion> <exclusion> <groupId>log4j</groupId> <artifactId>log4j</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>com.github.sgroschupf</groupId> <artifactId>zkclient</artifactId> <version>0.1</version> </dependency> </dependencies>
接下来是服务的提供方:
服务接口代码
public interface TestService { public String sayHello(); }
接口实现代码
import org.springframework.stereotype.Service; @Service("testService") public class TestServiceImpl implements TestService { //@Override public String sayHello() { return "hello dubbo"; } }
接下来是服务方的XML配置文件provider.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="provider" />
<!-- 使用zookeeper作为注册中心 -->
<dubbo:registry protocol="zookeeper" address="zookeeper://192.168.1.14:2181" />
<!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
<dubbo:service interface="com.bee.test.TestService" ref="testService"></dubbo:service>
</beans>
SpringBoot启动类(注意要引入XML配置文件):
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer; import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer; import org.springframework.boot.context.web.SpringBootServletInitializer; import org.springframework.context.annotation.ImportResource; @SpringBootApplication @ImportResource("classpath:provider.xml") // 很重要 public class Application extends SpringBootServletInitializer implements EmbeddedServletContainerCustomizer { @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) { return builder.sources(Application.class); } public static void main(String[] args) { SpringApplication.run(Application.class, args); } //@Override public void customize(ConfigurableEmbeddedServletContainer container) { container.setPort(8081); } }
接下来是服务消费方
这里接口要和服务的提供方保持一致不能有区别。
配置文件consumer
.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="consumer" /> <!-- zookeeper作为注册中心 --> <dubbo:registry protocol="zookeeper" address="zookeeper://192.168.1.14:2181" /> <!-- 生成远程服务代理,可以和本地bean一样使用demoService --> <dubbo:reference interface="com.bee.test.TestService" id="testService"></dubbo:reference> </beans>
Controller类:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class MyController {
@Autowired
private TestService testService;
@RequestMapping("/hello")
public String home(){
return testService.sayHello();
}
}
启动类:
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer; import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer; import org.springframework.boot.context.web.SpringBootServletInitializer; import org.springframework.context.annotation.ImportResource; @SpringBootApplication @ImportResource("classpath:consumer.xml") public class Application extends SpringBootServletInitializer implements EmbeddedServletContainerCustomizer{ public static void main(String[] args) { SpringApplication.run(Application.class,args); } @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) { return builder.sources(Application.class); } //@Override public void customize(ConfigurableEmbeddedServletContainer container) { container.setPort(8082); } }
最后运行服务端和消费端,访问消费端的controller成功调用服务。。。