1. dubbo-springboot入门级demo

1.1. 前言

  1. 最后一个做运维的朋友和我提起,他们公司想做个dubbo灰度发布的功能,而这个功能落到了他头上。在我的印象里,dubbo应该可以通过扩展代码来实现特定用户路由到特定服务器,来实现这方面的灰度功能,但完全由运维去做,可能就需要脚本了吧,dubbo是否支持脚本我还不清楚,趁着这个进一步熟悉dubbo的过程,我来系统的学一下dubbo的基本功能,由于现在基本都用springboot来开发了,所以直接从springboot入门级dubbo应用开始

1.2. 生产者

1.2.1. 服务方法和接口

public interface GreetingService {
String sayHello(String name);
}
import com.tzxylao.dubbo.service.GreetingService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Value; @Service(version = "1.0.0")
public class GreetingServiceImpl implements GreetingService { @Value("${dubbo.application.name}")
private String serviceName;
@Value("${dubbo.protocol.port:0}")
private int port; @Override
public String sayHello(String name) {
return String.format("[%s][%s] : Hello, %s", serviceName,port, name);
}
}

1.2.2. 配置文件application.properties

server.port=8082

dubbo.application.name=first-dubbo-provider
dubbo.scan.base-packages=com.tzxylao.dubbo.service dubbo.protocol.name=dubbo
dubbo.protocol.port=20884
dubbo.registry.address=zookeeper://127.0.0.1:2181

1.2.3. pom

<properties>
<java.version>1.8</java.version>
<spring-boot.version>2.1.1.RELEASE</spring-boot.version>
<dubbo.version>2.7.1</dubbo.version>
<curator.version>2.8.0</curator.version>
<zookeeper.version>3.4.6</zookeeper.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> <dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency> <!-- Dubbo Spring Boot Starter -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.7.1</version>
</dependency> <dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
</dependency> <!-- zookeeper -->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>${zookeeper.version}</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
<exclusion>
<groupId>io.netty</groupId>
<artifactId>netty</artifactId>
</exclusion>
</exclusions>
</dependency> <dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>${curator.version}</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>${curator.version}</version>
</dependency>
</dependencies>
<repositories>
<repository>
<id>apache.snapshots.https</id>
<name>Apache Development Snapshot Repository</name>
<url>https://repository.apache.org/content/repositories/snapshots</url>
<releases>
<enabled>false</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>

1.2.4. 启动方法

@EnableAutoConfiguration
public class DubboApplication { public static void main(String[] args) {
SpringApplication.run(DubboApplication.class, args);
} }

1.3. 消费者

1.3.1. 接口同上

1.3.2. pom同上

1.3.3. 配置文件

server.port=8081

spring.application.name=first-dubbo-consumer
dubbo.registry.address=zookeeper://127.0.0.1:2181

1.3.4. 启动方法

@EnableAutoConfiguration
public class DubboConsumerApplication { @Reference(version = "1.0.0")
private GreetingService greetingService; public static void main(String[] args) throws InterruptedException {
SpringApplication.run(DubboConsumerApplication.class, args);
} @Bean
public ApplicationRunner runner(){
int count = 0;
while (true) {
System.out.println(greetingService.sayHello("World "));
try {
TimeUnit.SECONDS.sleep(3);
} catch (InterruptedException e) {
e.printStackTrace();
}
count++;
if (count == 100) {
break;
}
}
return s -> {
System.out.println(greetingService.sayHello("World"));
};
} }

1.4. 运行结果

...
[first-dubbo-provider][20884] : Hello, World
[first-dubbo-provider][20884] : Hello, World
...

1.5. 总结

  1. 这里需要注意的几个核心点就是扫描包路劲别搞错
  2. @Refernece注解用的是dubbo包里的,@Service也是dubbo包里的
05-06 19:33