springboot dubbo nacos sentinel 快速集成

悠悠 2021-11-11 16:56 598阅读 0赞

一 场景

  1. 我们以用户服务为例来对 springboot dubbo nacos sentinel 进行整合,我们现在有一个根据用户ID获取用户详情的接口,需要服务化给其他业务使用,dubbo 作为RPC访问框架,nacos做服务注册中心,sentinel 做限流 来保护用户服务。新建了一个工程,工程目录如下:

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2xqMTMxNGFpbGo_size_16_color_FFFFFF_t_70

user-api :用户服务的API 包括接口定义,请求参数,返回参数 服务提供者(user-provider)和服务消费者使用(user-comsumer)

user-provider:用户服务的提供者

user-comsumer:用户服务消费者 消费者本不应该写在这个工程,我为方便就写在一起。

二 nacos 和 Sentinel Dashboard 安装

  1. 整合需要用到nacos Sentinel Dashboard 所以先安装

1.nacos安装

  1. nacos安装参考我的另一篇文章:[https://blog.csdn.net/lj1314ailj/article/details/98509699][https_blog.csdn.net_lj1314ailj_article_details_98509699]

2.Sentinel Dashboard 安装

  1. 下载地址:[https://github.com/alibaba/Sentinel/releases][https_github.com_alibaba_Sentinel_releases]
  2. 我下载的地址jar:[https://github.com/alibaba/Sentinel/releases/download/1.6.3/sentinel-dashboard-1.6.3.jar][https_github.com_alibaba_Sentinel_releases_download_1.6.3_sentinel-dashboard-1.6.3.jar]
  3. 启动:
  4. 注意:启动 Sentinel 控制台需要 JDK 版本为 1.8 及以上版本。
  5. 使用如下命令启动控制台:
  6. java -Dserver.port=8888 -Dcsp.sentinel.dashboard.server=localhost:8888 -Dproject.name=sentinel-dashboard -jar sentinel-dashboard-1.6.3.jar
  7. 其中 -Dserver.port=8888 用于指定 Sentinel 控制台端口为 8888
  8. 访问:[http://localhost:8888][http_localhost_8888]

从 Sentinel 1.6.0 起,Sentinel 控制台引入基本的登录功能,默认用户名和密码都是 sentinel。

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2xqMTMxNGFpbGo_size_16_color_FFFFFF_t_70 1

三 整合

1.user-api 代码编写

  1. package com.lvzhu.user.api;
  2. import com.lvzhu.user.resp.User;
  3. /**
  4. * @author lvzhu.
  5. * Time 2019-08-05 17:59
  6. * Desc 文件描述
  7. */
  8. public interface UserService {
  9. /**
  10. *
  11. * 获取用户信息
  12. * @param id
  13. * @return
  14. */
  15. User getUser(Long id);
  16. }

b.返回实体定义

  1. package com.lvzhu.user.resp;
  2. import java.io.Serializable;
  3. /**
  4. * @author lvzhu.
  5. * Time 2019-08-05 17:59
  6. * Desc 文件描述
  7. */
  8. public class User implements Serializable {
  9. private static final long serialVersionUID = 1008450983070357113L;
  10. /**
  11. * 主键ID
  12. */
  13. private Long id;
  14. /**
  15. * 用户名
  16. */
  17. private String userName;
  18. /**
  19. * 年龄
  20. */
  21. private Integer age;
  22. /** 省略get set**/
  23. }

2.user-provider 编码

  1. a.整合springboot dubbo nacos

pom.xml 添加

  1. <!-- springboot dubbo starter-->
  2. <dependency>
  3. <groupId>org.apache.dubbo</groupId>
  4. <artifactId>dubbo-spring-boot-starter</artifactId>
  5. <version>2.7.1</version>
  6. </dependency>
  7. <dependency>
  8. <groupId>org.apache.dubbo</groupId>
  9. <artifactId>dubbo</artifactId>
  10. <version>2.7.3</version>
  11. </dependency>
  12. <!-- nacos 注册中心-->
  13. <dependency>
  14. <groupId>org.apache.dubbo</groupId>
  15. <artifactId>dubbo-registry-nacos</artifactId>
  16. <version>2.7.3</version>
  17. </dependency>
  18. <dependency>
  19. <groupId>com.alibaba.nacos</groupId>
  20. <artifactId>nacos-client</artifactId>
  21. <version>1.1.1</version>
  22. </dependency>
  23. <dependency>
  24. <groupId>com.alibaba.spring</groupId>
  25. <artifactId>spring-context-support</artifactId>
  26. <version>1.0.2</version>
  27. </dependency>

参考文章:http://dubbo.apache.org/zh-cn/docs/user/references/registry/nacos.html

b. dubbo整合sentinel

pom.xml 添加

  1. <!--
  2. dubbo 版本 Apache Dubbo 2.7.x 及以上版本
  3. sentinel-apache-dubbo-adapter
  4. 对于Dubbo 2.6.x 及以下版本 sentinel-dubbo-adapter
  5. -->
  6. <dependency>
  7. <groupId>com.alibaba.csp</groupId>
  8. <artifactId>sentinel-apache-dubbo-adapter</artifactId>
  9. <version>1.6.3</version>
  10. </dependency>
  11. <!--使用sentinel dashboard导入-->
  12. <dependency>
  13. <groupId>com.alibaba.csp</groupId>
  14. <artifactId>sentinel-transport-simple-http</artifactId>
  15. <version>1.6.3</version>
  16. </dependency>

需要注意 dubbo 版本 整合时没有注意被坑了。

  1. dubbo 版本 Apache Dubbo 2.7.x 及以上版本 sentinel-apache-dubbo-adapter
  2. 对于Dubbo 2.6.x 及以下版本 sentinel-dubbo-adapter
  3. 如果dubbo版本使用2.7.x 使用了sentinel-dubbo-adapter jar服务调用时会出问题错误如下:
  4. java.lang.NoSuchMethodError: com.alibaba.dubbo.rpc.RpcContext.setAttachment

sentinel 和更多主流框整合:

https://github.com/alibaba/Sentinel/wiki/%E4%B8%BB%E6%B5%81%E6%A1%86%E6%9E%B6%E7%9A%84%E9%80%82%E9%85%8D

c.application.properties 配置

  1. spring.application.name=springboot-user-provider
  2. ##nacos 配置中心
  3. dubbo.config-center.address=nacos://127.0.0.1:8848
  4. dubbo.config-center.app-name=springboot-user-provider
  5. ##dubbo 服务扫描包
  6. dubbo.scan.base-packages=com.lvzhu.user.biz
  7. ##dubbo.metadata-report.address=zookeeper://127.0.0.1:2181
  8. ##dubbo.metadata-report.address=nacos://127.0.0.1:8848
  9. dubbo.protocol.name=dubbo
  10. dubbo.protocol.port=20880
  11. dubbo.application.name=springboot-user-provider
  12. ##nacos 注册中心 用zookeeper只需要把地址改为 zookeeper://127.0.0.1:2181
  13. dubbo.registry.address=nacos://127.0.0.1:8848
  14. ##dubbo.registry.simplified=true

nacos 既可以做注册中心也可以做配置中心

d.UserServiceImpl 用户实现类

  1. package com.lvzhu.user.biz;
  2. import com.lvzhu.user.api.UserService;
  3. import com.lvzhu.user.resp.User;
  4. import org.apache.dubbo.config.annotation.Service;
  5. /**
  6. * @author lvzhu.
  7. * Time 2019-08-05 18:16
  8. * Desc 文件描述
  9. */
  10. @Service(loadbalance = "random",cluster = "failsafe")
  11. public class UserServiceImpl implements UserService {
  12. /**
  13. * 获取用户信息
  14. */
  15. @Override
  16. public User getUser(Long id) {
  17. User user = new User();
  18. user.setAge(2+id.intValue());
  19. user.setUserName("LVZHU"+id);
  20. user.setId(id);
  21. System.out.println("Come in getUser()");
  22. return user;
  23. }
  24. }

e.UserProviderApplication 启动类

  1. package com.lvzhu.user;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. @SpringBootApplication
  5. public class UserProviderApplication {
  6. public static void main(String[] args) {
  7. SpringApplication.run(UserProviderApplication.class, args);
  8. }
  9. }

这样用户服务端可以使用了

3.user-comsumer 用服务消费者编写

a. pom.xml 同服务提供者基本相同只是 spring-boot-starter 变成了spring-boot-starter-web

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-web</artifactId>
  4. </dependency>

b.application.properties 如下

  1. spring.application.name=springboot-user-comsumer
  2. server.port=8081
  3. dubbo.scan.base-packages=com.lvzhu.user.client
  4. dubbo.application.name=springboot-user-comsumer
  5. ## dubbo.registry.address=zookeeper://127.0.0.1:2181
  6. dubbo.registry.address=nacos://127.0.0.1:8848

d.UserClient

  1. package com.lvzhu.user.client;
  2. import com.lvzhu.user.api.UserService;
  3. import com.lvzhu.user.resp.User;
  4. import org.apache.dubbo.config.annotation.Reference;
  5. import org.springframework.stereotype.Service;
  6. /**
  7. * @author lvzhu.
  8. * Time 2019-08-05 20:17
  9. * Desc 文件描述
  10. */
  11. @Service
  12. public class UserClient {
  13. @Reference(loadbalance = "roundrobin", cluster = "failfast", check = false)
  14. private UserService userService;
  15. public User getUser(Long id) {
  16. return userService.getUser(id);
  17. }
  18. }

e.UserController

  1. package com.lvzhu.user.controller;
  2. import com.lvzhu.user.client.UserClient;
  3. import com.lvzhu.user.resp.User;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.web.bind.annotation.GetMapping;
  6. import org.springframework.web.bind.annotation.PathVariable;
  7. import org.springframework.web.bind.annotation.RestController;
  8. /**
  9. * @author lvzhu.
  10. * Time 2019-08-05 20:17
  11. * Desc 文件描述
  12. */
  13. @RestController
  14. public class UserController {
  15. @Autowired
  16. private UserClient userClient;
  17. @GetMapping("/user/{id}")
  18. public User getUser(@PathVariable(name = "id") Long id) {
  19. return userClient.getUser(id);
  20. }
  21. }

f.UserComsumerApplication启动类

  1. package com.lvzhu.user;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. @SpringBootApplication
  5. public class UserComsumerApplication {
  6. public static void main(String[] args) {
  7. SpringApplication.run(UserComsumerApplication.class, args);
  8. }
  9. }

四 启动

1.启动用户服务提供者

  1. 运行 UserProviderApplication
  2. 运行需要添加参数
  3. -Djava.net.preferIPv4Stack=true -Dcsp.sentinel.api.port=8720 -Dcsp.sentinel.dashboard.server=localhost:8888 - Dproject.name=springboot-user-provider

如下配置:

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2xqMTMxNGFpbGo_size_16_color_FFFFFF_t_70 2

2.启动用户服务的消费者

  1. 运行UserComsumerApplication
  2. 运行需要添加参数
  3. -Djava.net.preferIPv4Stack=true -Dcsp.sentinel.api.port=8721 -Dcsp.sentinel.dashboard.server=localhost:8888 -Dproject.name=springboot-user-comsumer

启动完成后:

nacos控制台 如下:

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2xqMTMxNGFpbGo_size_16_color_FFFFFF_t_70 3

Sentinel Dashboard 如下:

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2xqMTMxNGFpbGo_size_16_color_FFFFFF_t_70 4

需要注意:要访问一下 http://localhost:8081/user/1 才会有 springboot-user-comsumer 和 springboot-user-provider

  1. 我们对springboot-user-provider进行流控:
  2. 点击簇点链路 可以对接口和方法进行流控 ,降级。

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2xqMTMxNGFpbGo_size_16_color_FFFFFF_t_70 5

如下新建流控 我把单机的qps设置为了1 超过1就会快速失败

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2xqMTMxNGFpbGo_size_16_color_FFFFFF_t_70 6

当然可以对 springboot-user-comsumer 进行限流方法同上。

整合的案例:https://github.com/ljmomo/springboot-dubbo-nacos-sentinel

clone 确保 naocos 和 sentinel都启动再运行

五 参考文献:

  1. dubbo:[http://dubbo.apache.org/zh-cn/docs/user/quick-start.html][http_dubbo.apache.org_zh-cn_docs_user_quick-start.html]
  2. nacos:[https://nacos.io/zh-cn/docs/quick-start.html][https_nacos.io_zh-cn_docs_quick-start.html]

sentinel:https://github.com/alibaba/Sentinel/wiki/%E4%BB%8B%E7%BB%8D

发表评论

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

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

相关阅读