SpringBoot整合Dubbo案例

使用框架:java

  1. jdk 1.8mysql

  2. springboot-2.1.3git

  3. dubbo-2.6github

  4. spring-data-jpa-2.1.5web

1、開發dubbo服務接口:

按照Dubbo官方開發建議,建立一個接口項目,該項目只定義接口和model類;spring

一、建立springboot工程 spring-boot-demo-dubbo-interfacesql

座標:apache

<groupId>com.example</groupId>
<artifactId>spring-boot-demo-dubbo-interface</artifactId>
<version>0.0.1-SNAPSHOT</version>
複製代碼

添加spring-data-jpa 依賴:springboot

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
複製代碼

二、建立modelbash

package com.example.demo.model;

@Entity
public class User implements Serializable{
    private static final long serialVersionUID = 1L;
    
    @Id @GeneratedValue
    private long id;
    private String userName;
    private String password;
    private int age;
    public long getId() {
        return id;
    }
//省略set get 方法
複製代碼

三、建立接口:

package com.example.demo.service;

import com.example.demo.model.User;

public interface UserService {
    
    public void save(User user);
    
    public String sayHello(String word);
}
複製代碼

四、使用命令 clean install 打包安裝到maven倉庫。

阿里巴巴提供的dubbo集成springboot開源項目;

參考文檔:

github.com/apache/dubb…

本工程採用該項目的jar包進行繼承:

<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>0.2.0</version>
</dependency>
複製代碼

2、開發dubbo服務提供者:

一、建立一個Springboot項目spring-boot-demo-dubbo-provider並配置好相關的依賴;

pom.xml

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        
        
        <!-- 加入springboot與dubbo集成的起步依賴 -->
        <dependency>
            <groupId>com.alibaba.boot</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>0.2.0</version>
        </dependency>

        <!-- 因爲使用了zookeeper做爲註冊中心,則須要加入zookeeper的客戶端jar包: -->
        <dependency>
            <groupId>com.101tec</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.10</version>
        </dependency>
        
        <!-- spring-data-jpa -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        
        <!-- 添加接口服務 -->
        <dependency>
            <groupId>com.example</groupId>
            <artifactId>spring-boot-demo-dubbo-interface</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>
複製代碼

二、在Springboot的核心配置文件application.properties中配置dubbo的信息:

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/test?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=true
spring.datasource.username=root
spring.datasource.password=root

spring.jpa.properties.hibernate.hbm2ddl.auto=create
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.show-sql=true

# 訪問端口
server.port=8080
# dubbo配置
dubbo.application.name=springboot-dubbo-provider
dubbo.registry.address=zookeeper://192.168.146.128:2181
複製代碼

三、開發編寫Dubbo的接口實現類:

package com.example.demo.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.dubbo.config.annotation.Service;
import com.example.demo.model.User;
import com.example.demo.repository.UserRepository;

@Component //註冊爲spring bean
@Service // 這注解是dubbo提供的 
public class UserServiceImpl implements UserService {
    
    @Autowired
    private UserRepository userRepository;

    @Override
    public void save(User user) {
        userRepository.save(user);
    }

    @Override
    public String sayHello(String word) {
        return word;
    }
}
複製代碼

四、入口main程序啓動Dubbo服務提供者:添加註解 @EnableDubbo

package com.example.demo;

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

import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;

@SpringBootApplication
@EnableDubbo
public class SpringBootDemoDubboProviderApplication {

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

}
複製代碼

啓動main ,服務發佈到zookeeper 註冊中心。

3、開發dubbo服務消費者:

一、建立一個Springboot項目spring-boot-demo-dubbo-consumer並配置好相關的依賴;

二、加入springboot與dubbo集成的起步依賴:(pom.xml 配置同上)

注意: 服務提供者 和 消費者都要配置 服務接口依賴

三、在Springboot的核心配置文件application.properties中配置dubbo的信息:

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/test?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=true
spring.datasource.username=root
spring.datasource.password=root

# WEB\u670D\u52A1\u7AEF\u53E3
server.port=8081
# dubbo\u914D\u7F6E
dubbo.application.name=springboot-dubbo-consumer
dubbo.registry.address=zookeeper://192.168.146.128:2181 
複製代碼

四、編寫一個Controller類,調用遠程的Dubbo服務:

package com.example.demo.controller;

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

import com.alibaba.dubbo.config.annotation.Reference;
import com.example.demo.model.User;
import com.example.demo.service.UserService;

@RestController
public class UserController {
    
    @Reference //該註解是dubbo提供的
    private UserService userService;
    
    @RequestMapping("/say")
    public String sayHello(String name) {
        return userService.sayHello(name);
    }
    
    @RequestMapping("/save")
    public void save() {
        User u = new User();
        u.setAge(20);
        u.setPassword("123");
        u.setUserName("zheng");
        userService.save(u);
    }
}
複製代碼

五、啓動類添加 開啓dubbo 註解 @EnableDubbo

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;

@SpringBootApplication
@EnableDubbo
public class SpringBootDemoDubboConsumerApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpringBootDemoDubboConsumerApplication.class, args);
    }
}
複製代碼

六、啓動main方法。

七、調用遠程接口:

http://localhost:8081/say?name=hello

一個SpringBoot基於Dubbo的服務接口開發完成。

相關文章
相關標籤/搜索