spring boot 已有项目不修改代码通过配置整合dubbo和nacos实现微服务改造
一、已有项目采用maven多模块方式开发的,通过引入dubbo进行微服务改造
(1) 使用注解自动扫描的方式,需要在开发好的代码中增加注解,涉及到部分代码改造
(2) 采用xml配置的方式新增模块,在不改动任何现有代码的情况下实现微服务改造
由于我改造的是已经开发好的项目,为了避免对代码进行大的改动,使用第二种方式进行
二、引入dubbo和nacos注册中心相关的jar包,进行微服务改造
(1)在服务提供方和消费方都引入以下包,一定要保证dubbo和nacos相关的包版本正确,否则在注册中心可以看到注册的服务,但是调用不一定成功
<!-- dubbo的依赖 --> <!-- https://mvnrepository.com/artifact/org.apache.dubbo/dubbo-spring-boot-starter --> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>2.7.7</version> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.dubbo/dubbo-registry-nacos --> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-registry-nacos</artifactId> <version>2.7.7</version> </dependency> <!-- https://mvnrepository.com/artifact/com.alibaba.boot/nacos-config-spring-boot-starter --> <dependency> <groupId>com.alibaba.boot</groupId> <artifactId>nacos-config-spring-boot-starter</artifactId> <version>0.2.7</version> </dependency> <!-- dubbo的依赖 -->
(2)服务提供方配置文件中增加dubbo及注册中心nacos相关的配置
- 在服务提供端application.yml中增加以下配置
dubbo:
application:
name: wdapi-provider
protocol:
port: -1
name: dubbo
registry:
address: nacos://127.0.0.1:8848
# 可换为zookeeperip
check: false
monitor:
protocol: register
consumer:
check: false
timeout: 3000
- 在服务提供方模块下增加dubbo-provider.xml配置文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://dubbo.apache.org/schema/dubbo" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd"> <!--1、指定当前服务/应用的名字(同样的服务名字相同,不要和其它的服务同名)--> <dubbo:application name="wdapi-provider"/> <!--2、指定注册中心的位置(注册中心不同,服务地址的写法不同)--> <!--<dubbo:registry address="redis://192.168.205.128:6379"/>--> <!-- <dubbo:registry address="zookeeper://192.168.205.128:2181"/>--> <!-- <dubbo:registry address="nacos://127.0.0.1:8848"/>--> <!--3、指定通信规则(通信协议&通信端口)--> <!-- <dubbo:protocol name="dubbo" port="20880"/>--> <!--4、声明需要暴露的服务接口,ref属性要指向容器中的接口实现对象--> <dubbo:service group="dubbo" ref="userService" interface="com.wonders.hrpms.auth.service.UserService"/> <bean id="userService" class="com.wonders.hrpms.auth.serviceimpl.UserServiceImpl"/> <dubbo:service group="dubbo" ref="apiBusinessService" interface="com.wonders.hrpms.auth.service.ApiBusinessService"/> <bean id="apiBusinessService" class="com.wonders.hrpms.auth.serviceimpl.ApiBusinessServiceImpl"/> <dubbo:service group="dubbo" ref="apiDeployService" interface="com.wonders.hrpms.auth.service.ApiDeployService"/> <bean id="apiDeployService" class="com.wonders.hrpms.auth.serviceimpl.ApiDeployServiceImpl"/> <dubbo:service group="dubbo" ref="bf01Service" interface="com.wonders.hrpms.auth.service.Bf01Service"/> <bean id="bf01Service" class="com.wonders.hrpms.auth.serviceimpl.Bf01ServiceImpl"/> <dubbo:service group="dubbo" ref="dictCommonService" interface="com.wonders.hrpms.auth.service.DictCommonService"/> <bean id="dictCommonService" class="com.wonders.hrpms.auth.serviceimpl.DictCommonServiceImpl"/> <dubbo:service group="dubbo" ref="dictService" interface="com.wonders.hrpms.auth.service.DictService"/> <bean id="dictService" class="com.wonders.hrpms.auth.serviceimpl.DictServiceImpl"/> <dubbo:service group="dubbo" ref="fwtjService" interface="com.wonders.hrpms.auth.service.FwtjService"/> <bean id="fwtjService" class="com.wonders.hrpms.auth.serviceimpl.FwtjServiceImpl"/> <dubbo:service group="dubbo" ref="homeServcie" interface="com.wonders.hrpms.auth.service.HomeServcie"/> <bean id="homeServcie" class="com.wonders.hrpms.auth.serviceimpl.HomeServiceImpl"/> <dubbo:service group="dubbo" ref="logServcie" interface="com.wonders.hrpms.auth.service.LogServcie"/> <bean id="logServcie" class="com.wonders.hrpms.auth.serviceimpl.LogServiceImpl"/> <dubbo:service group="dubbo" ref="menuService" interface="com.wonders.hrpms.auth.service.MenuService"/> <bean id="menuService" class="com.wonders.hrpms.auth.serviceimpl.MenuServiceImpl"/> <dubbo:service group="dubbo" ref="rolesMenuService" interface="com.wonders.hrpms.auth.service.RolesMenuService"/> <bean id="rolesMenuService" class="com.wonders.hrpms.auth.serviceimpl.RolesMenuServiceImpl"/> <dubbo:service group="dubbo" ref="rolesService" interface="com.wonders.hrpms.auth.service.RolesService"/> <bean id="rolesService" class="com.wonders.hrpms.auth.serviceimpl.RolesServiceImpl"/> <dubbo:service group="dubbo" ref="sx01Service" interface="com.wonders.hrpms.auth.service.Sx01Service"/> <bean id="sx01Service" class="com.wonders.hrpms.auth.serviceimpl.Sx01ServiceImpl"/> <dubbo:service group="dubbo" ref="sxbfService" interface="com.wonders.hrpms.auth.service.SxbfService"/> <bean id="sxbfService" class="com.wonders.hrpms.auth.serviceimpl.SxbfServiceImpl"/> <dubbo:service group="dubbo" ref="tzggService" interface="com.wonders.hrpms.auth.service.TzggService"/> <bean id="tzggService" class="com.wonders.hrpms.auth.serviceimpl.TzggServiceImpl"/> <dubbo:service group="dubbo" ref="xtpzService" interface="com.wonders.hrpms.auth.service.XtpzService"/> <bean id="xtpzService" class="com.wonders.hrpms.auth.serviceimpl.XtpzServiceImpl"/> <dubbo:service group="dubbo" ref="xtyhService" interface="com.wonders.hrpms.auth.service.XtyhService"/> <bean id="xtyhService" class="com.wonders.hrpms.auth.serviceimpl.XtyhServiceImpl"/> <dubbo:service group="dubbo" ref="entranceService" interface="com.wonders.wdapi.service.EntranceService"/> <bean id="entranceService" class="com.wonders.wdapi.console.serviceimpl.EntranceServiceImpl"/> </beans>
- 在服务启动方启动类中增加相关配置

(3)服务消费方配置文件中增加dubbo及注册中心nacos相关的配置
- 在服务消费端application.yml中增加以下配置
dubbo:
application:
name: wdapi-consumer
protocol:
port: -1
name: dubbo
registry:
address: nacos://127.0.0.1:8848
# 可换为zookeeper zookeeperip
check: false
monitor:
protocol: register
consumer:
check: false
timeout: 3000
- 在服务消费方模块下增加dubbo-consumer.xml配置文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://dubbo.apache.org/schema/dubbo" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd"> <!--1、指定当前服务/应用的名字(同样的服务名字相同,不要和其它的服务同名)--> <!-- <dubbo:application name="wdapi-consumer"/>--> <dubbo:application name="wdapi-consumer" > <dubbo:parameter key="qos.enable" value="true" /> <dubbo:parameter key="qos.accept.foreign.ip" value="false" /> <dubbo:parameter key="qos.port" value="33333" /> </dubbo:application> <!-- <dubbo:registry address="nacos://127.0.0.1:8848"/>--> <!--2、声明需要暴露的服务接口,ref属性要指向容器中的接口实现对象--> <dubbo:reference id="userService" interface="com.wonders.hrpms.auth.service.UserService" group="dubbo"/> <dubbo:reference id="apiBusinessService" interface="com.wonders.hrpms.auth.service.ApiBusinessService" group="dubbo"/> <dubbo:reference id="apiDeployService" interface="com.wonders.hrpms.auth.service.ApiDeployService" group="dubbo"/> <dubbo:reference id="bf01Service" interface="com.wonders.hrpms.auth.service.Bf01Service" group="dubbo"/> <dubbo:reference id="dictCommonService" interface="com.wonders.hrpms.auth.service.DictCommonService" group="dubbo"/> <dubbo:reference id="dictService" interface="com.wonders.hrpms.auth.service.DictService" group="dubbo"/> <dubbo:reference id="fwtjService" interface="com.wonders.hrpms.auth.service.FwtjService" group="dubbo"/> <dubbo:reference id="homeServcie" interface="com.wonders.hrpms.auth.service.HomeServcie" group="dubbo"/> <dubbo:reference id="logServcie" interface="com.wonders.hrpms.auth.service.LogServcie" group="dubbo"/> <dubbo:reference id="menuService" interface="com.wonders.hrpms.auth.service.MenuService" group="dubbo"/> <dubbo:reference id="rolesMenuService" interface="com.wonders.hrpms.auth.service.RolesMenuService" group="dubbo"/> <dubbo:reference id="rolesService" interface="com.wonders.hrpms.auth.service.RolesService" group="dubbo"/> <dubbo:reference id="sx01Service" interface="com.wonders.hrpms.auth.service.Sx01Service" group="dubbo"/> <dubbo:reference id="sxbfService" interface="com.wonders.hrpms.auth.service.SxbfService" group="dubbo"/> <dubbo:reference id="tzggService" interface="com.wonders.hrpms.auth.service.TzggService" group="dubbo"/> <dubbo:reference id="xtpzService" interface="com.wonders.hrpms.auth.service.XtpzService" group="dubbo"/> <dubbo:reference id="xtyhService" interface="com.wonders.hrpms.auth.service.XtyhService" group="dubbo"/> <dubbo:reference id="entranceService" interface="com.wonders.wdapi.service.EntranceService" group="dubbo"/> </beans>
- 在服务消费方启动类中增加相关配置

三、启动服务提供方和服务消费方服务,在nacos注册中心就可以看到相关的服务

项目结构

浙公网安备 33010602011771号