SpringBoot+Dubbo+zookeeper+redis 搭建一次让你永不忘记。

完整的SSM集成Dubbo

在搭建项目之前要会使用SpringBoot,不会的也不要怕,SpringBoot上手简单容易理解,现在很多的项目都是基于SpringBoot去做的开发

具体是Servic的实现类和Controller层怎么使用看本章节最后面 有具体的代码演示。
具体是Servic的实现类和Controller层怎么使用看本章节最后面 有具体的代码演示。
具体是Servic的实现类和Controller层怎么使用看本章节最后面 有具体的代码演示。

首先看我的模块 直接上图吧
SpringBoot+Dubbo+zookeeper+redis 搭建一次让你永不忘记。_第1张图片operation-parentpom.xml



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.1.5.RELEASE
         
    
    com.*.parent
    operation-parent
    0.0.1-SNAPSHOT
    pom
    operation-parent
    Demo project for Spring Boot
    
        operation-service
        operation-web
    
    
        UTF-8
        UTF-8
        1.8
        0.10
        1.16.18
        1.5.7.RELEASE
        2.6.1
    
    
        
            org.springframework.boot
            spring-boot-starter
            ${spring-boot.version}
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
        
            org.projectlombok
            lombok
            ${lombok.version}
            provided
        
        
        
            com.alibaba
            dubbo
            ${dubbo.version}
        
        
            com.alibaba.spring.boot
            dubbo-spring-boot-starter
            2.0.0
            
                
                    com.alibaba
                    dubbo
                
            
        
        
            com.alibaba
            dubbo-registry-zookeeper
            2.6.1
            
                
                    commons-logging
                    commons-logging
                
                
                    org.apache.log4j
                    log4j
                
                
                    log4j
                    log4j
                
                
                    slf4j-log4j12
                    org.slf4j
                
            
        
    
    
        
            
                org.apache.maven.plugins
                maven-compiler-plugin
                
                    ${java.version}
                    ${java.version}
                    UTF-8
                
            
            
                org.apache.maven.plugins
                maven-resources-plugin
                
                    UTF-8
                
            
        
    


具体是Servic的实现类和Controller层怎么使用看本章节最后面 有具体的代码演示。
具体是Servic的实现类和Controller层怎么使用看本章节最后面 有具体的代码演示。

operation-service
SpringBoot+Dubbo+zookeeper+redis 搭建一次让你永不忘记。_第2张图片
operation-service 的pom.xml



    4.0.0
    
        com.*.parent
        operation-parent
        0.0.1-SNAPSHOT
        pom.xml
    
    com.*.service
    operation-service
    0.0.1-SNAPSHOT
    jar
    operation-service
    Demo project for Spring Boot
    
        UTF-8
        UTF-8
        1.8
        0.10
    
    
        
            org.springframework.boot
            spring-boot-starter
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        

        
            org.springframework.boot
            spring-boot-starter-aop
        
        
        
            com.alibaba
            dubbo
            2.6.1
        
        
            com.alibaba.spring.boot
            dubbo-spring-boot-starter
            2.0.0
            
                
                    com.alibaba
                    dubbo
                
            
        
        
        
            com.alibaba
            dubbo-registry-zookeeper
            2.6.1
            
                
                    commons-logging
                    commons-logging
                
                
                    org.apache.log4j
                    log4j
                
                
                    log4j
                    log4j
                
                
                    slf4j-log4j12
                    org.slf4j
                
            
        

        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            1.3.1
        
        
            org.springframework.boot
            spring-boot-starter-jdbc
        
        
            mysql
            mysql-connector-java
            5.1.30
        
        
        
            com.alibaba
            druid
            1.0.11
        
        
            org.apache.commons
            commons-lang3
            3.3.2
        
        
            com.alibaba
            fastjson
            1.2.35
        
        
            org.jetbrains
            annotations
            13.0
        
        
            org.bouncycastle
            bcprov-jdk15on
            1.60
        
        
            junit
            junit
            4.12
        
        
            commons-codec
            commons-codec
            1.11
        
        
        
            org.springframework.boot
            spring-boot-starter-data-redis
        
        
            redis.clients
            jedis
            2.9.3
        
        
            org.apache.commons
            commons-lang3
        
    
    
        
            
                src/main/java
                
                    **/*.xml
                
                true
            
        
        
            
            
                org.mybatis.generator
                mybatis-generator-maven-plugin
                1.3.2
                
                    true
                    true
                
            
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    


具体是Servic的实现类和Controller层怎么使用看本章节最后面 有具体的代码演示。
具体是Servic的实现类和Controller层怎么使用看本章节最后面 有具体的代码演示。

operation-service 下的 resources

application.yml

logging:
  pattern:
    file: "my.log.%d{yyyy-MM-dd}.log"
    console: "%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n"
  path: "./logs"
  file:
    max-history: 30
    max-size: 10MB
  level:
   com.xattit.service: DEBUG


server:
  port: 8081

spring:
  datasource:
    name: test
    url: jdbc:mysql://192.168.*.70:3306/aa?useUnicode=true&characterEncoding=UTF-8&useSSL=false
    username: root
    password: root
    # 使用druid数据源
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: com.mysql.jdbc.Driver
    filters: stat
    maxActive: 20
    initialSize: 1
    maxWait: 60000
    minIdle: 1
    timeBetweenEvictionRunsMillis: 60000
    minEvictableIdleTimeMillis: 300000
    validationQuery: select 'x'
    testWhileIdle: true
    testOnBorrow: false
    testOnReturn: false
    poolPreparedStatements: true
    maxOpenPreparedStatements: 20

  dubbo:
    application:
      name: operation-service
    registry:
      address: zookeeper://127.0.0.1:2181
    protocol:
      name: dubbo
      port: 20880
      timeout: 600000
    provider:
      retries: 0
    scan: com.aa.service.*


  redis:
    host: 192.168.23.107
    port: 6379
    max-idle: 5
    max-total: 10
    max-wait-millis: 3000
  aop:
    proxy-target-class: true

mybatis:
  basePackage: com.xattit.aa.api.dao
  mapper-locations: classpath*:com/aa/service/api/mapper/*.xml
  type-aliases-package: com.aa.service.api.mapper

具体是Servic的实现类和Controller层怎么使用看本章节最后面 有具体的代码演示。
具体是Servic的实现类和Controller层怎么使用看本章节最后面 有具体的代码演示。

接下来是看我们的启动类

@SpringBootApplication(scanBasePackages = {"com.aa.service"})
@EnableDubboConfiguration   //很重要 Dubbo
@MapperScan("com.aa.service.api.mapper")
public class OperationServiceApplication {

    public static void main(String[] args) {
        SpringApplication.run(OperationServiceApplication.class, args);

        System.out.println("服务端启动成功!!!");

        try {
            System.in.read();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    

具体是Servic的实现类和Controller层怎么使用看本章节最后面 有具体的代码演示。
具体是Servic的实现类和Controller层怎么使用看本章节最后面 有具体的代码演示。

接下来就是 最外层 (Consumer)
operation-web pom.xml



    4.0.0
    
        com.aa.parent
        operation-parent
        0.0.1-SNAPSHOT
        pom.xml
    
    com.aa.client
    operation-client
    0.0.1-SNAPSHOT
    jar
    operation-client
    Demo project for Spring Boot
    
        UTF-8
        UTF-8
        1.8
        0.10
    
    
        
            com.aa.service
            operation-service
            0.0.1-SNAPSHOT
        
        
            org.springframework.boot
            spring-boot-starter
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
            com.alibaba.spring.boot
            dubbo-spring-boot-starter
            2.0.0
            
                
                    com.alibaba
                    dubbo
                
            
        
        
        
            com.alibaba
            dubbo-registry-zookeeper
            2.6.1
            
                
                    commons-logging
                    commons-logging
                
                
                    org.apache.log4j
                    log4j
                
                
                    log4j
                    log4j
                
                
                    slf4j-log4j12
                    org.slf4j
                
            
        
        
            junit
            junit
            4.12
        
    
    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    


operation-web 下的 resources 配置文件

application.yml

logging:
  pattern:
    file: "my.log.%d{yyyy-MM-dd}.log"
    console: "%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n"
  path: "./logs"
  file:
    max-history: 30
    max-size: 10MB
  level:
    com.xattit.client: DEBUG


server:
  port: 8082
  servlet:
    context-path: /operation

spring:
  dubbo:
    application:
      name: operation-client
      timeout: 600000
      logger: slf4j
    registry:
      address: zookeeper://192.168.23.108:2181
    scan: com.aa.client.*

具体是Servic的实现类和Controller层怎么使用看本章节最后面 有具体的代码演示。
具体是Servic的实现类和Controller层怎么使用看本章节最后面 有具体的代码演示。

启动类


@SpringBootApplication(scanBasePackages = {"com.xattit.service.exception","com.aa.service.jedis","com.aa.service.utils","com.aa.client.controller"},exclude = DataSourceAutoConfiguration.class)
@EnableDubboConfiguration
public class OperationClientApplication {

    public static void main(String[] args) {
        SpringApplication.run(OperationClientApplication.class, args);
    }

}

接下来我们看看怎么使用的 (Provider)

Dao层接口上记得加

@Mapper
public interface SysDeviceMapper {
}

Pojo的所有类 记得实现序列化

public class SysDevice implements Serializable {
}  

ServiceImpl类-注解
@Service(version = “0.0.1”) 是Dubbo的注解 其他的是Spring的

@Service(version = "0.0.1")
@Component
@org.springframework.stereotype.Service
public class DeviceServiceImpl implements DeviceService {
	@Autowired
    private SysDeviceMapper deviceMapper;
    
    @Override
    public String getDeviceByName(String name) {
    	return "hi:"+name;
    }
}

服务的消费者 (Consumer)
Cotroller
@Reference(version = “0.0.1”) Dubbo 的注解

@RestController
@Component
@RequestMapping(value = "/v1/operation")
@Slf4j
public class DeviceController {

    @Reference(version = "0.0.1")
    private DeviceService deviceService;
    @GetMapping(value = "/name")
    public String getDeiceByName(@RequestParam String name) {
    
    	return  deviceService.getDeviceByName(name);
    }
}

SpringBoot集成Dubbo的使用一个注解差不多就行了; @EnableDubboConfiguration

不了解的可以咨询
SpringBoot+Dubbo+zookeeper+redis 搭建一次让你永不忘记。_第3张图片

你可能感兴趣的:(架构)