端午節趁着假期熟悉一下spring boot 2.0 整合 Dubbo 實現如下:
1.工程的結構如下圖:
工程說明如下:
1.dubbo 工程爲父工程
2.service-api 爲 接口定義工程。
3.service-api-imp 爲接口實現工程
4.work-service 爲接口調用工程
Dbubbo工程實現如下:
pom.xml 的實現如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.hou</groupId>
<artifactId>dubbo</artifactId>
<version>1.0.0</version>
<name>dubbo</name>
<description>Demo project for Spring Boot</description>
<modules>
<module>service-api</module>
<module>service-api-imp</module>
<module>work-service</module>
</modules>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<curator-framework.version>4.0.1</curator-framework.version>
<zookeeper.version>3.4.13</zookeeper.version>
<dubbo.starter.version>0.2.1.RELEASE</dubbo.starter.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>${dubbo.starter.version}</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>${curator-framework.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.alibaba/dubbo -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.6.6</version>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>4.1.44.Final</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.jboss.netty/netty -->
<dependency>
<groupId>org.jboss.netty</groupId>
<artifactId>netty</artifactId>
<version>3.2.5.Final</version>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>${zookeeper.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2. service-api 工程如下:
1.接口類的實現如下:
package com.hou.service.api.User;
public interface IUserService {
public String getUserInfo();
}
2.pom.xml 如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.hou</groupId>
<artifactId>dubbo</artifactId>
<version>1.0.0</version>
</parent>
<groupId>com.hou</groupId>
<artifactId>service-api</artifactId>
<version>1.0.0</version>
<name>service-api</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
service-api-imp 工程如下:
1..接口類的實現如下:
package com.hou.api.imp.user;
import com.hou.service.api.User.IUserService;
import com.alibaba.dubbo.config.annotation.Service;
@Service
public class UserService implements IUserService {
@Override
public String getUserInfo() {
return "我是:houxian1103";
}
}
2.pom.xml的實現如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.hou</groupId>
<artifactId>dubbo</artifactId>
<version>1.0.0</version>
</parent>
<groupId>com.hou</groupId>
<artifactId>service-api-imp</artifactId>
<version>1.0.0</version>
<name>service-api-imp</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>com.hou</groupId>
<artifactId>service-api</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
3.application.yml的實現如下:
server:
port: 8080
dubbo:
application:
name: usermgr
protocol:
name: dubbo
port: 20880
registry:
address: zookeeper://106.13.99.25:2181
scan:
base-packages: com.hou.api.impl
spring:
main:
allow-bean-definition-overriding: true
work-service工程如下:
1.接口調用實現:
package com.hou.work.controller;
import com.alibaba.dubbo.config.annotation.Reference;
import com.hou.service.api.User.IUserService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class workController {
@Reference
private IUserService userService;
@RequestMapping("/getUserInfo")
public String getUserInfo() {
return userService.getUserInfo();
}
}
2.pom.xml 的實現如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.hou</groupId>
<artifactId>dubbo</artifactId>
<version>1.0.0</version>
</parent>
<groupId>com.hou</groupId>
<artifactId>work-service</artifactId>
<version>1.0.0</version>
<name>work-service</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>com.hou</groupId>
<artifactId>service-api</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
3.application.yml的實現如下:
server:
port: 8081
###dubbo 註冊服務名稱
dubbo:
application:
name: work
###dubbo服務地址
registry:
address: zookeeper://106.13.99.25:2181
consumer:
timeout: 5000
spring:
main:
allow-bean-definition-overriding: true
上述就是使用zookeeper 實現負載均衡dubbo +spring boot 的整合過程。
其中要幾個要注意的點:
1. service-api-imp 工程 中的實現類中,@Service 爲 dubbo中的註解。
import com.alibaba.dubbo.config.annotation.Service;
@Service
public class UserService implements IUserService {
2.yml 配置中,要求引用:
spring:
main:
allow-bean-definition-overriding: true