0、docker 启动 zk
docker-compose.yml
version: '3'
services:
zoo1:
image: zookeeper
restart: always
hostname: zoo1
ports:
- 2181:2181
docker-compose up -d
1、创建父 pom
4.0.0
com.csp
dubbo-exam
pom
1.0-SNAPSHOT
dubbo-api
dubbo-provider
dubbo-consumer
com.101tec
zkclient
0.10
org.apache.zookeeper
zookeeper
3.4.13
pom
org.slf4j
slf4j-log4j12
org.apache.curator
curator-recipes
2.8.0
org.apache.dubbo
dubbo-spring-boot-starter
2.7.7
org.slf4j
slf4j-log4j12
org.apache.dubbo
dubbo
2.7.7
org.apache.curator
curator-framework
4.0.1
2、创建 API maven 工程
package com.csp.dubbo.demo;
public interface TestApiService {
String sayHello(String name);
}
3、创建 Provider maven 工程
pom.xml
dubbo-exam
com.csp
1.0-SNAPSHOT
4.0.0
dubbo-provider
com.csp
dubbo-api
1.0-SNAPSHOT
com.101tec
zkclient
org.apache.curator
curator-recipes
org.apache.dubbo
dubbo-spring-boot-starter
org.apache.dubbo
dubbo
org.apache.curator
curator-framework
dubbo-provider.properties
dubbo.application.name=dubbo-demo-annotation-provider
dubbo.protocol.name=dubbo
dubbo.protocol.port=20880
dubbo-provider.properties
###set log levels###
log4j.rootLogger=info, stdout
###output to the console###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=[%d{dd/MM/yy HH:mm:ss:SSS z}] %t %5p %c{2}: %m%n
Application
package com.csp.dubbo.demo.provider;
import java.io.IOException;
import org.apache.dubbo.config.RegistryConfig;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
public class Application {
public static void main(String[] args) throws IOException {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ProviderConfiguration.class);
context.start();
System.in.read();
}
@Configuration
@EnableDubbo(scanBasePackages = "com.csp.dubbo.demo.provider.impl")
@PropertySource("classpath:dubbo-provider.properties")
static class ProviderConfiguration {
@Bean
public RegistryConfig registryConfig() {
RegistryConfig registryConfig = new RegistryConfig();
registryConfig.setAddress("zookeeper://127.0.0.1:2181");
return registryConfig;
}
}
}
TestApiServiceImpl
package com.csp.dubbo.demo.provider.impl;
import com.csp.dubbo.demo.TestApiService;
import org.apache.dubbo.config.annotation.DubboService;
@DubboService(version = "1.0.0", interfaceClass = TestApiService.class)
public class TestApiServiceImpl implements TestApiService {
public String sayHello(String name) {
System.out.println("我是 provider, 我被调用了");
return name + " say hello";
}
}
4、创建 Provider maven 工程
pom.xml
dubbo-exam
com.csp
1.0-SNAPSHOT
4.0.0
dubbo-consumer
com.csp
dubbo-api
1.0-SNAPSHOT
com.101tec
zkclient
org.slf4j
slf4j-log4j12
org.apache.curator
curator-recipes
org.apache.dubbo
dubbo-spring-boot-starter
org.apache.dubbo
dubbo
org.apache.curator
curator-framework
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-parent
2.1.7.RELEASE
pom
import
application.properties
#spring项目名
spring.application.name=dubbo_auto_configuration_consumer_demo
#Dubbo provider configuration
dubbo.application.name=dubbo_consumer
dubbo.registry.protocol=zookeeper
dubbo.registry.address=zookeeper://127.0.0.1:2181
server.port=8082
log4j.properties
###set log levels###
log4j.rootLogger=info, stdout
###output to the console###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=[%d{dd/MM/yy HH:mm:ss:SSS z}] %t %5p %c{2}: %m%n
Application
package com.csp.dubbo.demo.consumer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class);
}
}
TestController
package com.csp.dubbo.demo.consumer.controller;
import com.csp.dubbo.demo.Test2Service;
import com.csp.dubbo.demo.TestApiService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestController {
@DubboReference(version = "1.0.0")
TestApiService testApiService;
@GetMapping("/sayHello")
public Object sayHello(String name) {
String hello = testApiService.sayHello(name);
return hello;
}
}
5、总结
- 整体思路是将 web 相关的 maven 依赖从父 pom 中抽离出来,单独放到 consumer 中
- dubbo 使用的日志的依赖是
slf4j-log4j12
,spring boot 默认使用的是logback-classic
。二者有冲突,在笔者项目中,选择去除掉了slf4j-log4j12