10分鐘搞定Spring Boot + Dubbo + Dubbo admin UI一整套分布式解決方案
本機(jī)環(huán)境
JDK
: 1.8.0_161
Spring Boot
: 2.1.3.RELEASE
Zookeeper
: 3.4.11
org.apache.dubbo
: 2.7.0
Node
: v8.4.0
Npm
: 5.3.0
開(kāi)始搭建
Dubbo-Api
提供給外部使用
新建一個(gè)Dubbo-api 供consumer 和 provider使用
僅僅包含一個(gè)HelloService
public interface HelloService {
String sayHello(String name);
}
Dubbo-Provider
pom依賴
<properties>
<java.version>1.8</java.version>
<spring-boot.version>2.1.3.RELEASE</spring-boot.version>
<dubbo.version>2.7.0</dubbo.version>
</properties>
<dependencyManagement>
<dependencies>
<!-- Spring Boot -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- Apache Dubbo -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-dependencies-bom</artifactId>
<version>${dubbo.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
<version>${dubbo.version}</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
</exclusion>
<exclusion>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
</exclusion>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!-- Dubbo Spring Boot Starter -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.7.0</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<exclusions>
<exclusion>
<artifactId>log4j</artifactId>
<groupId>log4j</groupId>
</exclusion>
<exclusion>
<artifactId>slf4j-log4j12</artifactId>
<groupId>org.slf4j</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
</dependency>
</dependencies>
這些依賴都缺一不可,接下來(lái)就實(shí)現(xiàn)dubbo-api提供的Service方法即可
import com.skrein.dubbo.api.service.HelloService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
// 指定dubbo-provider的版本
@Service(version = "${demo.service.version}")
@Component
public class HelloServiceImpl implements HelloService {
@Value("${server.port}")
String port;
@Value("${spring.application.name}")
String appName;
@Override
public String sayHello(String name) {
return appName + ":" + port + "\n hello " + name;
}
}
@Service
: 這個(gè)注解是org.apache.dubbo提供的
然后BootApplication
設(shè)置@EnableDubbo 即可完成一個(gè)最簡(jiǎn)單的Provider
@SpringBootApplication
@EnableDubbo
public class BootApplication {
public static void main(String[] args) {
SpringApplication.run(BootApplication.class, args);
}
}
配置文件配置通過(guò)Zookeeper
作為我們的注冊(cè)中心:
application.yml
dubbo:
registry:
address: zookeeper://127.0.0.1:2181 # 注冊(cè)中心地址
protocol:
port: -1 # 隨機(jī)端口號(hào),我們等會(huì)會(huì)通過(guò)不同端口起2臺(tái)Provider,所以使用隨機(jī)端口
name: dubbo
metadata-report:
address: zookeeper://127.0.0.1:2181 # dubbo-admin需要這個(gè)配置上報(bào)meta信息
demo:
service:
version: 0.0.1
spring:
application:
name: dubbo-provider
main:
allow-bean-definition-overriding: true # 不設(shè)置會(huì)與Spring Boot沖突
然后我們通過(guò)命令行啟動(dòng)
$ java -jar target/dubbo-provider-0.0.1.jar --server.port=9000
$ java -jar target/dubbo-provider-0.0.1.jar --server.port=9000
Dubbo-Consumer
pom配置同Dubbo-provider, Dubbo-Consumer
這里作為一個(gè)Web服務(wù)
@RestController
public class HelloController {
@Reference(version = "${demo.service.version}")
private HelloService helloService;
@GetMapping("/hello/{name}")
public String hello(@PathVariable(name = "name") String name) {
return helloService.sayHello(name);
}
}
啟動(dòng)類加上@EnableDubbo
即可
application.yml
:
dubbo:
registry:
address: zookeeper://127.0.0.1:2181 # Zookeeper注冊(cè)中心地址
protocol:
port: -1 # 隨機(jī)端口
name: dubbo # dubbo協(xié)議名字
spring:
main:
allow-bean-definition-overriding: true # 不設(shè)置會(huì)與Spring Boot沖突
application:
name: dubbo-consumer
demo:
service:
version: 0.0.1 # 消費(fèi)者版本與生產(chǎn)者版本一致
spring.main.allow-bean-definition-overriding=true
: Spring Boot 2.7.0 Environment Beans conflict in Spring Boot
命令行啟動(dòng):
$ java -jar target/dubbo-consumer-0.0.1.jar
啟動(dòng)之后如下:
配置Dubbo-admin Dubbo-admin-ui
dubbo-admin-server
官網(wǎng)地址 Dubb-admin-UI
把項(xiàng)目下載下來(lái)馁害,使用IDEA打開(kāi)
首先啟動(dòng)dubbo-admin-server
,注意這里的端口號(hào)预吆,可能會(huì)與dubbo-consumer
沖突,我們這里修改
dubbo-admin-server
: 把端口號(hào)修改為8888
--server.port=8888
然后啟動(dòng)就行了。
dubbo-admin-ui
前端使用Vue,所以需要Node環(huán)境
推薦一個(gè)我常用的node管理: NVM
NVM-windows: https://github.com/coreybutler/nvm-windows
安裝完畢
$ nvm install 8.4.0
$ nvm use 8.4.0
nvm 安裝node如下:
然后切換到dubbo-admin-ui這個(gè)目錄下:
執(zhí)行 npm install
$ npm install
我本機(jī)已經(jīng)執(zhí)行過(guò),沒(méi)有執(zhí)行過(guò)的等待安裝:
你們還記得赦役,admin-server
的端口號(hào)為 8888
,因此我們需要把dubbo-admin-ui
連接server的端口號(hào)也要修改(默認(rèn)是8080
)
修改 config/index.js
然后啟動(dòng)dubbo-admin-ui
:
$ cnpm run dev
訪問(wèn) http://localhost:8081
可以看到我們的服務(wù)已經(jīng)被admin監(jiān)控了想暗。
點(diǎn)擊詳情,也能看到我們的2個(gè)provider也是在監(jiān)控當(dāng)中的:
代碼已經(jīng)上傳至我的github,歡迎star,后續(xù)會(huì)持續(xù)更新妇汗,為你帶來(lái)分布式,高并發(fā)等等詳細(xì)例子说莫。