nacos+springboot+dubbo微服务整合

电玩女神 2023-07-23 09:59 97阅读 0赞

1,安装nacos

从 Github 上下载源码方式

  1. git clone https://github.com/alibaba/nacos.git
  2. cd nacos/
  3. mvn -Prelease-nacos -Dmaven.test.skip=true clean install -U
  4. ls -al distribution/target/
  5. // change the $version to your actual path
  6. cd distribution/target/nacos-server-$version/nacos/bin

下载编译后压缩包方式
下载地址:https://github.com/alibaba/nacos/releases

  1. unzip nacos-server-$version.zip 或者 tar -xvf nacos-server-$version.tar.gz
  2. cd nacos/bin

启动服务器
Linux/Unix/Mac
启动命令(standalone代表着单机模式运行,非集群模式):

sh startup.sh -m standalone

如果您使用的是ubuntu系统,或者运行脚本报错提示[[符号找不到,可尝试如下运行:

bash startup.sh -m standalone

Windows
启动命令:

cmd startup.cmd

或者双击startup.cmd运行文件。

nacos控制台界面
http://ip:port/nacos/index.html
默认用户名:nacos
默认密码:nacos
在这里插入图片描述

2,项目框架结构

在这里插入图片描述

  • api:接口部分,对外提供接口使用
  • provider:服务的提供者
  • consumer:服务的消费者

3,springboot整合dubbo

1,父工程pom

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <modelVersion>4.0.0</modelVersion>
  6. <groupId>person.david</groupId>
  7. <artifactId>springboot-nacos-dubbo</artifactId>
  8. <packaging>pom</packaging>
  9. <version>1.0</version>
  10. <modules>
  11. <module>provider</module>
  12. <module>consumer</module>
  13. <module>api</module>
  14. </modules>
  15. <parent>
  16. <groupId>org.springframework.boot</groupId>
  17. <artifactId>spring-boot-parent</artifactId>
  18. <version>2.2.2.RELEASE</version>
  19. </parent>
  20. </project>

2,api模块
TestApi.class

  1. /**
  2. * @author David
  3. * @className TestApi
  4. * @date 2020/4/7 16:47
  5. */
  6. public interface TestApi {
  7. String sayHello(String name);
  8. }

3,provider提供者模块

SayHelloProvider.class

  1. import org.apache.dubbo.config.annotation.Service;
  2. import person.david.api.TestApi;
  3. /**
  4. * @author Administrator
  5. * @create 2020/4/7
  6. * @since 1.0.0
  7. */
  8. @Service
  9. public class SayHelloProvider implements TestApi {
  10. @Override
  11. public String sayHello(String name) {
  12. return "收到提供者返回的消息========>Hello,"+name;
  13. }
  14. }

ProviderStart.class

  1. import org.springframework.boot.SpringApplication;
  2. import org.springframework.boot.autoconfigure.SpringBootApplication;
  3. /**
  4. * @author Administrator
  5. * @create 2020/4/7
  6. * @since 1.0.0
  7. */
  8. @SpringBootApplication
  9. public class ProviderStart {
  10. public static void main(String[] args) {
  11. SpringApplication.run(ProviderStart.class, args);
  12. }
  13. }

application.yml

  1. spring:
  2. application:
  3. name: nacos-provider
  4. dubbo:
  5. scan:
  6. base-packages: person.david.provider
  7. protocol:
  8. name: dubbo
  9. port: 12345
  10. registry:
  11. address: nacos://120.75.70.231:8848
  12. server:
  13. port: 10000

4,consumer消费者模块
SayHelloController.class

  1. import org.apache.dubbo.config.annotation.Reference;
  2. import org.springframework.web.bind.annotation.RequestMapping;
  3. import org.springframework.web.bind.annotation.RestController;
  4. import person.david.api.TestApi;
  5. /**
  6. * @author Administrator
  7. * @create 2020/4/7
  8. * @since 1.0.0
  9. */
  10. @RestController
  11. @RequestMapping("/nacos")
  12. public class SayHelloController {
  13. @Reference
  14. private TestApi testApi;
  15. @RequestMapping("/sayHello")
  16. public String SayHello(){
  17. return testApi.sayHello("nacos");
  18. }
  19. }

ConsumerStart.class

  1. import org.springframework.boot.SpringApplication;
  2. import org.springframework.boot.autoconfigure.SpringBootApplication;
  3. /**
  4. * @author Administrator
  5. * @create 2020/4/7
  6. * @since 1.0.0
  7. */
  8. @SpringBootApplication
  9. public class ConsumerStart {
  10. public static void main(String[] args) {
  11. SpringApplication.run(ConsumerStart.class, args);
  12. }
  13. }

application.yml

  1. spring:
  2. application:
  3. name: nacos-consumer
  4. dubbo:
  5. registry:
  6. address: nacos://120.75.70.231:8848
  7. username: nacos
  8. password: nacos
  9. server:
  10. port: 10001

4,启动提供者,消费者服务

在这里插入图片描述

5,测试

在这里插入图片描述

发表评论

表情:
评论列表 (有 0 条评论,97人围观)

还没有评论,来说两句吧...

相关阅读