springboot整合dubbo的配置以及简单实例

因为项目需求,所以需要用到远程的dubbo和zookeeper,之前没有接触过分布式,查了很多博客,代码copy过来各种报错,后来慢慢找了好久,总结了一个简单的demo,如果有不对的地方欢迎指正!感谢感谢感谢~
** 因为我的的项目是连接远程的zookeeper,所以我并没有在我本地配置,如果有需求,看一下这个链接
点我点我点我

接下来,开始我的表演
1. 生产者
① 先上一个生产者项目的目录

在这里插入图片描述

② pom.xml文件
<dependency>
   <groupId>com.alibaba.boot</groupId>
    <artifactId>dubbo-spring-boot-starter</artifactId>
    <version>0.2.0</version>
</dependency>

<dependency>
    <groupId>org.apache.zookeeper</groupId>
    <artifactId>zookeeper</artifactId>
    <version>3.4.6</version>
    <exclusions>
        <exclusion>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
        </exclusion>
        <exclusion>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
        </exclusion>
    </exclusions>
</dependency>
③ application.yml文件
#dubbo配置
dubbo:
  application: # 应用配置,配置当前应用信息,不管当前是提供者还是消费者.
    name: Provide # 注册在注册中心的名称,唯一标识
  registry: # 注册中心配置,用于配置连接注册中心相关信息
    address: zookeeper://192.168.1.87:2181
  protocol: # 暴露服务端口
     # 默认名称,勿更改name: dubbo
    port: 12398 # 暴露服务端口(默认为20880,修改端口,不同的服务提供者端口不能重复)
  scan:
    base-packages: com.dubboService  # 生产者的包

scan:base-packages: 该配置中写生产者暴露服务的包
protocal下的name,默认为dubbo,如果生产者内随意更改这个name值导致dubbo报错,如下:

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
16:36:41.705 ERROR org.springframework.boot.SpringApplication 858 reportFailure - Application run failed java.lang.IllegalStateException: No such extension com.alibaba.dubbo.rpc.Protocol by name dobbo
	at com.alibaba.dubbo.common.extension.ExtensionLoader.findException(ExtensionLoader.java:482) ~[dubbo-2.6.2.jar:2.6.2]
	at com.alibaba.dubbo.common.extension.ExtensionLoader.createExtension(ExtensionLoader.java:489) ~[dubbo-2.6.2.jar:2.6.2]
	at com.alibaba.dubbo.common.extension.ExtensionLoader.getExtension(ExtensionLoader.java:309) ~[dubbo-2.6.2.jar:2.6.2]
	at com.alibaba.dubbo.config.ServiceConfig.findConfigedPorts(ServiceConfig.java:641) ~[dubbo-2.6.2.jar:2.6.2]
	at com.alibaba.dubbo.config.ServiceConfig.doExportUrlsFor1Protocol(ServiceConfig.java:471) ~[dubbo-2.6.2.jar:2.6.2]
	at com.alibaba.dubbo.config.ServiceConfig.doExportUrls(ServiceConfig.java:358) ~[dubbo-2.6.2.jar:2.6.2]
	at com.alibaba.dubbo.config.ServiceConfig.doExport(ServiceConfig.java:317) ~[dubbo-2.6.2.jar:2.6.2]
	at com.alibaba.dubbo.config.ServiceConfig.export(ServiceConfig.java:216) ~[dubbo-2.6.2.jar:2.6.2]
	at com.alibaba.dubbo.config.spring.ServiceBean.onApplicationEvent(ServiceBean.java:123) ~[dubbo-2.6.2.jar:2.6.2]
	at com.alibaba.dubbo.config.spring.ServiceBean.onApplicationEvent(ServiceBean.java:49) ~[dubbo-2.6.2.jar:2.6.2]
	at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:172) ~[spring-context-5.1.2.RELEASE.jar:5.1.2.RELEASE]
	at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:165) ~[spring-context-5.1.2.RELEASE.jar:5.1.2.RELEASE]
	at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139) ~[spring-context-5.1.2.RELEASE.jar:5.1.2.RELEASE]
	at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:398) ~[spring-context-5.1.2.RELEASE.jar:5.1.2.RELEASE]
	at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:355) ~[spring-context-5.1.2.RELEASE.jar:5.1.2.RELEASE]
	at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:882) ~[spring-context-5.1.2.RELEASE.jar:5.1.2.RELEASE]
	at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.finishRefresh(ServletWebServerApplicationContext.java:161) ~[spring-boot-2.1.0.RELEASE.jar:2.1.0.RELEASE]
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:549) ~[spring-context-5.1.2.RELEASE.jar:5.1.2.RELEASE]
	at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:140) ~[spring-boot-2.1.0.RELEASE.jar:2.1.0.RELEASE]
	at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:775) [spring-boot-2.1.0.RELEASE.jar:2.1.0.RELEASE]
	at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) [spring-boot-2.1.0.RELEASE.jar:2.1.0.RELEASE]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:316) [spring-boot-2.1.0.RELEASE.jar:2.1.0.RELEASE]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1260) [spring-boot-2.1.0.RELEASE.jar:2.1.0.RELEASE]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1248) [spring-boot-2.1.0.RELEASE.jar:2.1.0.RELEASE]
	at com.miyu.mfmsserver.MfmsServerApplication.main(MfmsServerApplication.java:11) [classes/:?]
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_181]
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_181]
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_181]
	at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_181]
	at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) [spring-boot-devtools-2.1.0.RELEASE.jar:2.1.0.RELEASE]
④ 来一个超级简单的Service
package com.dubboService;

public interface ComputeService {
    Integer add(int a, int b);
}
⑤来一个实现类
package com.dubboService;

import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.stereotype.Component;

@Service
@Component
public class ComputeServiceImpl implements ComputeService {

    @Override
    public Integer add(int a, int b) {
        return a + b;
    }
}

注意@Service这个注解是 com.alibaba.dubbo.config.annotation.Service下的,不要引错了!!!

生产者配置完毕,可以启动一下项目,日志中出现以下,证明我们的dubbo配置文件是没有问题的:

16:40:24.212 INFO com.alibaba.boot.dubbo.context.event.OverrideDubboConfigApplicationListener 68 onApplicationEvent - Dubbo Config was overridden by externalized configuration {dubbo.application.name=Provide, dubbo.protocol.port=12398, dubbo.registry.address=zookeeper://192.168.1.87:2181, dubbo.scan.base-packages=com.dubboService}

连接成功的话,日志中出现以下,证明我们连接成功:
在这里插入图片描述

2. 消费者
① 来一个目录

在这里插入图片描述
因为有公司名,包名部分我打了马赛克hhhh,一定要注意!生产者和消费者的Service包名一定要相同!不然找不到!

② pom.xml文件

同生产者,一毛一样的,copy来即可…

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>

<!-- 以下的部分!! -->
    <!-- SpringBoot Web容器 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>com.alibaba.boot</groupId>
        <artifactId>dubbo-spring-boot-starter</artifactId>
        <version>0.2.0</version>
    </dependency>
    <dependency>
        <groupId>org.apache.zookeeper</groupId>
        <artifactId>zookeeper</artifactId>
        <version>3.4.6</version>
        <exclusions>
            <exclusion>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-log4j12</artifactId>
            </exclusion>
            <exclusion>
                <groupId>log4j</groupId>
                <artifactId>log4j</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
</dependencies>
③ yml配置文件
#dubbo配置
dubbo:
  application: # 应用配置,用于配置当前应用信息,不管当前应用是提供者还是消费者.
    name: Consumer # 注册在注册中心的名称,唯一标识
  registry: # 注册中心配置,用于配置连接注册中心相关信息
    address: zookeeper://192.168.1.87:2181
  protocol: # 暴露服务端口
    name: dubbi # 默认名称,勿更改
    port: 12398 # 暴露服务端口(默认为20880,修改端口,不同的服务提供者端口不能重复)

server:
  port: 8081

关于protocol的配置:在消费者中,name可以随便更改,port也不必与生产者一致,改完之后,我的项目依旧可以获取到生产者的service,应该是可以不配置的,如果后期查到相关的我会再更改

④ 建一个相同的包,写一个和生产中的service相同 的service
package com.dubboService;

public interface ComputeService {
    Integer add(int a, int b);
}
⑤ 来一个测试controller
package com.debbotest.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.dubboService.ComputeService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class dubboTestController {

    @Reference
    private ComputeService computeService;

    @GetMapping("/test")
    public void test(){
        Integer result = computeService.add(1,3);
        System.out.println("#####################################"+result+"###################################");
    }
}
3. 然后我们开始测试

在这里插入图片描述
控制台输出结果:
在这里插入图片描述
完成!!!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Spring Boot整合Dubbo配置步骤如下: 1. 引入DubboZookeeper的依赖 在pom.xml文件中添加DubboZookeeper的依赖: ``` <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>2..</version> </dependency> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4.14</version> </dependency> ``` 2. 配置Dubbo的注册中心 在application.properties文件中添加Dubbo的注册中心配置: ``` # Dubbo Registry dubbo.registry.address=zookeeper://127...1:2181 ``` 3. 配置Dubbo的服务提供者 在Spring Boot配置类中添加Dubbo的服务提供者配置: ``` @Configuration public class DubboProviderConfig { @Value("${dubbo.application.name}") private String applicationName; @Value("${dubbo.registry.address}") private String registryAddress; @Bean public ApplicationConfig applicationConfig() { ApplicationConfig applicationConfig = new ApplicationConfig(); applicationConfig.setName(applicationName); return applicationConfig; } @Bean public RegistryConfig registryConfig() { RegistryConfig registryConfig = new RegistryConfig(); registryConfig.setAddress(registryAddress); return registryConfig; } @Bean public ProtocolConfig protocolConfig() { ProtocolConfig protocolConfig = new ProtocolConfig(); protocolConfig.setName("dubbo"); protocolConfig.setPort(20880); return protocolConfig; } @Bean public ServiceConfig<GreetingService> greetingServiceConfig(GreetingService greetingService) { ServiceConfig<GreetingService> serviceConfig = new ServiceConfig<>(); serviceConfig.setInterface(GreetingService.class); serviceConfig.setRef(greetingService); serviceConfig.setVersion("1.."); serviceConfig.setRegistry(registryConfig()); serviceConfig.setProtocol(protocolConfig()); return serviceConfig; } } ``` 4. 配置Dubbo的服务消费者 在Spring Boot配置类中添加Dubbo的服务消费者配置: ``` @Configuration public class DubboConsumerConfig { @Value("${dubbo.registry.address}") private String registryAddress; @Bean public RegistryConfig registryConfig() { RegistryConfig registryConfig = new RegistryConfig(); registryConfig.setAddress(registryAddress); return registryConfig; } @Bean public ReferenceConfig<GreetingService> greetingServiceReferenceConfig() { ReferenceConfig<GreetingService> referenceConfig = new ReferenceConfig<>(); referenceConfig.setInterface(GreetingService.class); referenceConfig.setVersion("1.."); referenceConfig.setRegistry(registryConfig()); return referenceConfig; } } ``` 5. 调用Dubbo服务 在Spring Boot的Controller中注入Dubbo的服务消费者,并调用Dubbo服务: ``` @RestController public class GreetingController { @Autowired private GreetingService greetingService; @GetMapping("/greeting") public String greeting(@RequestParam String name) { return greetingService.greeting(name); } } ``` 以上就是Spring Boot整合Dubbo配置步骤。 ### 回答2: Spring Boot是一种非常流行的Java开发框架,Dubbo是阿里巴巴的分布式框架,用于提高服务的并发性和可扩展性。然而,将Spring BootDubbo结合起来使用有点棘手。下面将讨论在Spring Boot应用程序中整合Dubbo配置过程。 1.首先,需要添加DubboSpring Boot的依赖到pom.xml中。这里我将展示如何添加这些依赖。 ```xml <dependency> <groupId>com.alibaba.spring.boot</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>0.2.0</version> </dependency> ``` ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> ``` 上述代码中的"dubbo-spring-boot-starter" jar文件提供了在Spring Boot环境中自动配置和启动Dubbo框架的功能,"spring-boot-starter-web" jar文件是Spring Boot开发常用的Web框架。 2.接下来,我们需要在Spring Boot应用程序的配置文件中添加Dubbo的相关配置。 ```properties # Dubbo application name spring.application.name=dubbo-consumer # Dubbo Registry address spring.dubbo.registry.address=zookeeper://localhost:2181 # Dubbo scan packages spring.dubbo.scan.base-packages=com.example.service # Dubbo Consumer timeout spring.dubbo.consumer.timeout=6000 # Dubbo Provider timeout spring.dubbo.provider.timeout=6000 # Dubbo Consumer retries spring.dubbo.consumer.retries=0 # Dubbo Provider retries spring.dubbo.provider.retries=0 ``` 在上述配置文件中,“spring.application.name”属性是应用程序的名称,用于在Dubbo注册中心上标识该服务。"spring.dubbo.registry.address"属性指定Dubbo注册中心的地址,Dubbo框架将使用该地址向注册中心注册和发现服务。"spring.dubbo.scan.base-packages"属性指定Dubbo扫描的服务包,所有符合条件的服务将被Dubbo管理。此外,其他Dubbo属性,如超时,重试次数等,也可以在此文件中配置。 3.接下来,我们需要向Spring Boot应用程序中添加Dubbo服务。我们可以通过以下步骤: ```java @Service public class UserServiceImpl implements UserService { @Override public UserDto getUserById(Long id) { UserDto userDto = new UserDto(); // do something return userDto; } } ``` 可以看到,上述代码中,我们使用"@Service"注解来标记服务实现类。我这里使用了一个简单的UserService接口和它的实现类UserServiceImpl,你可以根据自己的需求修改它们。 4.最后,我们创建一个Dubbo Consumer来调用这个服务: ```java @RestController public class UserController { @Reference UserService userService; @GetMapping("/users/{id}") public UserDto getUserById(@PathVariable Long id) { return userService.getUserById(id); } } ``` 上述代码中,我们使用"@Reference"注解将Dubbo服务注入到UserController中。我们可以使用@Reference注解在Spring Boot中编写Dubbo Consumer,该注解负责创建Dubbo服务的代理对象,从而简化应用程序的编写过程。 综上所述,将Spring BootDubbo结合使用的配置步骤如下:添加DubboSpring Boot的依赖,配置Dubbo属性,添加Dubbo服务以及创建Dubbo Consumer。这些步骤将帮助我们在Spring Boot应用程序中使用Dubbo框架,提高应用程序的性能和可扩展性。 ### 回答3: Spring Boot 是一种快速开发框架,适用于构建独立的,生产级别的应用程序,并且 Dubbo 是一种高性能,分布式服务框架,可以方便地实现RPC服务的注册,发现和调用。在开发现代化架构的企业级应用程序时,很常见使用Spring BootDubbo 框架一起工作。 Spring Boot整合Dubbo,需要进行以下步骤: 1. 添加Dubbo依赖 在pom.xml中引入Dubbo的依赖,这些依赖包括Dubbo的核心模块dubbo,以及ZooKeeper的客户端依赖curator。 2. 添加Dubbo配置 在application.properties文件中添加Dubbo配置。这些配置包括Dubbo应用程序的名称,协议,注册中心地址以及服务端口号等。 3. 配置Dubbo注册中心 配置Dubbo注册中心,我们可以使用ZooKeeper 或者 Redis。通常情况下,我们使用ZooKeeper作为Dubbo的注册中心。在Dubbo配置文件中指定zookeeper注册地址,它可以是一个单一的zookeer服务器,也可以是一个zookeeper集群。 4. 实现Dubbo服务 在实现Dubbo服务之前,我们需要定义一个接口供消费者调用。在该接口上使用Dubbo的@Service注解。使用Dubbo的@Service注解后,要使用Dubbo的@Service注解,需要使用Dubbo的@Reference注解。使用@Reference注解后,您就可以使用Dubbo的服务了。 总的来说,整合Spring BootDubbo是比较简单的,只需要按照上述步骤进行相应的配置即可。这样就能使用Dubbo框架来快速构建高性能,分布式服务的系统。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值