0
点赞
收藏
分享

微信扫一扫

面试官从这些方面考察你的Android开发水平!

北冥有一鲲 2023-05-11 阅读 107

生成过程视频: 如何使用ChatGPT快速生成代码 (qq.com)

1、Dubbo最新版本有哪些新特性

Dubbo最新版本是2.7.9,于2021年6月发布。以下是该版本的一些新特性:

总的来说,Dubbo 2.7.9 在多语言支持、服务网格、协议支持、IDL、性能优化等方面都有不少新特性。

2、Dubbo执行流程图

                       ┌───────────────┐     
                       │  Dubbo 服务消费者 │     
                       └───────────────┘     
                                 │             
                                 │             
                                 ▼             
                       ┌───────────────┐     
                       │     代理类        │     
                       └───────────────┘     
                                 │             
                                 │             
                                 ▼             
                       ┌───────────────┐     
                       │     过滤器链     │     
                       └───────────────┘     
                                 │             
                                 │             
                                 ▼             
                       ┌───────────────┐     
                       │      集群路由      │     
                       └───────────────┘     
                                 │             
                                 │             
                                 ▼             
                       ┌───────────────┐     
                       │   负载均衡选择器   │     
                       └───────────────┘     
                                 │             
                                 │             
                                 ▼             
                       ┌───────────────┐     
                       │     传输层         │     
                       └───────────────┘     
                                 │             
                                 │             
                                 ▼             
                       ┌───────────────┐     
                       │    协议层         │     
                       └───────────────┘     
                                 │             
                                 │             
                                 ▼             
                       ┌───────────────┐     
                       │  服务提供者    │     
                       └───────────────┘

具体的流程如下:

1)服务提供者启动时,会向Dubbo注册中心注册自己提供的服务以及服务的地址信息。

2)服务消费者启动时,会向Dubbo注册中心订阅需要消费的服务以及服务地址信息。

3)服务消费者通过Dubbo代理类调用Dubbo服务,Dubbo代理类会在本地缓存中查找可用的Dubbo服务提供者。

4)如果本地缓存中没有可用的Dubbo服务提供者,Dubbo代理类会向Dubbo注册中心查询可用的Dubbo服务提供者。

5)Dubbo注册中心返回可用的Dubbo服务提供者地址信息,Dubbo代理类根据负载均衡策略选择一个Dubbo服务提供者。

6)Dubbo代理类将请求发送到Dubbo服务提供者。

7)Dubbo服务提供者接收到请求后,根据请求的服务名、方法名等信息,调用相应的Java方法,得到结果。

8)Dubbo服务提供者将结果返回给Dubbo代理类。

9)Dubbo代理类将结果返回给服务消费者。

3、SpringBoot集成Dubbo的完整实例

第一步:在pom.xml中添加Dubbo和SpringBoot的依赖。在dependencies中添加以下内容:

<dependencies>
    <!-- Dubbo -->
    <dependency>
        <groupId>org.apache.dubbo</groupId>
        <artifactId>dubbo-spring-boot-starter</artifactId>
        <version>2.7.9</version>
    </dependency>
    <!-- SpringBoot -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <version>2.5.0</version>
    </dependency>
</dependencies>

第二步:在application.properties中添加Dubbo的配置。

#Dubbo
dubbo.application.name=example-provider
dubbo.registry.address=zookeeper://127.0.0.1:2181
dubbo.protocol.name=dubbo
dubbo.protocol.port=20880

第三步:创建一个接口,定义我们的Dubbo服务。

public interface ExampleService {
    String sayHello(String name);
}

第四步:创建一个实现类,实现ExampleService接口。

@Service
public class ExampleServiceImpl implements ExampleService {
    @Override
    public String sayHello(String name) {
        return "Hello " + name;
    }
}

第五步:在Dubbo配置类中,将ExampleServiceImpl暴露为Dubbo服务。

@Configuration
public class DubboConfig {
    @Bean
    public ApplicationConfig applicationConfig() {
        ApplicationConfig applicationConfig = new ApplicationConfig();
        applicationConfig.setName("example-provider");
        return applicationConfig;
    }

    @Bean
    public RegistryConfig registryConfig() {
        RegistryConfig registryConfig = new RegistryConfig();
        registryConfig.setAddress("zookeeper://127.0.0.1:2181");
        return registryConfig;
    }

    @Bean
    public ProtocolConfig protocolConfig() {
        ProtocolConfig protocolConfig = new ProtocolConfig();
        protocolConfig.setName("dubbo");
        protocolConfig.setPort(20880);
        return protocolConfig;
    }

    @Bean
    public ServiceConfig<ExampleService> serviceConfig(ExampleService exampleService) {
        ServiceConfig<ExampleService> serviceConfig = new ServiceConfig<>();
        serviceConfig.setInterface(ExampleService.class);
        serviceConfig.setRef(exampleService);
        serviceConfig.setVersion("1.0.0");
        serviceConfig.setTimeout(5000);
        return serviceConfig;
    }
}

第六步:创建一个RestController,将Dubbo服务注入进去。

@RestController
public class ExampleController {
    @Reference(version = "1.0.0")
    private ExampleService exampleService;

    @GetMapping("/hello/{name}")
    public String hello(@PathVariable("name") String name) {
        return exampleService.sayHello(name);
    }
}

第七步:启动SpringBoot应用程序,运行Dubbo服务提供者。

第八步:测试Dubbo服务。使用curl或Postman等工具访问http://localhost:8080/hello/yourname ,看到返回的字符串是"Hello yourname"。

举报

相关推荐

0 条评论