pretty-testing 测试技术、功能 ribbon、feign、hystrix

pom.xml

<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>
  <parent>
    <groupId>com.pretty</groupId>
    <artifactId>pretty-parent</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <artifactId>pretty-testing</artifactId>
  <dependencies>
  	 <dependency>
	    <groupId>pretty-common</groupId>
        <artifactId>pretty-common</artifactId>
	    <version>0.0.1-SNAPSHOT</version>
	 </dependency>
  	 <dependency>
	    <groupId>org.springframework.cloud</groupId>
	    <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
	 </dependency>
	 <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
     </dependency>
	 <dependency>
	    <groupId>org.springframework.cloud</groupId>
	    <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
	 </dependency>
	 <dependency>
	    <groupId>org.springframework.cloud</groupId>
	    <artifactId>spring-cloud-starter-openfeign</artifactId>
	 </dependency>
	 <dependency>
	    <groupId>org.springframework.cloud</groupId>
	    <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
	 </dependency>
	 <dependency>
	    <groupId>org.springframework.cloud</groupId>
	    <artifactId>spring-cloud-starter-netflix-zuul</artifactId>
	 </dependency>
	 <dependency>
	    <groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-thymeleaf</artifactId>
     </dependency>
     <!-- 安全控制 -->
     <dependency>
	    <groupId>org.springframework.boot</groupId>
	    <artifactId>spring-boot-starter-security</artifactId>
	 </dependency>
	 <!-- SpringBoot开发工具 -->
	 <dependency>
	    <groupId>org.springframework.boot</groupId>
	    <artifactId>spring-boot-devtools</artifactId>
	    <optional>true</optional>
	 </dependency>
  </dependencies>
</project>

Feign熔断保护

UserFeign.java

package com.pretty.testing.feign;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;

import com.pretty.common.entity.Result;
import com.pretty.common.entity.User;

@FeignClient(name = "pretty-security", fallback=UserFeignHystrix.class)
public interface UserFeign {
	@RequestMapping("/user")
	public Result<User> user();
}

UserFeignHystrix.java 当Feign调用失败或超时会执行熔断保护的方法

package com.pretty.testing.feign;

import org.springframework.stereotype.Component;

import com.pretty.common.entity.Result;
import com.pretty.common.entity.User;

@Component
public class UserFeignHystrix implements UserFeign {
	@Override
	public Result<User> user() {
		Result<User> result = new Result<User>();
		result.setCode(400);
		result.setMessage("获取用户信息失败");
		return result;
	}
}

UserController.java

@Autowired
	UserFeign userFeign;
	@RequestMapping("/user")
	public Result<User> user() {
		return userFeign.user();
	}

启动类添加@EnableEurekaClient、@EnableFeignClients、@EnableHystrix注解

package com.pretty.testing;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@EnableDiscoveryClient
@EnableEurekaClient
@EnableFeignClients
//启用Hystrix 熔断器
@EnableHystrix
//启用Zuul 路由
@EnableZuulProxy
@SpringBootApplication
public class PrettyTestingApplication {
	public static void main(String[] args) {
		SpringApplication.run(PrettyTestingApplication.class, args);
	}
	@Bean
	@LoadBalanced
    public RestTemplate restTemplate() {
	    return new RestTemplate();
    }
}

application.xml 配置开启Feign熔断保护

# 服务名称
spring:
  application:
    name: pretty-testing
# 服务端口号
server:
  port: 9001
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:10001/eureka/,http://localhost:10000/eureka/
  instance:
    preferIpAddress: true
    instanceId: ${spring.cloud.client.ip-address}:${server.port}
#feign开启熔断保护
feign:
  hystrix:
    enabled: true

Ribbon熔断保护,建议使用Feign

UserController.java 中添加如下代码

RestTemplate restTemplate;
	public Result<?> getUserBack() {
	    Result<?> result = new Result<User>();
	    result.setCode(400);
	    result.setMessage("获取用户信息失败");
	    return result;
	}
	//方法添加熔断器
	@HystrixCommand(fallbackMethod = "getUserBack")//fallbackMethod 回调方法
	@RequestMapping("/getUser")
	public Result<?> getUser() {
		return restTemplate.getForObject("http://pretty-security/user", Result.class);
	}
	@RequestMapping("/getUserList")
	public Result<List<User>> getUserList() {
		ArrayList<User> list = new ArrayList<User>();
		list.add(new User("root","123456"));
		list.add(new User("pretty-testing","123456"));
		Result<List<User>> result = new Result<List<User>>();
		result.setCode(200);
		result.setData(list);
		result.setMessage("获取用户列表成功");
		return result;
	}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值