spring boot 集成 dubbo starter 实现多注册中心配置(yaml配置)
spring boot 集成dubbo后yml配置如何写呢,下面就跟我一起看看。三个module:spring-boot-dubbo-sharespring-boot-dubbo-providerspring-boot-dubbo-consumer首先在spring-boot-dubbo-share模块中定义个一个接口【强调】:1.zookeeper的地址,替换成你...
spring boot 集成 dubbo后yml配置如何写呢,下面就跟我一起看看。
三个module:
- spring-boot-dubbo-share
- spring-boot-dubbo-provider
- spring-boot-dubbo-consumer
首先在spring-boot-dubbo-share模块中定义个一个接口
【强调】:
1.zookeeper的地址,替换成你们自己的,如果没有则写 N/A
2.share包需要被provider和consumer模块引入
3.消费者如果写了@Reference(url = "dubbo://localhost:28080"),那么会忽略注册中心的地址,走直连
package com.share.service;
/**
* @Author: zj
* @Date: 2019-09-23
* @Version 1.0
*/
public interface IDubboPrintService {
String print(String string);
}
完成之后,记得maven点击clean->package->install,方便后面的provder和consumer模块引用。
然后在spring-boot-dubbo-provider提供服务
首先pom.xml配置如下(注意:把第一步中的share模块引入进去),我这里没写出来,毕竟你的jar包名跟我的不一样
......
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<java.version>1.8</java.version>
<dubbo.version>2.7.1</dubbo.version>
</properties>
<dependencies>
...
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>${dubbo.version}</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
<version>${dubbo.version}</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-dependencies-zookeeper</artifactId>
<version>${dubbo.version}</version>
<type>pom</type>
</dependency>
...
...
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
yaml文件配置:
server:
port: 8080
servlet:
context-path: /
# -------------------- Dubbo 配置 BEGIN --------------------
dubbo:
# registry: # 单注册中心,注意属性级别:dubbo.registry,注意多注册中心格式:@Service(registry = {"registry1","registry2","registry3"})
# register: false # true(默认), false:表示服务不注册到注册中心(只订阅,直连服务正常),且dubbo-admin不显示
# address: zookeeper://127.0.0.1:2181
registries: # 多注册中心,注意属性级别:dubbo.registries, 不同的方法可以用@Service(registry = {"registry2"}),指定不同的注册中心
provider1:
register: false #默认true, false:表示服务不注册到注册中心
address: zookeeper://55.55.55.82:2181
provider2:
register: false
address: zookeeper://55.55.55.196:2181
provider3:
register: false
address: zookeeper://55.55.55.139:2181
application:
name: myProvider
# qos-enable: true # 默认值 true, 是否允许在线运维, 配置和查询服务
# qos-port: 22222 # 默认值2222
# qos-accept-foreign-ip: false # 默认false , 不允许远程访问 telnet
scan:
base-packages: com.privder.serivce
protocol:
name: dubbo
port: 28080
provider:
retries: 0 # 服务提供者无需重试
timeout: 6000 # 默认只有1s
# -------------------- Dubbo 配置 END --------------------
目录结构
注意:开启EnableDubbo这个注解
package com.privder;
import com.share.util.ServletContextUtil;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.annotation.Import;
@SpringBootApplication
@EnableDubbo
@Import(ServletContextUtil.class)
public class DubboProviderApplication extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(DubboProviderApplication.class, args);
}
}
package com.privder.serivce.impl;//package com.cmb.service.dubbo.provider;
import com.share.service.IDubboPrintService;
import org.apache.dubbo.config.annotation.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
// registry指定注册中心,默认是全部
// @Service(registry = {"provider1","provider3"})
@Service(group = "project-dubbo-provider", version = "1.0.0", registry = {"provider1", "provider2", "provider3"})
@org.springframework.stereotype.Service("DP.PrintService1")
public class IDubboPrintServiceImpl implements IDubboPrintService {
private static final Logger logger = LoggerFactory.getLogger(IDubboPrintServiceImpl.class);
@Override
public String print(String str) {
logger.info("{}-{}-{}:{}", "project1", "1.0.0", "provider1 provider2 provider3", str);
return "provider1+provider2+provider3";
}
}
接下来完成spring-boot-dubbo-consumer模块
pom.xml跟provder类似,记得把dubbo-starter依赖加进去
消费者的yaml配置如下:
server:
port: 8081
servlet:
context-path: /
# -------------------- Dubbo 配置 BEGIN --------------------
dubbo:
# registry: # 单注册中心,注意属性级别:dubbo.registry
# register: false # true(默认), false:表示服务不注册到注册中心(只订阅,直连服务正常),且dubbo-admin不显示
# address: zookeeper://127.0.0.1:2181
registries: # 多注册中心
# consumer1:
# register: true #默认,false:表示服务不注册到注册中心
# address: N/A
# consumer2:
# address: N/A
# consumer3:
# address: N/A
consumer1:
register: false #表示服务不注册到注册中心(此时下面的地址不会使用,因为comsume走直连)
address: zookeeper://55.55.55.82:2181
consumer2:
register: false #false:表示服务不注册到注册中心(此时下面的地址不会使用,因为comsume走直连)
address: zookeeper://55.55.55.196:2181
consumer3:
register: false #false:表示服务不注册到注册中心(此时下面的地址不会使用,因为comsume走直连)
address: zookeeper://55.55.55.139:2181
application:
name: myConsumer
scan:
base-packages: com.consumer.service
consumer:
timeout: 3000
check: false # 默认true.服务启动时候检查是否可用,服务不可用时无法启动项目, false则不检查
retries: 2 # 服务重试次数
# -------------------- Dubbo 配置 END --------------------
目录结构
package com.consumer;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
@SpringBootApplication
@EnableDubbo
public class DubboConsumerApplication extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(DubboConsumerApplication.class, args);
}
}
package com.consumer.service.impl;//package com.cmb.service.consumer;
import com.share.service.IDubboPrintService;
import org.apache.dubbo.config.annotation.Reference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @Author: z信息
* @Date: 2019-11-08
* @Version 1.0
*/
@org.springframework.stereotype.Service
public class DubboPrintConsumer {
private static final Logger logger = LoggerFactory.getLogger(DubboPrintConsumer.class);
// project1 与provider中的group名保持一致
// @Reference(group = "project1", version = "1.0.0",url = "127.0.0.1:2181")
// consumer 相当于xml中的 reference id
// 如果不是直连,去掉 url = "dubbo://localhost:28080"
@Reference(url = "dubbo://localhost:28080", group = "project-dubbo-provider", version = "1.0.0", registry = {"consumer1", "consumer2", "consumer3"})
private IDubboPrintService printService1;
public IDubboPrintService getPrintService1() {
return printService1;
}
}
package com.consumer.controller;
import com.consumer.service.impl.DubboPrintConsumer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @Date: 2019-08-29
* @Version 1.0
*/
@RestController
public class Controller {
private static final Logger logger = LoggerFactory.getLogger(Controller.class);
@Autowired
private DubboPrintConsumer dubboPrintConsumer;
@GetMapping(value = "/dubboPrint", produces = "application/json; charset=UTF-8")
public String dubboPrint() {
String fromProvider= dubboPrintConsumer.getPrintService1().print("consumer1,consumer3");
logger.info("Call Provider Success: {}", fromProvider);
return "success";
}
}
配置完成后,启动应用,访问:http://localhost:8081/dubboPrint
至此基本功能介绍完了。
顺便说一句:在我参与的大型app后台项目中,实际上这种dubbo-starter的yaml配置写法其实不常用。
一般还是用【xml】dubbo-provider.xml和dubbo-comsumer.xml来配置。
原因:简单直接明了
下面分享一下我在实际工作中遇到的坑:
- 同时配置了 XML 和 properties 文件,那么 properties 中的配置是无效的,只有 XML 没有配置时,properties 才生效。
- 实际应用的时候,有些dubbo服务提供者本地是无法使用的,但是无法启动,那么将 dubbo check 属性改 false(默认是true)
- dubbo:registry 标签的 register 属性设置为 false(默认是true)作用:有时候开发环境就一个注册中心,本地开发人员都注册服务,可能导致服务无法正常运行。当 register: false 的时候,就表示服务只订阅,不注册; 说简单点就是服务是以直连的方式调用,绕过注册中心
- 如果dubbo调用提示消息传递失败,一般都是你的参数没有实现序列化
更多推荐
所有评论(0)