2024年Spring Cloud入门教程-Hystrix断路器实现容错和降级(1),Java开发面试题及答案

本文介绍了Java开发者如何通过学习SpringCloud框架,实现服务注册与配置管理,并提供了SpringBoot和Hystrix的实践示例,同时涵盖了大厂Java面试题的相关内容,为求职者提供系统化的学习资源。
摘要由CSDN通过智能技术生成

最后

每年转战互联网行业的人很多,说白了也是冲着高薪去的,不管你是即将步入这个行业还是想转行,学习是必不可少的。作为一个Java开发,学习成了日常生活的一部分,不学习你就会被这个行业淘汰,这也是这个行业残酷的现实。

如果你对Java感兴趣,想要转行改变自己,那就要趁着机遇行动起来。或许,这份限量版的Java零基础宝典能够对你有所帮助。

本文已被CODING开源项目:【一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码】收录

需要这份系统化的资料的朋友,可以点击这里获取

org.springframework.cloud

spring-cloud-dependencies

Finchley.M9

pom

import

org.springframework.boot

spring-boot-maven-plugin

spring-milestones

Spring Milestones

https://repo.spring.io/libs-milestone

false

我们继续使用了spring-cloud-starter-netflix-eureka-client以使产品服务自动注册到eureka服务中。然后还使用了spring-cloud-starter-config读取配置服务中心的配置文件。这个项目只是一个简单的spring web项目。

src/main/resources下创建bootstrap.yml文件,添加如下内容:

spring:

application:

name: product-service

cloud:

config:

uri: http://localhost:8888

在配置中心的git仓库中创建product-service.yml文件 添加如下配置并提交:

server:

port: 8081

此配置指定了产品服务的端口为8081。接着创建Application类,添加如下代码:

package cn.zxuqian;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@EnableDiscoveryClient

@SpringBootApplication

public class Application {

public static void main(String[] args) {

SpringApplication.run(Application.class, args);

}

}

@EnableDiscoveryClient注解将指示spring cloud自动把本服务注册到eureka。最后创建cn.zxuqian.controllers.ProductController控制器,提供/products API,返回示例数据:

package cn.zxuqian.controllers;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

@RestController

public class ProductController {

@RequestMapping(“/products”)

public String productList() {

return “外套,夹克,毛衣,T恤”;

}

}

配置Web客户端


打开我们之前创建的web项目,在pom.xml中新添Hystrix依赖:

org.springframework.cloud

spring-cloud-starter-netflix-hystrix

然后更新Application类的代码:

package cn.zxuqian;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.boot.web.client.RestTemplateBuilder;

import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;

import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

import org.springframework.context.annotation.Bean;

import org.springframework.web.client.RestTemplate;

@EnableCircuitBreaker

@EnableDiscoveryClient

@SpringBootApplication

public class Application {

public static void main(String[] args) {

SpringApplication.run(Application.class, args);

}

@Bean

public RestTemplate rest(RestTemplateBuilder builder) {

return builder.build();

}

}

这里使用@EnableCircuitBreaker来开启断路器功能,然后还添加了一个rest方法并使用@Bean注解。这部分属于Spring依赖注入功能,使用@Bean标记的方法将告诉如何初始化此类对象,比如本例中就是使用RestTemplateBuilder来创建一个RestTemplate的对象,这个稍后在使用断路器的service中用到。

创建cn.zxuqian.service.ProductService类,并添加如下代码:

package cn.zxuqian.services;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.cloud.client.ServiceInstance;

import org.springframework.cloud.client.discovery.DiscoveryClient;

import org.springframework.stereotype.Service;

import org.springframework.web.client.RestTemplate;

import java.util.List;

@Service

public class ProductService {

private final RestTemplate restTemplate;

@Autowired

private DiscoveryClient discoveryClient;

public ProductService(RestTemplate restTemplate) {

this.restTemplate = restTemplate;

}

@HystrixCommand(fallbackMethod = “backupProductList”)

public String productList() {

List instances = this.discoveryClient.getInstances(“product-service”);

if(instances != null && instances.size() > 0) {

return this.restTemplate.getForObject(instances.get(0).getUri() + “/products”, String.class);

}

return “”;

}

最后

我还通过一些渠道整理了一些大厂真实面试主要有:蚂蚁金服、拼多多、阿里云、百度、唯品会、携程、丰巢科技、乐信、软通动力、OPPO、银盛支付、中国平安等初,中级,高级Java面试题集合,附带超详细答案,希望能帮助到大家。

新鲜出炉的蚂蚁金服面经,熬夜整理出来的答案,已有千人收藏

还有专门针对JVM、SPringBoot、SpringCloud、数据库、Linux、缓存、消息中间件、源码等相关面试题。

新鲜出炉的蚂蚁金服面经,熬夜整理出来的答案,已有千人收藏

本文已被CODING开源项目:【一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码】收录

需要这份系统化的资料的朋友,可以点击这里获取

(img-PGd0QD2w-1715008074080)]

还有专门针对JVM、SPringBoot、SpringCloud、数据库、Linux、缓存、消息中间件、源码等相关面试题。

[外链图片转存中…(img-zYtXPgZf-1715008074080)]

本文已被CODING开源项目:【一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码】收录

需要这份系统化的资料的朋友,可以点击这里获取

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值