Spring Boot整合Dubbo

This commit is contained in:
zhaiyongchao
2016-07-14 10:54:13 +08:00
parent 47032cd6b7
commit 7f0bfb5afe
16 changed files with 283 additions and 72 deletions
+83
View File
@@ -0,0 +1,83 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.didispace</groupId>
<artifactId>compute-service</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>
<name>compute-service</name>
<description>Spring Boot project</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.3</version>
<exclusions>
<exclusion>
<artifactId>spring</artifactId>
<groupId>org.springframework</groupId>
</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>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
@@ -0,0 +1,30 @@
package com.didispace;
import org.apache.log4j.Logger;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ImportResource;
import java.util.concurrent.CountDownLatch;
@SpringBootApplication
@ImportResource({"classpath:dubbo.xml"})
public class Application {
private static final Logger logger = Logger.getLogger(Application.class);
@Bean
public CountDownLatch closeLatch() {
return new CountDownLatch(1);
}
public static void main(String[] args) throws InterruptedException {
ApplicationContext ctx = SpringApplication.run(Application.class, args);
logger.info("项目启动!");
CountDownLatch closeLatch = ctx.getBean(CountDownLatch.class);
closeLatch.await();
}
}
@@ -0,0 +1,10 @@
package com.didispace.service;
/**
* Created by zhaiyc on 2016/7/14.
*/
public interface ComputeService {
Integer add(int a, int b);
}
@@ -0,0 +1,15 @@
package com.didispace.service.impl;
import com.didispace.service.ComputeService;
/**
* Created by zhaiyc on 2016/7/14.
*/
public class ComputeServiceImpl implements ComputeService {
@Override
public Integer add(int a, int b) {
return a + b;
}
}
@@ -0,0 +1,4 @@
#ZooKeeper
dubbo.registry.address=localhost:2181
logging.level.root=DEBUG
@@ -0,0 +1,24 @@
<?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="compute-service" />
<!-- 注册中心服务地址 -->
<dubbo:registry id="zookeeper" protocol="zookeeper" address="${dubbo.registry.address}" />
<!-- 用dubbo协议在30001 -->
<dubbo:protocol name="dubbo" port="30001" dispather="all" threadpool="cached" threads="5000"/>
<!-- 声明需要暴露的服务接口 -->
<dubbo:service interface="com.didispace.service.ComputeService" ref="computeService"
version="1.0" registry="zookeeper" owner="shp"/>
<!-- 具体服务接口的实现 -->
<bean id="computeService" class="com.didispace.service.impl.ComputeServiceImpl" />
</beans>
@@ -4,11 +4,11 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.didispace</groupId>
<artifactId>Chapter9-2-1</artifactId>
<artifactId>consumer</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>
<name>Chapter9-2-1</name>
<name>consumer</name>
<description>Spring Boot project</description>
<parent>
@@ -29,21 +29,21 @@
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.4.9</version>
<version>2.5.3</version>
<exclusions>
<exclusion>
<artifactId>spring</artifactId>
@@ -52,6 +52,28 @@
</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>
<build>
@@ -1,14 +1,19 @@
package com.didispace;
import org.apache.log4j.Logger;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ImportResource;
import java.util.concurrent.CountDownLatch;
@SpringBootApplication
@ImportResource({"classpath:dubbo.xml"})
public class Application {
public static void main(String[] args) {
public static void main(String[] args) throws InterruptedException {
SpringApplication.run(Application.class, args);
}
@@ -0,0 +1,10 @@
package com.didispace.service;
/**
* Created by zhaiyc on 2016/7/14.
*/
public interface ComputeService {
Integer add(int a, int b);
}
@@ -0,0 +1,24 @@
package com.didispace.web;
import com.didispace.service.ComputeService;
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.RequestParam;
import org.springframework.web.bind.annotation.RestController;
/**
* Created by zhaiyc on 2016/7/14.
*/
@RestController
public class ComputeController {
@Autowired
private ComputeService computeService;
@RequestMapping(value = "/add", method = RequestMethod.GET)
public String add(@RequestParam Integer a, @RequestParam Integer b) {
return String.valueOf(a + b);
}
}
@@ -0,0 +1,5 @@
#ZooKeeper
dubbo.registry.address=localhost:2181
logging.level.root=DEBUG
@@ -0,0 +1,18 @@
<?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" />
<!-- 注册中心服务地址 -->
<dubbo:registry id="zookeeper" protocol="zookeeper" address="${dubbo.registry.address}" />
<!-- 引用ComputeService服务-->
<dubbo:reference id="computeService" interface="com.didispace.service.ComputeService"
check="false" version="1.0" url="" registry="zookeeper" protocol="dubbo" timeout="15000"/>
</beans>
@@ -0,0 +1,24 @@
package com.didispace;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)
@WebAppConfiguration
public class ApplicationTests {
@Before
public void setUp() throws Exception {
}
@Test
public void getHello() throws Exception {
}
}
-23
View File
@@ -1,23 +0,0 @@
<?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="hello-world-app" />
<!-- 使用multicast广播注册中心暴露服务地址 -->
<dubbo:registry address="multicast://224.5.6.7:1234" />
<!-- 用dubbo协议在20880端口暴露服务 -->
<dubbo:protocol name="dubbo" port="20880" />
<!-- 声明需要暴露的服务接口 -->
<dubbo:service interface="com.alibaba.dubbo.demo.DemoService" ref="demoService" />
<!-- 和本地bean一样实现服务 -->
<bean id="demoService" class="com.alibaba.dubbo.demo.provider.DemoServiceImpl" />
</beans>
@@ -1,40 +0,0 @@
package com.didispace;
import com.didispace.web.HelloController;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.http.MediaType;
import org.springframework.mock.web.MockServletContext;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import static org.hamcrest.Matchers.equalTo;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = MockServletContext.class)
@WebAppConfiguration
public class ApplicationTests {
private MockMvc mvc;
@Before
public void setUp() throws Exception {
mvc = MockMvcBuilders.standaloneSetup(new HelloController()).build();
}
@Test
public void getHello() throws Exception {
mvc.perform(MockMvcRequestBuilders.get("/hello").accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(content().string(equalTo("Hello World")));
}
}