show-sql: true
open-in-view: true
#配置Eureka
eureka:
client:
service-url:
defaultZone: http://localhost:9000/eureka/
instance:
prefer-ip-address: true #使用ip地址注册
3)修改启动类,添加服务发现的支持(可选)
package cn.itbluebox.product;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EntityScan(“cn.itbluebox.product.entity”)
//激活Eureka
//@EnableEurekaClient
@EnableDiscoveryClient //写不写都一样
public class ProductApplication {
public static void main(String[] args) {
SpringApplication.run(ProductApplication.class,args);
}
}
4)运行测试,启动商品微服务
多了
(3)服务消费者通过注册中心获取服务列表,并调用
Eureka中的元数据:服务的主机名,ip,等信息.可以通过eurekaserver进行获取,用于服务之间的调用,
1)引入EurekaClient的坐标
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
2)修改application.yml添加EurekaServer的信息
server:
port: 9002 #端口
spring:
application:
name: service-order #服务名称
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地址注册
3)完善order_service当中的 OrderController
package cn.itbluebox.order.controller;
import cn.itbluebox.order.entity.Product;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import java.util.List;
@RestController
@RequestMapping(“/order”)
public class OrderController {
//注入RestTemplate对象
@Autowired
private RestTemplate restTemplate;
/*
注入DiscoveryClient:
SpringCloud 提供的获取原数组的工具类
调用方法获取服务的元数据信息
*/
@Autowired
private DiscoveryClient discoveryClient;
/*
参数:商品ID
通过订单系统,调用商品服务
1、需要配置商品对象
2、需要调用商品服务
3、entity.Product
使用java 当中的urlconnection,httpclient,okhttp
*/
@RequestMapping(value = “/buy/{id}”, method = RequestMethod.GET)
public Product findById(@PathVariable Long id) {
//调用discoveryClient方法
//已调用服务名称获取所有的元数据
List instances = discoveryClient.getInstances(“service-product”);
//获取唯一的一个元数据
ServiceInstance instance = instances.get(0);
//根据元数据中的主机地址和端口号拼接请求微服务的URL
Product product = null;
//如何调用商品服务?
product = restTemplate.getForObject(“http://”+instance.getHost()+“:”+instance.getPort()+“/product/”+id,Product.class);
return product;
}
@RequestMapping(method = RequestMethod.POST)
public String save(@RequestBody Product product) {
//调用discoveryClient方法
//已调用服务名称获取所有的元数据
List instances = discoveryClient.getInstances(“service-product”);
//获取唯一的一个元数据
ServiceInstance instance = instances.get(0);
restTemplate.postForObject(“http://”+instance.getHost()+“:”+instance.getPort()+“/product/”,product,Product.class);
return “保存成功”;
}
@RequestMapping(method = RequestMethod.PUT)
public String update(@RequestBody Product product) {
//调用discoveryClient方法
//已调用服务名称获取所有的元数据
List instances = discoveryClient.getInstances(“service-product”);
//获取唯一的一个元数据
ServiceInstance instance = instances.get(0);
restTemplate.put(“http://”+instance.getHost()+“:”+instance.getPort()+“/product/”,product);
return “更新成功”;
}
@RequestMapping(value = “/{id}”, method = RequestMethod.DELETE)
public String delete(@PathVariable Long id) {
//调用discoveryClient方法
//已调用服务名称获取所有的元数据
List instances = discoveryClient.getInstances(“service-product”);
//获取唯一的一个元数据
ServiceInstance instance = instances.get(0);
restTemplate.delete(“http://”+instance.getHost()+“:”+instance.getPort()+“/product/”+id);
return “删除成功”;
}
}
4)运行测试
访问http://localhost:9002/order/buy/1
=========================================================================
在上一个章节,实现了单节点的Eureka Server的服务注册与服务发现功能。
Eureka Client会定时连接Eureka Server,获取注册表中的信息并缓存到本地。
微服务在消费远程API时总是使用本地缓存中的数据。
因此一般来说,即使Eureka Server发生宕机,也不会影响到服务之间的调用。但如果Eureka Server宕机时,某些微服务也出现了不可用的情况,Eureka Server中的缓存若不被刷新,就可能会影响到微服务的调用,甚至影响到整个应用系统的高可用。
因此,在生成环境中,通常会部署一个高可用的Eureka Server集群。
Eureka Server可以通过运行多个实例并相互注册的方式实现高可用部署,Eureka Server实例会彼此增量地同步信息,从而确保所有节点数据一致。
事实上,节点之间相互注册是Eureka Server的默认行为。
(1)搭建 Eureka Server高可用集群
1 准备2个EurekaServer,需要相互注册
1号server : 9000
2号server : 8000I
2需要将微服务注册到两个EurekaServer上
1)修改eureka_server当中的application.yml
#模拟两个EurekaServer
#端口9000 , 8000
#两个server需要相互注册
spring:
application:
name: eureka-server
server:
port: 9000 #端口
#配置eureka server
eureka:
client:
#register-with-eureka: false #是否将自己注册到注册中心
#fetch-registry: false #是否从eureka中获取注册信息
service-url: #配置暴露给Eureka Client的请求地址
defaultZone: http://127.0.0.1:8000/eureka/
运行测试
打开Run Dashboard
- 修改application.yml
#模拟两个EurekaServer
#端口9000 , 8000
#两个server需要相互注册
spring:
application:
name: eureka-server
server:
port: 8000 #端口
#配置eureka server
eureka:
client:
#register-with-eureka: false #是否将自己注册到注册中心
#fetch-registry: false #是否从eureka中获取注册信息
service-url: #配置暴露给Eureka Client的请求地址
defaultZone: http://127.0.0.1:9000/eureka/
启动多个eureka_server
实现eureka的相互注册
访问地址:
2)启动product_service
将product_service注册到http://localhost:8000/eureka/
修改product_service当中的application.yml
server:
port: 9001 #端口
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/,http://localhost:8000/eureka/
instance:
prefer-ip-address: true #使用ip地址注册
- 启动product_service
http://localhost:8000/
3)启动order_service
将order_service注册到http://localhost:8000/eureka/
修改order_service当中的application.yml
server:
port: 9002 #端口
spring:
application:
name: service-order #服务名称
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/,http://localhost:8000/eureka/
instance:
prefer-ip-address: true #使用ip地址注册
访问:http://localhost:9000/
访问:http://localhost:8000/
4)测试
断开一台eurekaService
8000断开
测试数据是否可以正确获取:http://localhost:9002/order/buy/1
修改eureka_server当中的application.yml,修改回之前的配置方便后期测试
#模拟两个EurekaServer
#端口9000 , 8000
#两个server需要相互注册
spring:
application:
name: eureka-server
server:
port: 9000 #端口
#配置eureka server
eureka:
client:
register-with-eureka: false #是否将自己注册到注册中心
fetch-registry: false #是否从eureka中获取注册信息
service-url: #配置暴露给Eureka Client的请求地址
defaultZone: http://127.0.0.1:8000/eureka/
1)使product_service暴露对应的ip
修改product_service当中的application.yml
server:
port: 9001 #端口
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
重新运行并测试
在服务的提供者,设置心跳间隔,设置续约到期时间
修改product_service
当中的application.yml
server:
port: 9001 #端口
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 #续约到期的时间
重新启动
小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数初中级Java工程师,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年最新Java开发全套学习资料》送给大家,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频
如果你觉得这些内容对你有帮助,可以添加下面V无偿领取!(备注Java)
总结
面试前的“练手”还是很重要的,所以开始面试之前一定要准备好啊,不然也是耽搁面试官和自己的时间。
我自己是刷了不少面试题的,所以在面试过程中才能够做到心中有数,基本上会清楚面试过程中会问到哪些知识点,高频题又有哪些,所以刷题是面试前期准备过程中非常重要的一点。
面试题及解析总结
大厂面试场景
知识点总结
63bcea4e69a4763824a686ff5d.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBA6JOd55uS5a2QaXRibHVlYm94,size_20,color_FFFFFF,t_70,g_se,x_16)
小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数初中级Java工程师,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年最新Java开发全套学习资料》送给大家,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
[外链图片转存中…(img-5eWI5oZ3-1711052063119)]
[外链图片转存中…(img-pNfaQGkm-1711052063119)]
[外链图片转存中…(img-piQ3uRRr-1711052063120)]
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频
如果你觉得这些内容对你有帮助,可以添加下面V无偿领取!(备注Java)
[外链图片转存中…(img-j2sRBEOD-1711052063120)]
总结
面试前的“练手”还是很重要的,所以开始面试之前一定要准备好啊,不然也是耽搁面试官和自己的时间。
我自己是刷了不少面试题的,所以在面试过程中才能够做到心中有数,基本上会清楚面试过程中会问到哪些知识点,高频题又有哪些,所以刷题是面试前期准备过程中非常重要的一点。
面试题及解析总结
[外链图片转存中…(img-jCM7TT43-1711052063120)]
大厂面试场景
[外链图片转存中…(img-sACim1MQ-1711052063121)]
知识点总结
[外链图片转存中…(img-tUj2wpBL-1711052063121)]