有一个很简单的不用配置pom和dependecy的方法,但是每个模块不是工程,https://blog.csdn.net/qq_43762185/article/details/120811212?spm=1001.2014.3001.5501
我也是参考了很多博主,其中主要是这个博主的
https://cloud.tencent.com/developer/article/1837815
我会描述的更详细一点
- nacos下载及配置
1.1进入官网https://github.com/alibaba/nacos/releases
将页面滑倒最下面,可以看到,一般win系统下载zip,然后解压
1.2.
如果你是在跟我一样练手,cmd cd到bin目录,输入以下语句 单机模式启动
startup.cmd -m standalone
如果你是集群启动,就可以直接在bin目录下双击stratup.cmd
1.3启动成功后,输入红框中的地址,默认账号和密码是nacos
2.springboot项目搭建(重点)
2.1创建一个一个maven项目
创建好以后,删除src目录,配置pom文件,下面是我的pom文件
这里有几个注意点:
你前面的groupid是什么,你这里就要是什么,我的就是com.sxh
你粘贴上去可能会报错,因为还没有创建modules,之后创建了就好了,这是父工程。
4.0.0 com.sxh dubbo-nacos 1.0-SNAPSHOT common-api dubbo-provide service-consumer pom dubbo-nacos Maven Webapp http://www.example.com UTF-8 8 8 4.1.56.Final 2.3.5.RELEASE 2.7.8 1.4.0 org.springframework.boot spring-boot-dependencies ${spring-boot.version} pom import org.apache.dubbo dubbo-dependencies-bom ${dubbo.version} pom import org.apache.dubbo dubbo-spring-boot-starter ${dubbo.version} org.apache.dubbo dubbo-registry-nacos ${dubbo.version} com.alibaba.nacos nacos-client ${nacos-client.version} org.apache.dubbo dubbo ${dubbo.version} org.springframework spring javax.servlet servlet-api log4j log4j org.springframework.boot spring-boot-starter-test test junit junit 4.11 test dubbo-nacos maven-clean-plugin 3.1.0 maven-resources-plugin 3.0.2 maven-compiler-plugin 3.8.0 maven-surefire-plugin 2.22.1 maven-war-plugin 3.2.2 maven-install-plugin 2.5.2 maven-deploy-plugin 2.8.2
2.2在父工程下面创建三个子工程,分别为commn-api(maven工程),dubbo-service(springboot工程),service-consumer(springboot工程),这是整体的目录
创建好了之后,对于common-api
pom.xml
主要加入parent模块和一些依赖
4.0.0 com.sxh dubbo-nacos 1.0-SNAPSHOT com.sxh common-api 1.0-SNAPSHOT war common-api Maven Webapp http://www.example.com UTF-8 1.7 1.7 junit junit 4.11 test common-api maven-clean-plugin 3.1.0 maven-resources-plugin 3.0.2 maven-compiler-plugin 3.8.0 maven-surefire-plugin 2.22.1 maven-war-plugin 3.2.2 maven-install-plugin 2.5.2 maven-deploy-plugin 2.8.2
创建SayService:
package com.sxh.service;
public interface SayService {
String sayHelloByName(String name);
}
dubbo-provide:
pom.xml
com.sxh dubbo-nacos 1.0-SNAPSHOT 4.0.0 com.sxh dubbo-provide 0.0.1-SNAPSHOT dubbo-provide dubbo-provide 1.8 UTF-8 UTF-8 2.3.7.RELEASE org.springframework.boot spring-boot-starter-web org.apache.dubbo dubbo-spring-boot-starter org.apache.dubbo dubbo org.apache.dubbo dubbo-registry-nacos com.alibaba.nacos nacos-client com.sxh common-api 1.0-SNAPSHOT org.springframework.boot spring-boot-test 2.5.4 test org.junit.jupiter junit-jupiter test org.springframework.boot spring-boot-dependencies ${spring-boot.version} pom import org.apache.maven.plugins maven-compiler-plugin 3.8.1 1.8 1.8 UTF-8 org.springframework.boot spring-boot-maven-plugin 2.3.7.RELEASE com.sxh.dubboprovide.DubboProvideApplication repackage repackage
SayServiceImpl.java:
出现红线不要慌,看能不能import class,要不然就加入依赖,刷新一下maven
package com.sxh.dubboprovide.impl;
import com.sxh.service.SayService;
import org.apache.dubbo.config.annotation.DubboService;
@DubboService
public class SayServiceImpl implements SayService {
@Override
public String sayHelloByName(String name) {
return name+",hello!";
}
}
启动类上加上@EnableDubbo注解
application.yml:`
server:
port: 14511
spring:
application:
name: provider-service
main:
allow-bean-definition-overriding: true
dubbo:
application:
name: provider-service
registry:
address: nacos://10.199.15.74:8848
username: nacos
password: nacos
scan:
base-packages: com.sxh.dubboprovide.impl
protocol:
name: dubbo
port: 15511
dubbo-consumer
pom.xml:
com.sxh dubbo-nacos 1.0-SNAPSHOT 4.0.0 com.sxh service-consumer 0.0.1-SNAPSHOT service-consumer service-consumer 1.8 UTF-8 UTF-8 2.3.7.RELEASE org.springframework.boot spring-boot-starter-web org.apache.dubbo dubbo-spring-boot-starter org.apache.dubbo dubbo org.apache.dubbo dubbo-registry-nacos com.alibaba.nacos nacos-client com.sxh common-api 1.0-SNAPSHOT org.testng testng RELEASE test org.springframework.boot spring-boot-test test org.junit.jupiter junit-jupiter-api test org.springframework.boot spring-boot-dependencies ${spring-boot.version} pom import org.apache.maven.plugins maven-compiler-plugin 3.8.1 1.8 1.8 UTF-8 org.springframework.boot spring-boot-maven-plugin 2.3.7.RELEASE com.sxh.serviceconsumer.ServiceConsumerApplication repackage repackage
application.yml:
跟上面一样,改成自己的ip
server:
port: 14512
spring:
application:
name: consumer-service
main:
allow-bean-definition-overriding: true
dubbo:
application:
name: consumer-service
registry:
address: nacos://10.199.15.74:8848
username: nacos
password: nacos
protocol:
name: dubbo
port: 15511
SayController.java:
package com.sxh.serviceconsumer.controller;
import com.sxh.service.SayService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/demo/say")
public class SayController {
@DubboReference
private SayService sayService;
@GetMapping("/sayHello")
public ResponseEntity sayHello(@RequestParam("name") String name) {
return ResponseEntity.ok(sayService.sayHelloByName(name));
}
}
启动类上也要加上@EnableDubbo
2.3依次启动provider,consumer模块,就可以看到
2.4测试接口



