最后
码字不易,觉得有帮助的可以帮忙点个赞,让更多有需要的人看到
又是一年求职季,在这里,我为各位准备了一套Java程序员精选高频面试笔试真题,来帮助大家攻下BAT的offer,题目范围从初级的Java基础到高级的分布式架构等等一系列的面试题和答案,用于给大家作为参考
以下是部分内容截图
import io.grpc.ManagedChannelBuilder;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.util.Arrays;
import static com.google.common.base.Charsets.UTF_8;
/**
-
@author will (zq2599@gmail.com)
-
@version 1.0
-
@description: 包装了SimpleBlockingStub实例的类,发起gRPC请求时需要用到SimpleBlockingStub实例
-
@date 2021/5/8 19:34
*/
@Component(“stubWrapper”)
@Data
@Slf4j
@ConfigurationProperties(prefix = “grpc”)
public class StubWrapper {
/**
- 这是etcd中的一个key,该key对应的值是grpc服务端的地址信息
*/
private static final String GRPC_SERVER_INFO_KEY = “/grpc/local-server”;
/**
- 配置文件中写好的etcd地址
*/
private String etcdendpoints;
private SimpleGrpc.SimpleBlockingStub simpleBlockingStub;
/**
-
从etcd查询gRPC服务端的地址
-
@return
*/
public String[] getGrpcServerInfo() {
// 创建client类
KV kvClient = Client.builder().endpoints(etcdendpoints.split(“,”)).build().getKVClient();
GetResponse response = null;
// 去etcd查询/grpc/local-server这个key的值
try {
response = kvClient.get(ByteSequence.from(GRPC_SERVER_INFO_KEY, UTF_8)).get();
} catch (Exception exception) {
log.error(“get grpc key from etcd error”, exception);
}
if (null==response || response.getKvs().isEmpty()) {
log.error(“empty value of key [{}]”, GRPC_SERVER_INFO_KEY);
return null;
}
// 从response中取得值
String rawAddrInfo = response.getKvs().get(0).getValue().toString(UTF_8);
// rawAddrInfo是“192.169.0.1:8080”这样的字符串,即一个IP和一个端口,用":"分割,
// 这里用":"分割成数组返回
return null==rawAddrInfo ? null : rawAddrInfo.split(“:”);
}
/**
-
每次注册bean都会执行的方法,
-
该方法从etcd取得gRPC服务端地址,
-
用于实例化成员变量SimpleBlockingStub
*/
@PostConstruct
public void simpleBlockingStub() {
// 从etcd获取地址信息
String[] array = getGrpcServerInfo();
log.info(“create stub bean, array info from etcd {}”, Arrays.toString(array));
// 数组的第一个元素是gRPC服务端的IP地址,第二个元素是端口
if (null==array || array.length<2) {
log.error(“can not get valid grpc address from etcd”);
return;
}
// 数组的第一个元素是gRPC服务端的IP地址
String addr = array[0];
// 数组的第二个元素是端口
int port = Integer.parseInt(array[1]);
// 根据刚才获取的gRPC服务端的地址和端口,创建channel
Channel channel = ManagedChannelBuilder
.forAddress(addr, port)
.usePlaintext()
.build();
// 根据channel创建stub
simpleBlockingStub = SimpleGrpc.newBlockingStub(channel);
}
}
- GrpcClientService是封装了StubWrapper的服务类:
package com.bolingcavalry.dynamicrpcaddr;
import com.bolingcavalry.grpctutorials.lib.HelloReply;
import com.bolingcavalry.grpctutorials.lib.HelloRequest;
import com.bolingcavalry.grpctutorials.lib.SimpleGrpc;
import io.grpc.StatusRuntimeException;
import lombok.Setter;
import net.devh.boot.grpc.client.inject.GrpcClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class GrpcClientService {
@Autowired(required = false)
@Setter
private StubWrapper stubWrapper;
public String sendMessage(final String name) {
// 很有可能simpleStub对象为null
if (null==stubWrapper) {
return “invalid SimpleBlockingStub, please check etcd configuration”;
}
try {
final HelloReply response = stubWrapper.getSimpleBlockingStub().sayHello(HelloRequest.newBuilder().setName(name).build());
return response.getMessage();
} catch (final StatusRuntimeException e) {
return "FAILED with " + e.getStatus().getCode().name();
}
}
}
- 新增一个controller类GrpcClientController,提供一个http接口,里面会调用GrpcClientService的方法,最终完成远程gRPC调用:
package com.bolingcavalry.dynamicrpcaddr;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class GrpcClientController {
@Autowired
private GrpcClientService grpcClientService;
@RequestMapping(“/”)
public String printMessage(@RequestParam(defaultValue = “will”) String name) {
return grpcClientService.sendMessage(name);
}
}
- 接下来新增一个controller类RefreshStubInstanceController,对外提供一个http接口refreshstub,作用是删掉stubWrapper这个bean,再重新注册一次,这样每当外部调用refreshstub接口,就可以从etcd取得服务端信息再重新实例化SimpleBlockingStub成员变量,这样就达到了客户端动态获取服务端地址的效果:
package com.bolingcavalry.dynamicrpcaddr;
import com.bolingcavalry.grpctutorials.lib.SimpleGrpc;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class RefreshStubInstanceController implements ApplicationContextAware {
private ApplicationContext applicationContext;
@Autowired
private GrpcClientService grpcClientService;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
@RequestMapping(“/refreshstub”)
public String refreshstub() {
String beanName = “stubWrapper”;
//获取BeanFactory
DefaultListableBeanFactory defaultListableBeanFactory = (DefaultListableBeanFactory) applicationContext.getAutowireCapableBeanFactory();
// 删除已有bean
defaultListableBeanFactory.removeBeanDefinition(beanName);
//创建bean信息.
BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder.genericBeanDefinition(StubWrapper.class);
//动态注册bean.
defaultListableBeanFactory.registerBeanDefinition(beanName, beanDefinitionBuilder.getBeanDefinition());
// 更新引用关系(注意,applicationContext.getBean方法很重要,会触发StubWrapper实例化操作)
grpcClientService.setStubWrapper(applicationContext.getBean(StubWrapper.class));
return “Refresh success”;
}
}
- 编码完成,开始验证;
部署gRPC服务端应用
部署gRPC服务端应用很简单,启动local-server应用即可:
部署etcd
- 为了简化操作,我这里的etcd集群是用docker部署的,对应的docker-compose.yml文件内容如下:
version: ‘3’
services:
etcd1:
image: “quay.io/coreos/etcd:v3.4.7”
entrypoint: /usr/local/bin/etcd
command:
-
‘–name=etcd1’
-
‘–data-dir=/etcd_data’
-
‘–initial-advertise-peer-urls=http://etcd1:2380’
-
‘–listen-peer-urls=http://0.0.0.0:2380’
-
‘–listen-client-urls=http://0.0.0.0:2379’
-
‘–advertise-client-urls=http://etcd1:2379’
-
‘–initial-cluster-token=etcd-cluster’
-
‘–heartbeat-interval=250’
-
‘–election-timeout=1250’
-
‘–initial-cluster=etcd1=http://etcd1:2380,etcd2=http://etcd2:2380,etcd3=http://etcd3:2380’
-
‘–initial-cluster-state=new’
ports:
- 2379:2379
volumes:
- ./store/etcd1/data:/etcd_data
etcd2:
image: “quay.io/coreos/etcd:v3.4.7”
entrypoint: /usr/local/bin/etcd
command:
-
‘–name=etcd2’
-
‘–data-dir=/etcd_data’
-
‘–initial-advertise-peer-urls=http://etcd2:2380’
-
‘–listen-peer-urls=http://0.0.0.0:2380’
-
‘–listen-client-urls=http://0.0.0.0:2379’
-
‘–advertise-client-urls=http://etcd2:2379’
-
‘–initial-cluster-token=etcd-cluster’
-
‘–heartbeat-interval=250’
-
‘–election-timeout=1250’
-
‘–initial-cluster=etcd1=http://etcd1:2380,etcd2=http://etcd2:2380,etcd3=http://etcd3:2380’
-
‘–initial-cluster-state=new’
ports:
- 2380:2379
volumes:
- ./store/etcd2/data:/etcd_data
etcd3:
image: “quay.io/coreos/etcd:v3.4.7”
entrypoint: /usr/local/bin/etcd
command:
-
‘–name=etcd3’
-
‘–data-dir=/etcd_data’
-
‘–initial-advertise-peer-urls=http://etcd3:2380’
-
‘–listen-peer-urls=http://0.0.0.0:2380’
-
‘–listen-client-urls=http://0.0.0.0:2379’
-
‘–advertise-client-urls=http://etcd3:2379’
-
‘–initial-cluster-token=etcd-cluster’
-
‘–heartbeat-interval=250’
-
‘–election-timeout=1250’
-
‘–initial-cluster=etcd1=http://etcd1:2380,etcd2=http://etcd2:2380,etcd3=http://etcd3:2380’
最后
小编在这里分享些我自己平时的学习资料,由于篇幅限制,pdf文档的详解资料太全面,细节内容实在太多啦,所以只把部分知识点截图出来粗略的介绍,每个小节点里面都有更细化的内容!
开源分享:【一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码】
程序员代码面试指南 IT名企算法与数据结构题目最优解
这是” 本程序员面试宝典!书中对IT名企代码面试各类题目的最优解进行了总结,并提供了相关代码实现。针对当前程序员面试缺乏权威题目汇总这一-痛点, 本书选取将近200道真实出现过的经典代码面试题,帮助广“大程序员的面试准备做到万无一失。 “刷”完本书后,你就是“题王”!
《TCP-IP协议组(第4版)》
本书是介绍TCP/IP协议族的经典图书的最新版本。本书自第1版出版以来,就广受读者欢迎。
本书最新版进行」护元,以体境计算机网络技不的最新发展,全书古有七大部分共30草和7个附录:第一部分介绍一些基本概念和基础底层技术:第二部分介绍网络层协议:第三部分介绍运输层协议;第四部分介绍应用层协议:第五部分介绍下一代协议,即IPv6协议:第六部分介绍网络安全问题:第七部分给出了7个附录。
Java开发手册(嵩山版)
这个不用多说了,阿里的开发手册,每次更新我都会看,这是8月初最新更新的**(嵩山版)**
MySQL 8从入门到精通
本书主要内容包括MySQL的安装与配置、数据库的创建、数据表的创建、数据类型和运算符、MySQL 函数、查询数据、数据表的操作(插入、更新与删除数据)、索引、存储过程和函数、视图、触发器、用户管理、数据备份与还原、MySQL 日志、性能优化、MySQL Repl ication、MySQL Workbench、 MySQL Utilities、 MySQL Proxy、PHP操作MySQL数据库和PDO数据库抽象类库等。最后通过3个综合案例的数据库设计,进步讲述 MySQL在实际工作中的应用。
Spring5高级编程(第5版)
本书涵盖Spring 5的所有内容,如果想要充分利用这一领先的企业级 Java应用程序开发框架的强大功能,本书是最全面的Spring参考和实用指南。
本书第5版涵盖核心的Spring及其与其他领先的Java技术(比如Hibemate JPA 2.Tls、Thymeleaf和WebSocket)的集成。本书的重点是介绍如何使用Java配置类、lambda 表达式、Spring Boot以及反应式编程。同时,将与企业级应用程序开发人员分享一些见解和实际经验,包括远程处理、事务、Web 和表示层,等等。
JAVA核心知识点+1000道 互联网Java工程师面试题
企业IT架构转型之道 阿里巴巴中台战略思想与架构实战
本书讲述了阿里巴巴的技术发展史,同时也是-部互联网技 术架构的实践与发展史。
Thymeleaf和WebSocket)的集成。本书的重点是介绍如何使用Java配置类、lambda 表达式、Spring Boot以及反应式编程。同时,将与企业级应用程序开发人员分享一些见解和实际经验,包括远程处理、事务、Web 和表示层,等等。
[外链图片转存中…(img-8F1Vi3Xd-1714997679488)]
JAVA核心知识点+1000道 互联网Java工程师面试题
[外链图片转存中…(img-i91rT77Y-1714997679488)]
[外链图片转存中…(img-7PLcpDAv-1714997679489)]
企业IT架构转型之道 阿里巴巴中台战略思想与架构实战
本书讲述了阿里巴巴的技术发展史,同时也是-部互联网技 术架构的实践与发展史。
[外链图片转存中…(img-RzP2OSC0-1714997679489)]