最后
每年转战互联网行业的人很多,说白了也是冲着高薪去的,不管你是即将步入这个行业还是想转行,学习是必不可少的。作为一个Java开发,学习成了日常生活的一部分,不学习你就会被这个行业淘汰,这也是这个行业残酷的现实。
如果你对Java感兴趣,想要转行改变自己,那就要趁着机遇行动起来。或许,这份限量版的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、缓存、消息中间件、源码等相关面试题。
(img-PGd0QD2w-1715008074080)]
还有专门针对JVM、SPringBoot、SpringCloud、数据库、Linux、缓存、消息中间件、源码等相关面试题。
[外链图片转存中…(img-zYtXPgZf-1715008074080)]