springboot+dubbo+redis+RabbitMQ 项目整合实例

来源:https://blog.csdn.net/qq_28125445

作者:夏佐

关于springboot, 之前零零碎碎地写了一些,今天从项目实战角度给大家分享一下我的一点经验。

话不多说,先从项目的目录结构讲起。(文章最后贴了源码下载地址

如图:

项目分层:

parent(顶级 )pom

    -- api(公共API、DTO等)jar

    -- foundation(公共基设)jar

    -- modules(前端consumer)pom

        -- www(官网)jar

        -- admin(后台)jar

        -- ......

     -- service(provider,本应该拆分,我这里只写了一个) pom

         -- user(用户中心)jar

         -- order(订单中心)jar

         -- ......

所用技术: springboot、dubbo、zookeeper、mybatis、redis、RabbitMQ、druid、swagger等

效果截图:

自动生成的swagger接口文档,可直接测试:



使用druid的SQL监控,记录慢SQL:

dubbo admin:

RabbitMQ admin:

项目截图:

部分代码:

消费者(暴露给前端的controller)

  1. package com.ysk.admin.controller;

  2. import java.util.List;

  3. import org.springframework.web.bind.annotation.GetMapping;

  4. import org.springframework.web.bind.annotation.RequestBody;

  5. import org.springframework.web.bind.annotation.RequestMapping;

  6. import org.springframework.web.bind.annotation.RequestMethod;

  7. import org.springframework.web.bind.annotation.RequestParam;

  8. import org.springframework.web.bind.annotation.RestController;

  9. import com.alibaba.dubbo.config.annotation.Reference;

  10. import com.ysk.api.model.User;

  11. import com.ysk.api.service.UserService;

  12. @RestController

  13. @RequestMapping("/")

  14. public class TestController {

  15. @Reference(version = "1.0.0")

  16. private UserService testService;

  17. @GetMapping("hello")

  18. public String hello() {

  19. return testService.sayHello("Hello springboot and dubbo!");

  20. }

  21. @GetMapping("user")

  22. public User user() {

  23. return testService.findUser();

  24. }

  25. @GetMapping("list")

  26. public List<User> list(@RequestParam(defaultValue = "1") int page,

  27. @RequestParam(defaultValue = "10") int pageSize) {

  28. return testService.getUser(page, pageSize);

  29. }

  30. @RequestMapping(value = "add", method = RequestMethod.POST)

  31. public List<User> add(@RequestBody User user) {

  32. System.out.println(user.toString());

  33. return null;

  34. }

  35. // 从redis获取某个用户

  36. @RequestMapping(value = "getuserfromredis", method = RequestMethod.GET)

  37. public User getRedis(@RequestParam String key) {

  38. return testService.getUserForRedis(key);

  39. }

  40. }

  1. package com.ysk.admin;

  2. import org.apache.log4j.Logger;

  3. import org.springframework.boot.SpringApplication;

  4. import org.springframework.boot.autoconfigure.SpringBootApplication;

  5. import org.springframework.context.annotation.ImportResource;

  6. @SpringBootApplication

  7. @ImportResource({ "classpath:config/spring-dubbo.xml" })

  8. public class App {

  9. private static Logger logger = Logger.getLogger(App.class);

  10. public static void main(String[] args) {

  11. SpringApplication.run(App.class, args);

  12. logger.info("SpringBoot Start Success");

  13. }

  14. }

提供者(具体的实现与逻辑)

  1. package com.ysk.service.impl;

  2. import java.text.SimpleDateFormat;

  3. import java.util.Date;

  4. import java.util.List;

  5. import org.springframework.beans.factory.annotation.Autowired;

  6. import com.alibaba.dubbo.config.annotation.Service;

  7. import com.github.pagehelper.PageHelper;

  8. import com.ysk.api.model.User;

  9. import com.ysk.api.service.UserService;

  10. import com.ysk.resource.UserMapper;

  11. import com.ysk.service.RedisService;

  12. @Service(version = "1.0.0", interfaceClass = UserService.class)

  13. public class UserServiceImpl implements UserService {

  14. @Autowired

  15. private UserMapper userMapper;

  16. @Autowired

  17. private RedisService redisService;

  18. @Override

  19. public String sayHello(String str) {

  20. SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

  21. return dateFormat.format(new Date()) + ": " + str;

  22. }

  23. @Override

  24. public User findUser() {

  25. User user = new User();

  26. user.setId(1001);

  27. user.setName("张三");

  28. user.setAge(20);

  29. user.setAddress("上海徐汇");

  30. return user;

  31. }

  32. @Override

  33. public List<User> getUser(int page, int pageSize) {

  34. PageHelper.startPage(page, pageSize);

  35. return userMapper.getUsers();

  36. }

  37. @Override

  38. public User getUserForRedis(String key) {

  39. User user = new User();

  40. user.setId(1008);

  41. user.setName("刘德华");

  42. user.setAge(60);

  43. user.setAddress("中国香港");

  44. redisService.set(user.getId() + "", user);

  45. return (User) redisService.get(key);

  46. }

  47. }

  1. package com.ysk.config;

  2. import java.util.Properties;

  3. import org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator;

  4. import org.springframework.context.annotation.Bean;

  5. import org.springframework.context.annotation.Configuration;

  6. import org.springframework.transaction.PlatformTransactionManager;

  7. import org.springframework.transaction.interceptor.TransactionInterceptor;

  8. @Configuration

  9. public class TransactionConfiguration {

  10. @Bean(name = "transactionInterceptor")

  11. public TransactionInterceptor transactionInterceptor(PlatformTransactionManager platformTransactionManager) {

  12. TransactionInterceptor transactionInterceptor = new TransactionInterceptor();

  13. // 事务管理器

  14. transactionInterceptor.setTransactionManager(platformTransactionManager);

  15. Properties transactionAttributes = new Properties();

  16. // 新增

  17. transactionAttributes.setProperty("insert*", "PROPAGATION_REQUIRED,-Throwable");

  18. // 修改

  19. transactionAttributes.setProperty("update*", "PROPAGATION_REQUIRED,-Throwable");

  20. // 删除

  21. transactionAttributes.setProperty("delete*", "PROPAGATION_REQUIRED,-Throwable");

  22. // 查询

  23. transactionAttributes.setProperty("select*", "PROPAGATION_REQUIRED,-Throwable, readOnly");

  24. transactionInterceptor.setTransactionAttributes(transactionAttributes);

  25. return transactionInterceptor;

  26. }

  27. // 代理到ServiceImpl的Bean

  28. @Bean

  29. public BeanNameAutoProxyCreator transactionAutoProxy() {

  30. BeanNameAutoProxyCreator transactionAutoProxy = new BeanNameAutoProxyCreator();

  31. transactionAutoProxy.setProxyTargetClass(true);

  32. transactionAutoProxy.setBeanNames("*ServiceImpl");

  33. transactionAutoProxy.setInterceptorNames("transactionInterceptor");

  34. return transactionAutoProxy;

  35. }

  36. }

  1. package com.ysk.config;

  2. import org.springframework.amqp.core.Binding;

  3. import org.springframework.amqp.core.BindingBuilder;

  4. import org.springframework.amqp.core.Queue;

  5. import org.springframework.amqp.core.TopicExchange;

  6. import org.springframework.context.annotation.Bean;

  7. import org.springframework.context.annotation.Configuration;

  8. @Configuration

  9. public class RabbitConfiguration {

  10. // 声明队列

  11. @Bean

  12. public Queue queue1() {

  13. return new Queue("hello.queue1", true); // true表示持久化该队列

  14. }

  15. @Bean

  16. public Queue queue2() {

  17. return new Queue("hello.queue2", true);

  18. }

  19. // 声明交互器

  20. @Bean

  21. TopicExchange topicExchange() {

  22. return new TopicExchange("topicExchange");

  23. }

  24. // 绑定

  25. @Bean

  26. public Binding binding1() {

  27. return BindingBuilder.bind(queue1()).to(topicExchange()).with("key.1");

  28. }

  29. @Bean

  30. public Binding binding2() {

  31. return BindingBuilder.bind(queue2()).to(topicExchange()).with("key.#");

  32. }

  33. }

  1. package com.ysk.config;

  2. import org.springframework.cache.CacheManager;

  3. import org.springframework.cache.annotation.CachingConfigurerSupport;

  4. import org.springframework.cache.annotation.EnableCaching;

  5. import org.springframework.context.annotation.Bean;

  6. import org.springframework.context.annotation.Configuration;

  7. import org.springframework.data.redis.cache.RedisCacheManager;

  8. import org.springframework.data.redis.connection.RedisConnectionFactory;

  9. import org.springframework.data.redis.core.RedisTemplate;

  10. @Configuration  

  11. @EnableCaching

  12. public class RedisConfiguration extends CachingConfigurerSupport {

  13. @Bean

  14. public CacheManager cacheManager(RedisTemplate<?,?> redisTemplate) {

  15. CacheManager cacheManager = new RedisCacheManager(redisTemplate);

  16. return cacheManager;

  17. }

  18. @Bean

  19. public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory factory) {

  20. RedisTemplate<String, String> redisTemplate = new RedisTemplate<String, String>();

  21. redisTemplate.setConnectionFactory(factory);

  22. return redisTemplate;

  23. }

  24. }

  1. spring.datasource.url = jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8

  2. spring.datasource.username = root

  3. spring.datasource.password = root

  4. spring.datasource.driverClassName = com.mysql.jdbc.Driver

  5. #连接池的配置信息

  6. spring.datasource.initial-size=5

  7. spring.datasource.min-idle=5

  8. spring.datasource.max-idle=8

  9. spring.datasource.max-active=20

  10. spring.datasource.max-wait=60000

  11. spring.datasource.timeBetweenEvictionRunsMillis=60000

  12. spring.datasource.minEvictableIdleTimeMillis=300000

  13. spring.datasource.validationQuery=SELECT 1 FROM DUAL

  14. spring.datasource.testWhileIdle=true

  15. spring.datasource.testOnBorrow=false

  16. spring.datasource.testOnReturn=false

  17. spring.datasource.poolPreparedStatements=true

  18. spring.datasource.maxPoolPreparedStatementPerConnectionSize=20

  19. spring.datasource.filters=stat,wall,log4j

  20. spring.datasource.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=200

  21. server.port=8082

  22. server.context-path=/

  23. logging.level.com.ysk.resource=debug

  24. # MyBatis 配置

  25. mybatis.mapper-locations=classpath:/mapper/*Mapper.xml

  26. mybatis.type-aliases-package=com.ysk.api.model

  27. #druid 配置

  28. druid.username=admin

  29. druid.password=admin

  30. druid.allow=127.0.0.1

  31. druid.reset.enable=true

  32. #redis  

  33. spring.redis.hostName=127.0.0.1

  34. spring.redis.port=6379    

  35. spring.redis.pool.maxActive=8    

  36. spring.redis.pool.maxWait=-1    

  37. spring.redis.pool.maxIdle=8    

  38. spring.redis.pool.minIdle=0    

  39. spring.redis.timeout=0

  40. #rabbitMQ

  41. spring.rabbitmq.host=10.99.2.10

  42. spring.rabbitmq.port=5672

  43. spring.rabbitmq.username=admin

  44. spring.rabbitmq.password=admin123

  45. spring.rabbitmq.publisher-confirms=true

  46. spring.rabbitmq.publisher-returns=true

  47. spring.rabbitmq.template.mandatory=true

  48. # 最小消息监听线程数

  49. spring.rabbitmq.listener.concurrency=2

  50. spring.rabbitmq.listener.max-concurrency=2

附上源码:https://pan.baidu.com/s/1FRyL2sWJxx29DaJ0YWhcvQ

猜你喜欢

转载自blog.csdn.net/dome_/article/details/80889249