离职两个月,太久没写项目了,重新温故下。
这里的代码是最简单的演示效果。
项目下载地址 git cloen -b v1.0.0 https://github.com/z201/learning-spring-dubbo-micro-service.git
v1.0.0
分支dubbo xml 配置演示基于zk做注册中心。
v2.0.0
分支dubbo annotation 配置演示基于zk做注册中心。
v3.0.0
分支dubbo xml 配置演示基于nacos做注册中心。
项目模块
1 2 3 4 5 6
| <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>
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
| <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>
<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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| package cn.z201.io.api;
public interface DefaultDemoServiceI {
String sayHello(String name);
}
|
dubbo-micro-service-consumer
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
| 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;
@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;
@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); } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| server: port: 8001 compression: enabled: true min-response-size: 1
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
|
1 2 3 4 5 6 7 8 9
| <?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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
| package cn.z201.io.learning.rpc.micro.provider.service;
import cn.z201.io.api.DefaultDemoServiceI; import org.springframework.beans.factory.annotation.Value;
public class DefaultDemoServiceImpl implements DefaultDemoServiceI{
@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;
@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(); } catch (IOException e) { e.printStackTrace(); } } }
|
1 2 3 4 5 6
| spring: name: provider-spring-boot-starter devtools: restart: enabled: true
|
1 2 3 4 5 6 7 8 9 10
| <?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
。