搭建Apache Dubbo工程
2021-03-08 00:30
标签:prot int ram hose evel user active 插入 group 此项目是Spring Cloud Alibaba 和 Dubbo 整合 Demo JDK 1.8 Dubbo 2.7.2 Nacos Client 1.0.0 Spring Boot 2.1.6.RELEASE Dubbo Registry Nacos 2.6.6 Dubbo Spring Boot Starter 2.7.1 Spring Cloud Greenwich.SR2 Spring Cloud Alibaba 2.1.0.RELEASE Alibaba Spring Context Support 1.0.2 Spring Cloud Alibaba Nacos Config 2.1.0.RELEASE 此工程只负责管理模块 此工程只负责管理依赖 服务提供者 服务消费者 注: https://github.com/baihaibo1024/apache-dubbo-demo 搭建Apache Dubbo工程 标签:prot int ram hose evel user active 插入 group 原文地址:https://www.cnblogs.com/cnbai/p/12879804.html版本
Apache Dubbo Demo
创建父工程 apache-dubbo-demo
一、项目结构
二、pom.xml
统一依赖管理 apache-dubbo-dependencies
一、项目结构
二、pom.xml
服务提供者 apache-dubbo-provider
一、项目结构
二、apache-dubbo-provider/pom.xml
三、apache-dubbo-provider-api/pom.xml
四、EchoService
package com.cnbai.apache.dubbo.provider.api;
/**
* 测试动态化配置
*
* @author bai
* @since 2020/5/12
*/
public interface EchoService {
/**
* 测试 nacos 动态化配置文件
*
* @param string string
* @return {@link String}
* @author bai
* @since 2020/5/12
*/
String echo(String string);
}
五、apache-dubbo-provider-service/pom.xml
六、EchoServiceImpl
package com.cnbai.apache.dubbo.provider.service;
import com.cnbai.apache.dubbo.provider.api.EchoService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Value;
/**
* 测试负载均衡,动态化配置
*
* @author bai
* @since 2020/5/12
*/
@Service(version = "1.0.0")
public class EchoServiceImpl implements EchoService {
/**
* 测试负载均衡
*/
@Value("${dubbo.protocol.port}")
private String port;
/**
* 测试负载均衡,动态化配置
*/
@Override
public String echo(String string) {
return "Echo Hello Dubbo " + string + " i am from port: " + port;
}
}
七、ProviderApplication
package com.cnbai.apache.dubbo.provider;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* 服务提供者启动类
*
* @author bai
* @since 2020/5/12
*/
@SpringBootApplication
public class ProviderApplication {
public static void main(String[] args) {
SpringApplication.run(ProviderApplication.class, args);
}
}
八、application.yml
spring:
application:
name: dubbo-provider
# 定义两个 id 相同的 bean 起冲突时,以后定义的为主
main:
allow-bean-definition-overriding: true
dubbo:
scan:
base-packages: com.cnbai.apache.dubbo.provider.service
protocol:
name: dubbo
# 自动分配端口号 20880
# port: -1
port: 20881
serialization: kryo
registry:
address: nacos://192.168.205.130:8848
# 负载均衡策略 random 随机(默认),roundrobin 轮循,leastactive 最少活跃调用数,consistenthash 一致性 Hash
provider:
loadbalance: roundrobin
服务消费者 apache-dubbo-consumer
一、项目结构
二、pom.xml
三、EchoController
package com.cnbai.apache.dubbo.consumer.controller;
import com.cnbai.apache.dubbo.provider.api.EchoService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
/**
* 测试负载均衡,动态化配置
*
* @author bai
* @since 2020/5/12
*/
@RefreshScope
@RestController
public class EchoController {
@Reference(version = "1.0.0")
private EchoService echoService;
/**
* 测试动态化配置
*/
@Value("${user.name}")
private String username;
/**
* 测试 nacos 动态化配置
*
* @param string string
* @author cnbai
* @return {@link String}
* @since 2020/5/12
*/
@GetMapping(value = "/echo/{string}")
public String echo(@PathVariable String string) {
return echoService.echo(string) + " " + username;
}
}
四、ConsumerApplication
package com.cnbai.apache.dubbo.consumer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* 服务消费者启动类
*
* @author bai
* @since 2020/5/12
*/
@SpringBootApplication
public class ConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class, args);
}
}
五、bootstrap.properties
##########################################################################
#
# Spring Boot 配置文件的加载顺序:
# bootstrap.properties
# > bootstrap.yml
# > application.properties
# > application.yml
#
# 注:bootstrap.properties 配置为最高优先级一定会加载
#
##########################################################################
spring.application.name=dubbo-consumer-config
spring.cloud.nacos.config.server-addr=192.168.205.130:8848
spring.cloud.nacos.config.file-extension=yaml
六、dubbo-consumer-config.yaml
yml文件都写在Nacos配置管理中,Nacos中的配置不能写注释,要删掉
yml文件都写在Nacos配置管理中,Nacos中的配置不能写注释,要删掉
yml文件都写在Nacos配置管理中,Nacos中的配置不能写注释,要删掉spring:
application:
name: dubbo-consumer
main:
allow-bean-definition-overriding: true
dubbo:
scan:
base-packages: com.cnbai.apache.dubbo.consumer.controller
protocol:
name: dubbo
port: -1
serialization: kryo
registry:
address: nacos://192.168.205.130:8848
server:
port: 8080
# Dubbo 端口检查
endpoints:
dubbo:
enabled: true
# Dubbo 健康检查
management:
health:
dubbo:
status:
defaults: memory
extras: threadpool
# web 健康检查
endpoints:
web:
exposure:
include: "*"
# 测试 Nacos 动态化配置
user:
name: "只狼"
# name: "条狗"
代码