创建服务接口项目
创建一个名为TestDubboLogin的项目,该项目只负责定义接口
POM
```
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.suoron</groupId>
<artifactId>DubboLoadBalance</artifactId>
<version>1.0.0-SNAPSHOT</version>
<modules>
<module>testdubbo-interface</module>
<module>testdubbo-client</module>
<module>testdubbo-server</module>
</modules>
<packaging>pom</packaging>
<!-- 继承父包 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.2.RELEASE</version>
</parent>
</project>
```
创建3个模块(login-server、login-client、login-interface)
定义服务接口 interface
```
package com.suoron.dubbo.api.service;
/**
* 用户相关接口
*/
public interface IUserService {
public String login(String username,String password);
}
```
创建服务提供者项目
创建一个名为login-server的项目,该项目主要用于实现接口
POM
```
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>DubboLoadBalance</artifactId>
<groupId>com.suoron</groupId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>testdubbo-server</artifactId>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<dubbo-spring-boot>1.0.0</dubbo-spring-boot>
<log4j.version>1.2.17</log4j.version>
</properties>
<dependencies>
<!-- qos server依赖 -->
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>4.1.30.Final</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.8</version>
</dependency>
<!-- 非WEB启动器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!-- 健康检查、审计、统计和监控 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- dubbo启动器 -->
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>0.2.0</version>
</dependency>
<!-- dubbo健康检查 -->
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-actuator</artifactId>
<version>0.2.0</version>
</dependency>
<!-- 接口jar -->
<dependency>
<groupId>com.suoron</groupId>
<artifactId>testdubbo-interface</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
```
主要增加了以下依赖:
com.alibaba.boot:dubbo-spring-boot-starter:0.2.0:Dubbo Starter,0.2.0 版本支持 Spring Boot 2.x,是一个长期维护的版本。注:0.1.0 版本已经不推荐使用了,是个短期维护的版本,如果你还在用旧版,请大家尽快升级。
com.alibaba.boot:dubbo-spring-boot-actuator:0.2.0:Dubbo 的服务状态检查
通过@Service注解实现服务提供方
```
import com.alibaba.dubbo.config.annotation.Service;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.suoron.dubbo.api.entity.UserEntity;
import com.suoron.dubbo.api.service.IUserService;
import org.springframework.beans.factory.annotation.Value;
import javax.xml.bind.annotation.XmlValue;
@Service(version = "1.0.0")
public class UserServiceImpl implements IUserService {
@Value("${dubbo.protocol.port}")
private String port;
@HystrixCommand(commandProperties = {
///一个统计窗口,10s/5s(目前)
@HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "4"),
///访问超时
@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "2000")
})
@Override
public String login(String username, String password) {
System.out.println(System.currentTimeMillis()+":收到客户端请求!!!");
if(true){ /* 测试触发熔断 */
throw new RuntimeException("test.....");
}
return "success:"+port;
}
```
Application
```
import com.alibaba.dubbo.container.Main;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableAutoConfiguration
@ComponentScan("com.suoron.dubbo.api.service")
@EnableHystrix
public class MyDubboServer {
public static void main(String[] args) {
SpringApplication.run(MyDubboServer.class,args);
// 启动 Provider 容器,注意这里的 Main 是 com.alibaba.dubbo.container 包下的
Main.main(args); //只需服务端设置
}
}
```
application.yml
dubbo protocol port 设置为-1会随机设置端口号(需要启动多个服务时)
如果端口报错,可尝试设置个服务端口
server:
port:8070
```
spring:
application:
name: login-provider
# Dubbo 属性配置
dubbo:
provider:
loadbalance: roundrobin //轮询
# 有@Service的包
scan:
basePackages: com.suoron.dubbo.api.service
## ApplicationConfig Bean dubbo全局唯一应用名(控制台可见)
application:
id: login-provider
name: login-provider
qosEnable: true
qosPort: 33333
qosAcceptForeignIp: true
## 服务监听接口21808
protocol:
id: dubbo
name: dubbo
port: 21808 //可以任意设置,但不要超过5000
status: server
serialization: kryo //快速序列化框架
optimizer: com.suoron.dubbo.api.common.SerializationOptimizerImpl //可以选择,但设置了效率更高,兼容更好(需要配置(下节))
## RegistryConfig Bean
registry:
id: zookeeper
#address: zookeeper://127.0.0.1:2181?backup=192.168.10.131:2182,192.168.10.131:2183 配置多个
address: zookeeper://10.3.133.160:2181
# Enables Dubbo All Endpoints
management:
endpoint:
dubbo:
enabled: true
dubbo-shutdown:
enabled: true
dubbo-configs:
enabled: true
dubbo-services:
enabled: true
dubbo-references:
enabled: true
dubbo-properties:
enabled: true
# Dubbo Health
health:
dubbo:
status:
## StatusChecker Name defaults (default : "memory", "load" )
defaults: memory
## StatusChecker Name extras (default : empty )
extras: load,threadpool
logging:
level:
root: info
```
消费者项目login-client
创建一个名为login-client的模块,该项目用于消费接口(调用接口)
POM
```
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>DubboLoadBalance</artifactId>
<groupId>com.suoron</groupId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>testdubbo-interface</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
<version>2.0.1.RELEASE</version>
</dependency>
<!-- Hystrix服务熔断 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
<version>2.0.1.RELEASE</version>
</dependency>
<dependency>
<groupId>de.javakaffee</groupId>
<artifactId>kryo-serializers</artifactId>
<version>0.42</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.6.2</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
```
通过@Reference注入IUserService
```
import com.alibaba.dubbo.config.annotation.Reference;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.suoron.dubbo.api.entity.UserEntity;
import com.suoron.dubbo.api.service.IUserService;
import com.suoron.dubbo.client.service.UserService;
import org.springframework.stereotype.Service;
@Service
public class UserServiceImpl implements UserService {
@Reference(version = "1.0.0")
public IUserService iUserService;
@Override
@HystrixCommand(fallbackMethod = "userLoginError")
public String userLogin(String user, String password) {
return iUserService.login(user,password);
}
public String userLoginError(String user, String password) {
return "网络故障,请再次请求!";
}
@Override
public UserEntity userLoginx(String user, String password) {
return iUserService.loginx(user,password);
}
}
``
注意客户端是实现的自己提供的方法
```
import com.suoron.dubbo.api.entity.UserEntity;
public interface UserService {
public String userLogin(String user,String password);
public UserEntity userLoginx(String user, String password);
}
```
Application
```
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableAutoConfiguration
@ComponentScan("com.suoron.dubbo.client.service")
@ComponentScan("com.suoron.dubbo.client.controller")
@ComponentScan("com.suoron.dubbo.client.config")
@EnableHystrix //Hystrix熔断注解
@EnableHystrixDashboard //Hystrix熔断监控
public class MyDubboClient {
public static void main(String[] args) {
SpringApplication.run(MyDubboClient.class,args);
}
}
```
application.yml
```
# Spring boot application
spring:
application:
name: login-consumer
server: //通常只需客户端设置
port: 8090
# Dubbo Config properties
dubbo:
# consumer:
# loadbalance: random
scan:
basePackages: com.suoron.client.service
## ApplicationConfig Bean
application:
id: login-consumer
name: login-consumer
## RegistryConfig Bean
registry:
id: zookeeper
address: zookeeper://10.3.133.160:2181
protocol:
serialization: kryox
optimizer: com.suoron.dubbo.api.common.SerializationOptimizerImplx
# Dubbo Endpoint (default status is disable)
endpoints:
dubbo:
enabled: true
##健康检查http://127.0.0.1:9091/actuator/health
management:
server:
port: 9091
# Dubbo Health
health:
dubbo:
status:
## StatusChecker Name defaults (default : "memory", "load" )
defaults: memory
# Enables Dubbo All Endpoints
endpoint:
dubbo:
enabled: true
dubbo-shutdown:
enabled: true
dubbo-configs:
enabled: true
dubbo-services:
enabled: true
dubbo-references:
enabled: true
dubbo-properties:
enabled: true
health:
show-details: always
endpoints:
web:
exposure:
include: "*"
```
启动 Dubbo Admin 控制台
查看是否成功注册服务,效果图如下:
Dubbo控制台
附: 参考资料
Spring boot 2.0 Actuator 的健康检查
作者:索伦x
链接:https://www.jianshu.com/p/801dc0f21573
来源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。