Dubbo专栏:SpringBoot整合Dubbo服务降级与豪猪断路器一

Posted 园码生活

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Dubbo专栏:SpringBoot整合Dubbo服务降级与豪猪断路器一相关的知识,希望对你有一定的参考价值。

SpringBoot整合Dubbo与豪猪断路器,必须了解什么是服务降级?

      当服务器压力剧增的情况下,根据实际业务情况及流量,对一些服务和页面有策略的不处理或换种简单的方式处理,从而释放服务器资源以保证核心交易正常运作或高效运作。

工程目录


创建maven的坐标

<?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> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.4.RELEASE</version> <relativePath /> <!-- lookup parent from repository --> </parent> <modelVersion>4.0.0</modelVersion>
<artifactId>springboot-dubbo-service-provider</artifactId>
<packaging>jar</packaging>
<name>boot-user-service-provider</name> <description>Demo project for Spring Boot</description>


<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties>
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency>
<dependency> <groupId>com.example</groupId> <artifactId>iterface-common</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <dependency> <groupId>com.alibaba.boot</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>0.2.0</version> </dependency>
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency>

<dependency> <groupId>org.springframework.cloud</groupId> <artifactId> spring-cloud-starter-netflix-hystrix </artifactId> </dependency> </dependencies>
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>

<dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Finchley.SR1</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
</project>

添加配置文件

application.yml

#dubbo.application.name=user-service-provider
#dubbo.registry.address=127.0.0.1:2181
#dubbo.registry.protocol=zookeeper
#
#dubbo.protocol.name=dubbo
##dubbo.protocol.port=20881
#
#dubbo.monitor.protocol=registry
##dubbo.scan.base-packages=com.atguigu.gmall


dubbo.properties

dubbo.protocol.port=20882

provider.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!-- 1、指定当前服务/应用的名字(同样的服务名字相同,不要和别的服务同名) -->
<dubbo:application name="springboot-dubbo-service-provider"></dubbo:application>

<!-- 2、指定注册中心的位置 -->
<!-- <dubbo:registry address="zookeeper://127.0.0.1:2181"></dubbo:registry> -->
<dubbo:registry protocol="zookeeper" address="127.0.0.1:2181"></dubbo:registry>

<!-- 3、指定通信规则(通信协议?通信端口) -->
<dubbo:protocol name="dubbo" port="20882"></dubbo:protocol>

<!-- 4、暴露服务 ref:指向服务的真正的实现对象 -->
<dubbo:service interface="com.dubbo.common.service.UserService"
ref="userServiceImpl01" timeout="1000" version="1.0.0">
<dubbo:method name="getUserAddressList" timeout="1000"></dubbo:method>
</dubbo:service>

<!--统一设置服务提供方的规则 -->
<dubbo:provider timeout="1000"></dubbo:provider>


<!-- 服务的实现 -->
<bean id="userServiceImpl01" class="com.yang.dubbo.service.impl.UserServiceImpl"></bean>


<!-- 连接监控中心 -->
<dubbo:monitor protocol="registry"></dubbo:monitor>

</beans>

创建配置类

package com.yang.dubbo.config;
import java.util.ArrayList;
import java.util.List;
import com.dubbo.common.service.UserService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.alibaba.dubbo.config.ApplicationConfig;
import com.alibaba.dubbo.config.MethodConfig;
import com.alibaba.dubbo.config.MonitorConfig;
import com.alibaba.dubbo.config.ProtocolConfig;
import com.alibaba.dubbo.config.ProviderConfig;
import com.alibaba.dubbo.config.RegistryConfig;
import com.alibaba.dubbo.config.ServiceConfig;
@Configuration
public class MyDubboConfig {

@Bean
public ApplicationConfig applicationConfig() {
ApplicationConfig applicationConfig = new ApplicationConfig();
applicationConfig.setName("springboot-dubbo-service-provider");
return applicationConfig;
}

//<dubbo:registry protocol="zookeeper" address="127.0.0.1:2181"></dubbo:registry>
@Bean
public RegistryConfig registryConfig() {
RegistryConfig registryConfig = new RegistryConfig();
registryConfig.setProtocol("zookeeper");
registryConfig.setAddress("127.0.0.1:2181");
return registryConfig;
}

//<dubbo:protocol name="dubbo" port="20882"></dubbo:protocol>
@Bean
public ProtocolConfig protocolConfig() {
ProtocolConfig protocolConfig = new ProtocolConfig();
protocolConfig.setName("dubbo");
protocolConfig.setPort(20882);
return protocolConfig;
}

/**
*<dubbo:service interface="com.atguigu.gmall.service.UserService"
ref="userServiceImpl01" timeout="1000" version="1.0.0">
<dubbo:method name="getUserAddressList" timeout="1000"></dubbo:method>
</dubbo:service>
*/
@Bean
public ServiceConfig<UserService> userServiceConfig(UserService userService){
ServiceConfig<UserService> serviceConfig = new ServiceConfig<>();
serviceConfig.setInterface(UserService.class);
serviceConfig.setRef(userService);
serviceConfig.setVersion("1.0.0");

//配置每一个method的信息
MethodConfig methodConfig = new MethodConfig();
methodConfig.setName("getUserAddressList");
methodConfig.setTimeout(1000);

//method的设置关联到service配置中
List<MethodConfig> methods = new ArrayList<>();
methods.add(methodConfig);
serviceConfig.setMethods(methods);

//ProviderConfig
//MonitorConfig

return serviceConfig;
}

}

service层

package com.yang.dubbo.service.impl;
import java.util.Arrays;
import java.util.List;
import com.dubbo.common.bean.UserAddress;
import com.dubbo.common.service.UserService;
import org.springframework.stereotype.Component;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
@Service//暴露服务
@Component
public class UserServiceImpl implements UserService {

@HystrixCommand
@Override
public List<UserAddress> getUserAddressList(String userId) {
// TODO Auto-generated method stub
System.out.println("UserServiceImpl..3.....");
UserAddress address1 = new UserAddress(1, "北京市昌平区宏福科技园综合楼3", "1", "李老师", "010-56253825", "Y");
UserAddress address2 = new UserAddress(2, "深圳市宝安区西部硅谷大厦B3层(深圳分校)", "1", "王老师", "010-56253825", "N");
// try {
// Thread.sleep(2000);
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
if(Math.random()>0.5) {
throw new RuntimeException();
}
return Arrays.asList(address1,address2);
}

}

启动类

package com.yang.dubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
/**
* 1、导入依赖;
* 1)、导入dubbo-starter
* 2)、导入dubbo的其他依赖
* @author lfy
*
* SpringBootdubbo整合的三种方式:
* 1)、导入dubbo-starter,在application.properties配置属性,使用@Service【暴露服务】使用@Reference【引用服务】
* 2)、保留dubbo xml配置文件;
* 导入dubbo-starter,使用@ImportResource导入dubbo的配置文件即可
* 3)、使用注解API的方式:
* 将每一个组件手动创建到容器中,dubbo来扫描其他的组件
*/
//@EnableDubbo //开启基于注解的dubbo功能
//@ImportResource(locations="classpath:provider.xml")
@EnableDubbo(scanBasePackages="com.yang.dubbo")
@EnableHystrix //开启服务容错
@SpringBootApplication
public class BootUserServiceProviderApplication {

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


以上是关于Dubbo专栏:SpringBoot整合Dubbo服务降级与豪猪断路器一的主要内容,如果未能解决你的问题,请参考以下文章

dubbo入门学习-----dubbo整合springboot

springboot整合dubbo

Dubbo学习 —— SpringBoot 整合Dubbo

Dubbo学习 —— SpringBoot 整合Dubbo

SpringBoot与Dubbo整合-项目搭建

SpringBoot整合Dubbo案例