Spring Boot整合dubbo+Nacos实战(二)
前言
nacos的实战在前面一章已经介绍到
Spring Cloud Alibaba+Nacos的介绍与实战(一)
以及几种注册中心的区别介绍
几种常见的注册中心以及区别
新建父工程cloud-alibaba-demo
源码已经上传到gitee上
地址:https://gitee.com/culzb/cloud-alibaba-demo
配置文件pom.xml
管理子工程jar版本
<modules>
<module>gtw</module>
<module>demo-service</module>
<module>dubbo-demo-service</module>
</modules>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR1</spring-cloud.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>0.9.0.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>2.2.1.RELEASE</version>
<scope>test</scope>
</dependency>
<!-- nacos注册中心-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>0.2.1.RELEASE</version>
</dependency>
</dependencies>
</dependencyManagement>
新建子工程dubbo-demo-service
当前子工程相是被调用的服务,也就是说服务提供者。
在dubbo
中作为provider
。
然后引入相关jar包
配置pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.cuizb</groupId>
<artifactId>cloud-alibaba-demo</artifactId>
<version>1.0</version>
</parent>
<artifactId>dubbo-demo-service</artifactId>
<version>1.0</version>
<name>dubbo-demo-service</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- Dubbo依赖 -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.7.0</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
<version>2.7.0</version>
</dependency>
<!-- Nacos依赖 -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-registry-nacos</artifactId>
<version>2.7.1</version>
</dependency>
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
然后创建api接口,配置dubbo
扫描的包路径,会将指定扫描的包路径中的所有接口注册到nacos中。
-
注意
创建的api接口实现类上一定要加上@service
,引入包路径为:import org.apache.dubbo.config.annotation.Service;
否则在启动时控制台会出现
[DUBBO] No Spring Bean annotating Dubbo's @Service was found under package[com.cuizb.dubbo.demoservice.api]
我创建了一个api接口和一个api接口实现类
DubboDemoService
package com.cuizb.dubbo.demoservice.api;
/**
* @Author cuizb
* @Date 2022-03-19 18:09:31
* @Desc *
*/
public interface DubboDemoService {
String hello(String name);
}
DubboDemoServiceImpl
package com.cuizb.dubbo.demoservice.controller;
import com.cuizb.dubbo.demoservice.api.DubboDemoService;
import org.apache.dubbo.config.annotation.Service;
/**
* @Author cuizb
* @Date 2022-03-20 22:07:15
* @Desc *
*/
@Service
public class DubboDemoServiceImpl implements DubboDemoService {
@Override
public String hello(String name) {
return "hello " + name + "i am dubbo demoService!";
}
}
最后配置dubbo以及nacos相关信息
application.properties
server.port=8702
spring.application.name=dubbo-demo-service
# 配置服务信息
dubbo.application.name=dubbo-demo-service
## 禁用QOS同一台机器可能会有端口冲突现象
#dubbo.qos-enable=false
#dubbo.qos-accept-foreign-ip=False
# 配置注册中心
#spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
dubbo.registry.address=nacos://127.0.0.1:8848
# 设置协议-协议由提供方指定消费方被动接受
dubbo.protocol.name=dubbo
dubbo.protocol.port=7702
dubbo.scan.base-packages=com.cuizb.dubbo.demoservice.api
# 解决Bean重复定义问题
spring.main.allow-bean-definition-overriding=true
启动类加上@EnableDubbo
package com.cuizb.dubbo.demoservice;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@EnableDubbo //开启Dubbo的注解支持
//@EnableDiscoveryClient
@SpringBootApplication
public class DubboDemoServiceApplication {
public static void main(String[] args) {
SpringApplication.run(DubboDemoServiceApplication.class, args);
}
}
新建子工程gtw
我没有按照dubbo那样分为provider和consumer区分工程,我按照项目习惯将网关作为服务消费者,其他服务作为服务提供者。
由于我沿用了上一章节的网关工程,所以我会将pom.xml中的
<!-- <dependency>-->
<!-- <groupId>org.springframework.cloud</groupId>-->
<!-- <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>-->
<!-- </dependency>-->
注释掉了,防止jar冲突。以及config中也不需要注入RestTemplate的bean了。
首先引入jar包
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.cuizb</groupId>
<artifactId>cloud-alibaba-demo</artifactId>
<version>1.0</version>
</parent>
<groupId>com.cuizb.cloud.alibaba</groupId>
<artifactId>gtw</artifactId>
<version>1.0</version>
<name>gtw</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- <dependency>-->
<!-- <groupId>org.springframework.cloud</groupId>-->
<!-- <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>-->
<!-- </dependency>-->
<!-- Dubbo依赖 -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.7.0</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
<version>2.7.0</version>
</dependency>
<!-- Nacos依赖 -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-registry-nacos</artifactId>
<version>2.7.1</version>
</dependency>
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>com.cuizb</groupId>
<artifactId>dubbo-demo-service</artifactId>
<version>1.0</version>
<scope>compile</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
网关入口类
GtwServiceImpl
package com.cuizb.cloud.alibaba.gtw.controller;
import com.cuizb.dubbo.demoservice.api.DubboDemoService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
/**
* @Author cuizb
* @Date 2022-03-19 18:12:16
* @Desc *
*/
@RestController
public class GtwServiceImpl {
// @Autowired
// private RestTemplate restTemplate;
//
// @GetMapping("/hello")
// public String hello(@RequestParam("name") String name) {
// System.out.println(name);
// return restTemplate.getForObject("http://demo-service/hello?name=" + name , String.class);
//
// }
// Dubbo远程调用注解
@Reference
private DubboDemoService dubboDemoService;
@GetMapping("/hello")
public String hello(@RequestParam String name) {
return dubboDemoService.hello(name);
}
}
注意:
注入注解是dubbo中@Reference
dubbo和nacos配置信息
server.port=8705
spring.application.name=dubbo-gtw
# 配置服务信息
dubbo.application.name=dubbo-gtw
# 禁用QOS同一台机器可能会有端口冲突现象
dubbo.qos-enable=false
dubbo.qos-accept-foreign-ip=false
# 配置注册中心
dubbo.registry.address=nacos://127.0.0.1:8848
# 设置协议-协议由提供方指定消费方被动接受
dubbo.protocol.name=dubbo
dubbo.protocol.port=7705
dubbo.consumer.timeout=3000
# 解决Bean重复定义问题
spring.main.allow-bean-definition-overriding=true
启动类加上@EnableDubbo
@EnableDubbo
@SpringBootApplication
public class GtwApplication {
public static void main(String[] args) {
SpringApplication.run(GtwApplication.class, args);
}
}
启动工程
首先启动服务提供者dubbo-demo-service,然后启动服务消费者gtw
查看nacos
测试
输入地址:http://localhost:8705/hello?name=cuizb1
如果先启动消费者会出现启动报错
Caused by: java.lang.IllegalStateException: Failed to check the status of the service com.cuizb.dubbo.demoservice.api.DubboDemoService. No provider available for the service com.cuizb.dubbo.demoservice.api.DubboDemoService from the url nacos://127.0.0.1:8848/org.apache.dubbo.registry.RegistryService?application=dubbo-gtw&default.generic=false&default.timeout=3000&dubbo=2.0.2&generic=false&interface=com.cuizb.dubbo.demoservice.api.DubboDemoService&methods=hello&pid=53247&qos.enable=false®ister.ip=192.168.1.5&release=2.7.0&side=consumer×tamp=1647833584728 to the consumer 192.168.1.5 use dubbo version 2.7.0
at org.apache.dubbo.config.ReferenceConfig.createProxy(ReferenceConfig.java:393) ~[dubbo-2.7.0.jar:2.7.0]
at org.apache.dubbo.config.ReferenceConfig.init(ReferenceConfig.java:301) ~[dubbo-2.7.0.jar:2.7.0]
at org.apache.dubbo.config.ReferenceConfig.get(ReferenceConfig.java:225) ~[dubbo-2.7.0.jar:2.7.0]
at org.apache.dubbo.config.spring.beans.factory.annotation.ReferenceAnnotationBeanPostProcessor$ReferenceBeanInvocationHandler.init(ReferenceAnnotationBeanPostProcessor.java:162) ~[dubbo-2.7.0.jar:2.7.0]
at org.apache.dubbo.config.spring.beans.factory.annotation.ReferenceAnnotationBeanPostProcessor$ReferenceBeanInvocationHandler.access$100(ReferenceAnnotationBeanPostProcessor.java:146) ~[dubbo-2.7.0.jar:2.7.0]
at org.apache.dubbo.config.spring.beans.factory.annotation.ReferenceAnnotationBeanPostProcessor.buildInvocationHandler(ReferenceAnnotationBeanPostProcessor.java:140) ~[dubbo-2.7.0.jar:2.7.0]
at org.apache.dubbo.config.spring.beans.factory.annotation.ReferenceAnnotationBeanPostProcessor.buildProxy(ReferenceAnnotationBeanPostProcessor.java:122) ~[dubbo-2.7.0.jar:2.7.0]
at org.apache.dubbo.config.spring.beans.factory.annotation.ReferenceAnnotationBeanPostProcessor.doGetInjectedBean(ReferenceAnnotationBeanPostProcessor.java:116) ~[dubbo-2.7.0.jar:2.7.0]
at org.apache.dubbo.config.spring.beans.factory.annotation.ReferenceAnnotationBeanPostProcessor.doGetInjectedBean(ReferenceAnnotationBeanPostProcessor.java:49) ~[dubbo-2.7.0.jar:2.7.0]
at org.apache.dubbo.config.spring.beans.factory.annotation.AnnotationInjectedBeanPostProcessor.getInjectedObject(AnnotationInjectedBeanPostProcessor.java:340) ~[dubbo-2.7.0.jar:2.7.0]
at org.apache.dubbo.config.spring.beans.factory.annotation.AnnotationInjectedBeanPostProcessor$AnnotatedFieldElement.inject(AnnotationInjectedBeanPostProcessor.java:520) ~[dubbo-2.7.0.jar:2.7.0]
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:116) ~[spring-beans-5.2.1.RELEASE.jar:5.2.1.RELEASE]
at org.apache.dubbo.config.spring.beans.factory.annotation.AnnotationInjectedBeanPostProcessor.postProcessPropertyValues(AnnotationInjectedBeanPostProcessor.java:128) ~[dubbo-2.7.0.jar:2.7.0]
JVM内存泄漏和内存溢出的原因
JVM常用监控工具解释以及使用
Redis 常见面试题(一)
ClickHouse之MaterializeMySQL引擎(十)
三种实现分布式锁的实现与区别
线程池的理解以及使用
号外!号外!
最近面试BAT,整理一份面试资料,覆盖了Java核心技术、JVM、Java并发、SSM、微服务、数据库、数据结构等等。想获取吗?如果你想提升自己,并且想和优秀的人一起进步,感兴趣的朋友,可以在扫码关注下方公众号。资料在公众号里静静的躺着呢。。。
- 喜欢就收藏
- 认同就点赞
- 支持就关注
- 疑问就评论
一键四连,你的offer也四连
————————————————————————————————————————————————————————————————
本文作者:Java技术债务
原文链接:https://www.cuizb.top/myblog/article/1647833843
版权声明: 本博客所有文章除特别声明外,均采用 CC BY 3.0 CN协议进行许可。转载请署名作者且注明文章出处。
本文来自博客园,作者:Java技术债务,转载请注明原文链接:https://www.cnblogs.com/cuizb/p/16428490.html