离职两个月,太久没写项目了,重新温故下。
这里的代码是最简单的演示效果。
项目下载地址
git cloen -b v1.0.0 https://github.com/z201/learning-spring-dubbo-micro-service.gitv1.0.0分支dubbo xml 配置演示基于zk做注册中心。v2.0.0分支dubbo annotation 配置演示基于zk做注册中心。v3.0.0分支dubbo xml 配置演示基于nacos做注册中心。
项目模块
- 现在演示的
v1.0.0的代码
<modules>
<module>dubbo-micro-service-consumer</module> // 消费者
<module>dubbo-micro-service-provider</module> // 生产者
<module>dubbo-micro-service-zookeeper</module> // 注册中心
<module>dubbo-micro-service-api</module> // 公用api
</modules>
- 相关依赖处理,主要是处理一个log输出的问题
<!-- 排除掉 zkclient 、Zookeeper、 dubbo log4j 使用slf4j -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>log4j-over-slf4j</artifactId>
<version>1.7.25</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.6.0</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</exclusion>
<exclusion>
<groupId>org.jboss.netty</groupId>
<artifactId>netty</artifactId>
</exclusion>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.2</version>
<exclusions>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- Zookeeper -->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.13</version>
<exclusions>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
</exclusions>
</dependency>
dubbo-micro-service-api
package cn.z201.io.api;
/**
* Demo Service interface
*
* @author zengqingfeng
* @since 2.7.0
*/
public interface DefaultDemoServiceI {
/**
* 测试
* @param name
* @return
*/
String sayHello(String name);
}
dubbo-micro-service-consumer
package cn.z201.io.learning.rpc.micro.consumer;
import cn.z201.io.api.DefaultDemoServiceI;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author z201.coding@gmail.com
**/
@RestController
@Slf4j
public class ApiController {
@Autowired
private DefaultDemoServiceI defaultDemoService;
@RequestMapping(value = "")
public String runner() {
return defaultDemoService.sayHello("consumer");
}
}
package cn.z201.io.learning.rpc.micro.consumer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;
/**
* @author z201.coding@gmail.com
**/
@SpringBootApplication
@Slf4j
@ImportResource(value = {
"classpath:dubbo-consumer.xml"
})
public class AppConsumerMain {
public static void main(String[] args) {
System.setProperty("dubbo.application.logger","slf4j");
SpringApplication.run(AppConsumerMain.class, args);
}
}
- 配置文件
server:
port: 8001
compression:
enabled: true
min-response-size: 1
# undertow
undertow:
worker-threads: 20
buffer-size: 512
io-threads: 2
access-log:
enabled: true
dir: target/logs
pattern: combined
spring:
name: consumer-spring-boot-starter
devtools:
restart:
enabled: true
<?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-spring-boot-starter"/>
<dubbo:consumer timeout="5000" />
<dubbo:registry address="zookeeper://127.0.0.1:2181" check="false"/>
<dubbo:reference id="defaultDemoServiceI" interface="cn.z201.io.api.DefaultDemoServiceI" check="false"/>
</beans>
dubbo-micro-service-provider
package cn.z201.io.learning.rpc.micro.provider.service;
import cn.z201.io.api.DefaultDemoServiceI;
import org.springframework.beans.factory.annotation.Value;
/**
* Default {@link DefaultDemoServiceImpl}
*
* @author zengqingfeng
* @see DefaultDemoServiceImpl
* @since 2.7.0
*/
public class DefaultDemoServiceImpl implements DefaultDemoServiceI{
/**
* The default value of ${dubbo.application.name} is ${spring.application.name}
*/
@Value("${spring.name}")
private String serviceName;
@Override
public String sayHello(String name) {
return String.format("[%s] : Hello, %s", serviceName, name);
}
}
package cn.z201.io.learning.rpc.micro.provider;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;
import java.io.IOException;
/**
* @author z201.coding@gmail.com
**/
@SpringBootApplication
@ImportResource(value = {
"classpath:dubbo-provider.xml"
})
@Slf4j
public class AppProviderMain {
public static void main(String[] args) {
System.setProperty("dubbo.application.logger","slf4j");
SpringApplication.run(AppProviderMain.class, args);
try {
System.in.read(); // 没有使用http容器启动,防止程序直接运行结束。
} catch (IOException e) {
e.printStackTrace();
}
}
}
- 配置文件
spring:
name: provider-spring-boot-starter
devtools:
restart:
enabled: true
<?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-spring-boot-starter"></dubbo:application>
<dubbo:registry address="zookeeper://127.0.0.1:2181"/>
<dubbo:protocol name="dubbo" port="20881"/>
<dubbo:service interface="cn.z201.io.api.DefaultDemoServiceI" ref="defaultDemoServiceImpl"/>
<bean id="defaultDemoServiceImpl" class="cn.z201.io.learning.rpc.micro.provider.service.DefaultDemoServiceImpl"/>
</beans>
dubbo-micro-service-zookeeper
这个就不贴代码了,单独跑个zk是一样的。只不过我吧dubbo官方的demo代码复制过来了。
效果

访问 http://127.0.0.1:8001/显示[dubbo-spring-boot-starter] : Hello, consumer。