读者福利
分享一份自己整理好的Java面试手册,还有一些面试题pdf
不要停下自己学习的脚步
spring:
application:
name: service-product #服务名称
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/springcloud?useUnicode=true&characterEncoding=utf8
username: root
password: root
jpa:
database: MySQL
show-sql: true
open-in-view: true
#配置Eureka
eureka:
client:
service-url:
defaultZone: http://localhost:9000/eureka/
instance:
prefer-ip-address: true #使用ip地址注册
instance-id: s p r i n g . c l o u d . c l i e n t . i p − a d d r e s s : {spring.cloud.client.ip-address}: spring.cloud.client.ip−address:{server.port} #向组成中心注册服务ID
lease-renewal-interval-in-seconds: 5 #向注册中心中注册服务id
lease-expiration-duration-in-seconds: 10 #续约到期的时间
name: itbluebox-dev
4)将上述配置文件上传到gitee
(1)创建工程
(2)引入依赖
org.springframework.cloud
spring-cloud-config-server
org.springframework.cloud
spring-cloud-starter-config
(3)编写启动类
@SpringBootApplication
@EnableConfigServer
public class ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class,args);
}
}
- @EnableConfigServer : 通过此注解开启注册中心服务端功能
(4)设置配置文件
在这之前复制一下Git请求的服务器的地址
server:
port: 10001 #服务端口
spring:
application:
name: config-server #指定服务名
cloud:
config:
server:
git:
uri: https://gitee.com/itbluebox/config-repostory.git
(5)启动服务进行测试
运行成功
访问测试:http://localhost:10001/product-dev.yml
(1)修改product_service
1)引入依赖
org.springframework.cloud
spring-cloud-starter-config
2)删除application.yml
springboot的应用配置文件,需要通过Config-server获取,这里不再需要。
spring:
cloud:
config:
name: product #应用名称需要对应Git中配置文件的名称的前半部分
profile: dev #开发环境
label: master #git当中的分支
uri: http://localhost:10001 #config-server的请求地址
3)运行测试
访问:http://localhost:9001/product/1
访问:http://localhost:9001/product/test
4)修改配置文件然后在运行测试
将dev修改为pro
spring:
cloud:
config:
name: product #应用名称需要对应Git中配置文件的名称的前半部分
profile: pro #开发环境
label: master #git当中的分支
uri: http://localhost:10001 #config-server的请求地址
重新启动运行
请求的端口改变了
访问:http://localhost:9002/product/1
(1)问题解析
我们已经在客户端取到了配置中心的值,
修改Gitee上的值
修改内容
访问:http://localhost:9002/product/test
但当我们修改Gitee上面的值时,
服务端(Config Server)能实时获取最新的值,但客户端(Config Client)读的是缓存,无法实时获取最新值。
SpringCloud已经为我们解决了这个问题,那就是客户端使用post去触发refresh,获取最新数据,需要依赖spring-boot-starter-actuator
(2)引入依赖
org.springframework.boot
spring-boot-starter-actuator
(3)在ProductController
类加上@RefreshScope
@RestController
@RequestMapping(“/product”)
@RefreshScope //代表的是开启动态刷新
public class ProductController {
@Autowired
private ProductService productService;
@Value(“${server.port}”)
private String port;
@Value(“${spring.cloud.client.ip-address}”)
private String ip;
@Value(value = “${name}”)
private String name;
@RequestMapping(value = “/{id}”, method = RequestMethod.GET)
public Product findById(@PathVariable Long id) {
Product product = productService.findByID(id);
product.setProductName(“访问的服务地址:”+ip+“:”+port);
return product;
}
@RequestMapping(value = “/test”)
public String test(){
return name;
}
}
(4)配置文件bootstrap.yml
中开发端点
开启动态刷新的请求路径的端点
management:
endpoints:
web:
exposure:
include: refresh
(5)运行测试
访问:http://localhost:9002/product/test
其值itbluebox-product改变因为重新启动了
我们再次修改Gitee对应的配置文件的值
提交并保存
再次访问:http://localhost:9002/product/test
(6)手动刷新发起POST请求,这里使用的工具是Insomnia
访问:http://localhost:9002/actuator/refresh
再次访问:http://localhost:9002/product/test
=====================================================================
在之前的代码中,客户端都是直接调用配置中心的server端来获取配置文件信息。
这样就存在了一个问题,客户端和服务端的耦合性太高,如果server端要做集群,客户端只能通过原始的方式来路由,server端改变IP地址的时候,客户端也需要修改配置,不符合springcloud服务治理的理念。
springcloud提供了这样的解决方案,我们只需要将server端当做一个服务注册到eureka中,client端去eureka中去获取配置中心server端的服务既可。
(1)添加依赖
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
org.springframework.boot
spring-boot-starter-actuator
org.springframework.cloud
spring-cloud-bus
org.springframework.cloud
spring-cloud-stream-binder-rabbit
(2)配置文件
#配置Eureka
eureka:
client:
service-url:
defaultZone: http://localhost:9000/eureka/
instance:
prefer-ip-address: true #使用ip地址注册
instance-id: s p r i n g . c l o u d . c l i e n t . i p − a d d r e s s : {spring.cloud.client.ip-address}: spring.cloud.client.ip−address:{server.port} #向组成中心注册服务ID
lease-renewal-interval-in-seconds: 5 #向注册中心中注册服务id
lease-expiration-duration-in-seconds: 10 #续约到期的时间
这样server端的改造就完成了。
(3)运行测试
先启动eureka注册中心,在启动server端,
在浏览器中访问:
复制一份ConfigServerApplication
修改一下配置文件的端口号
访问Eurekahttp://localhost:9000/
修改配置文件
spring:
cloud:
config:
name: product #应用名称需要对应Git中配置文件的名称的前半部分
profile: pro #开发环境
label: master #git当中的分支
#uri: http://localhost:10001 #config-server的请求地址
#通过注册中心去获取config-server配置
discovery:
enabled: true #开启服务发现
service-id: config-server
开启动态刷新的请求路径的端点
management:
endpoints:
web:
exposure:
include: refresh
#配置Eureka
eureka:
client:
service-url:
defaultZone: http://localhost:9000/eureka/
instance:
prefer-ip-address: true #使用ip地址注册
instance-id: s p r i n g . c l o u d . c l i e n t . i p − a d d r e s s : {spring.cloud.client.ip-address}: spring.cloud.client.ip−address:{server.port} #向组成中心注册服务ID
重新启动
访问测试:http://localhost:9002/product/1
====================================================================
在微服务架构中,
通常会使用轻量级的消息代理来构建一个共用的消息主题来连接各个微服务实例,
它广播的消息会被所有在注册中心的微服务实例监听和消费,也称消息总线。
SpringCloud中也有对应的解决方案,
SpringCloud Bus 将分布式的节点用轻量的消息代理连接起来,
可以很容易搭建消息总线,
配合SpringCloud config 实现微服务应用配置信息的动态更新。
根据此图我们可以看出利用Spring Cloud Bus做配置更新的步骤:
-
提交代码触发post请求给bus/refresh
-
server端接收到请求并发送给Spring Cloud Bus
-
Spring Cloud bus接到消息并通知给其它客户端
-
其它客户端接收到通知,请求Server端获取最新配置
-
全部客户端均获取到最新的配置
(1)服务端引入依赖
刚刚在上述已经引入了对应消息总线的依赖,在这里是做介绍
(2)服务端添加配置
server:
port: 10001 #服务端口
spring:
application:
name: config-server #指定服务名
cloud:
config:
server:
git:
uri: https://gitee.com/itbluebox/config-repostory.git
rabbitmq:
host: 127.0.0.1
port: 5672
username: guest
password: guest
management:
endpoints:
web:
exposure:
include: bus-refresh
#配置Eureka
eureka:
client:
service-url:
defaultZone: http://localhost:9000/eureka/
instance:
prefer-ip-address: true #使用ip地址注册
instance-id: s p r i n g . c l o u d . c l i e n t . i p − a d d r e s s : {spring.cloud.client.ip-address}: spring.cloud.client.ip−address:{server.port} #向组成中心注册服务ID
lease-renewal-interval-in-seconds: 5 #向注册中心中注册服务id
lease-expiration-duration-in-seconds: 10 #续约到期的时间
(3)微服务客户端引入依赖
org.springframework.cloud
spring-cloud-bus
org.springframework.cloud
spring-cloud-stream-binder-rabbit
(4)微服务客户端配置
1)bootstrap.yml 当中删除请求路径的端点的配置
spring:
cloud:
config:
name: product #应用名称需要对应Git中配置文件的名称的前半部分
profile: pro #开发环境
label: master #git当中的分支
#uri: http://localhost:10001 #config-server的请求地址
#通过注册中心去获取config-server配置
discovery:
enabled: true #开启服务发现
service-id: config-server
#配置Eureka
eureka:
client:
service-url:
defaultZone: http://localhost:9000/eureka/
instance:
prefer-ip-address: true #使用ip地址注册
instance-id: s p r i n g . c l o u d . c l i e n t . i p − a d d r e s s : {spring.cloud.client.ip-address}: spring.cloud.client.ip−address:{server.port} #向组成中心注册服务ID
总结
阿里伤透我心,疯狂复习刷题,终于喜提offer 哈哈~好啦,不闲扯了
1、JAVA面试核心知识整理(PDF):包含JVM,JAVA集合,JAVA多线程并发,JAVA基础,Spring原理,微服务,Netty与RPC,网络,日志,Zookeeper,Kafka,RabbitMQ,Hbase,MongoDB,Cassandra,设计模式,负载均衡,数据库,一致性哈希,JAVA算法,数据结构,加密算法,分布式缓存,Hadoop,Spark,Storm,YARN,机器学习,云计算共30个章节。
2、Redis学习笔记及学习思维脑图
3、数据面试必备20题+数据库性能优化的21个最佳实践
service-id: config-server
#配置Eureka
eureka:
client:
service-url:
defaultZone: http://localhost:9000/eureka/
instance:
prefer-ip-address: true #使用ip地址注册
instance-id: s p r i n g . c l o u d . c l i e n t . i p − a d d r e s s : {spring.cloud.client.ip-address}: spring.cloud.client.ip−address:{server.port} #向组成中心注册服务ID
总结
阿里伤透我心,疯狂复习刷题,终于喜提offer 哈哈~好啦,不闲扯了
[外链图片转存中…(img-booC586v-1715292699772)]
1、JAVA面试核心知识整理(PDF):包含JVM,JAVA集合,JAVA多线程并发,JAVA基础,Spring原理,微服务,Netty与RPC,网络,日志,Zookeeper,Kafka,RabbitMQ,Hbase,MongoDB,Cassandra,设计模式,负载均衡,数据库,一致性哈希,JAVA算法,数据结构,加密算法,分布式缓存,Hadoop,Spark,Storm,YARN,机器学习,云计算共30个章节。
[外链图片转存中…(img-2T1yJWJM-1715292699773)]
2、Redis学习笔记及学习思维脑图
[外链图片转存中…(img-hfOgdWqb-1715292699773)]
3、数据面试必备20题+数据库性能优化的21个最佳实践
[外链图片转存中…(img-NQOuJf3i-1715292699773)]