项目地址
[CloudStudy项目](zko0/cloudStudy: Springcloud学习工程 (github.com))
1.依赖版本
- cloud:Hoxton.SR1
- boot:2.2.2.RELEASE
- cloud alibaba 2.1.0RELEASE
- Java 8
- Maven>3.5
- Mysql>5.7
2.工程编写
- 创建父工程
- 编写payment工程
1.热部署插件(不推荐使用)
-
添加依赖
<!--热部署--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency>
-
添加插件到Pom.xml
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>2.4.1</version> <configuration> <addResources>true</addResources> </configuration> </plugin> </plugins> </build>
-
修改Idea设置
-
配置,(我使用的为idea2022,2021.2 之前方法不同,需要百度)
-
2.RestTemplate
提供多种便携访问远程Http服务的方法,是一种简单辨析的restful服务模板类。Spring提供的用于访问Rest服务端客户端模板工具集。
@Configuration
public class ApplicationContextConfig {
@Bean
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
}
@RestController
@Slf4j
public class OrderController {
private static final String PAYMENT_URL="http://localhost:8001";
@Resource
private RestTemplate restTemplate;
@GetMapping("/consumer/payment/create")
public CommonResult<Payment> create(Payment payment){
return restTemplate.postForObject(PAYMENT_URL+"/payment/create",payment,CommonResult.class);
}
@GetMapping("/consumer/payment/get/{id}")
public CommonResult<Payment> getPayment(@PathVariable("id") Long id){
return restTemplate.getForObject(PAYMENT_URL+"/payment/get/"+id,CommonResult.class);
}
}
剩余部分在阳哥CLoud2.0的前15P,自行编写。
3.服务注册中心
1.Eureka单机
一.EurekaServer
新建项目cloud-eureka-server7001
pom.xml:
<dependencies>
<!--eureka server-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<!--引入自己定义的api通用包,可以使用payment支付Entity-->
<dependency>
<groupId>cn.zko0.cloud</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!--boot web actuator-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!--一般通用配置-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
</dependencies>
application.yml:
server:
port: 7001
eureka:
instance:
hostname: localhost #eureka服务端的实例名称
client:
#false表示不向注册中心注册自己
register-with-eureka: false
#false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
fetch-registry: false
service-url:
#设置与Eureka Server交互的地址查询服务和注册服务都需要依赖这个地址
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
入口类:
@SpringBootApplication
@EnableEurekaServer
public class EurekaServer7001 {
public static void main(String[] args) {
SpringApplication.run(EurekaServer7001.class,args);
}
}
二.EurekaClient-Provider
在provider项目上添加eureka-client依赖
<!--eureka-client-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
在入口类加入Enable
@SpringBootApplication
@EnableEurekaClient
public class PaymentMain8001 {
public static void main(String[] args) {
SpringApplication.run(PaymentMain8001.class,args);
}
}
配置文件添加配置:
这里的service-url需要参考Server中配置文件的service-url
spring:
application:
name: cloud-payment-service
eureka:
client:
#表示是否将自己注册进EurekaServer默认为true
register-with-eureka: true
#是否从EurekaServer抓取已有的注册信息,默认为true。单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡
fetch-registry: true
service-url:
defaultZone: http://localhost:7001/eureka
测试:
Eureka自我保护机制(暂时忽略)
其中Eureka注册服务名称为Spring.application.name名称
三.EurekaClient-Consuemr
1.添加eureka-client依赖:
<!--eureka-client-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
2.入口类添加Enable
@SpringBootApplication
@EnableEurekaClient
public class OrderMain80 {
public static void main(String[] args) {
SpringApplication.run(OrderMain80.class,args);
}
}
3.配置文件添加配置信息
spring:
application:
name: cloud-order-server
eureka:
client:
#表示是否将自己注册进EurekaServer默认为true
register-with-eureka: true
fetch-registry: true
service-url:
defaultZone: http://localhost:7001/eureka
4.测试
eureka注册开关:
2.Eureka集群+LoadBalanced服务调用
1.创建Eureka-Server7002
cloud-eureka-server7002
@SpringBootApplication
@EnableEurekaServer
public class EurekaServer7002 {
public static void main(String[] args) {
SpringApplication.run(EurekaServer7002.class,args);
}
}
2.相互注册,相互守望
修改hostname,不能7001和7002都为localhost
修改配置文件:
127.0.0.1 eureka7001.com
127.0.0.2 eureka7002.com
127.0.0.3 eureka7003.com
修改项目配置文件,相互注册,相互守望
cloud-eureka-server7002的pom.xml文件修改:
server:
port: 7002
eureka:
instance:
hostname: eureka7002.com #eureka服务端的实例名称
client:
#false表示不向注册中心注册自己
register-with-eureka: false
#false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
fetch-registry: false
service-url:
#设置与Eureka Server交互的地址查询服务和注册服务都需要依赖这个地址
defaultZone: http://eureka7001.com:7001/eureka/
cloud-eureka-server7001的pom.xml文件修改:
server:
port: 7001
eureka:
instance:
hostname: eureka7001.com #eureka服务端的实例名称
client:
#false表示不向注册中心注册自己
register-with-eureka: false
#false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
fetch-registry: false
service-url:
#设置与Eureka Server交互的地址查询服务和注册服务都需要依赖这个地址
defaultZone: http://eureka7002.com:7002/eureka/
3.测试集群
4.将微服务配置进集群
将payment和order项目的配置文件修改service-url的地址
5.测试微服务的配置
6.多个提供者集群搭建
新建cloud-provider-payment-8002
@SpringBootApplication
@EnableEurekaClient
public class PaymentMain8002 {
public static void main(String[] args) {
SpringApplication.run(PaymentMain8002.class,args);
}
}
pom.xml:
server:
port: 8002
提取port信息到controller,方法后面方法调用显示对应的信息:
简单测试服务是否注册:
order调用只会调用8001,因为在order中已经写死了调用地址
修改Controller调用地址
重启Order调用,出错:
只有@LoadBalanced修饰的restTemplate才能通过服务名调用,否则只能通过ip+port调用
7.最后的测试
4.细节完善
1.去除服务主机名
eureka可以看到主机名,让它不显示
修改8001和8002的application.yml:
测试:
2.鼠标悬停显示Ip+port
如果你已经显示了,则不需要配置(我的项目悬停默认显示Ip和端口号)
测试:
5.服务发现
能够通过接口调用,获取到Eureka上的所有服务信息
在provider-payment-8001上添加discovery()方法:
@GetMapping(value = "/payment/discovery")
public Object discovery() {
//获取在Eureka中注册的services有哪些
List<String> services = discoveryClient.getServices();
for (String element:services) {
log.info("*****element:{}",element);
}
List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-PAYMENT-SERVICE");
for (ServiceInstance instance : instances) {
log.info(instance.getServiceId()+"\t"+instance.getHost()+"\t"+instance.getPort()+"\t"+instance.getUri());
}
return this.discoveryClient;
}
在启动类上添加注解:
![image-20230112151339981](assets/image-20230112151339981.png)
测试:
6.Eureka自我保护机制
1.保护模式现象
Eureka进入保护模式:
2.保护模式是什么?
Eureka Server会尝试保护其服务注册表中的信息,不再删除服务注册表中的数据,也就是不会注销任何的微服务。
翻译成人话:某个微服务不可用,Eureka不会立刻清理,依然会对该微服务的信息进行保存
CAP中的AP
这句话没搞懂,后面慢慢学
3.为什么需要保护模式
默认情况下,EurekaServer在一段事件里没有接收到某个微服务实例的心跳,EurekaServer会注销该实例。(90s)
但是当网络分区故障,EurekaServer和微服务之间无法正常通信了,上面的行为就会很危险。
因为这时:微服务是健康的(这个微服务不应该被注销掉)
Eureka自我保护模式如何启动:当EurekaServer节点短时间内有很多Client丢失,那么这个节点就会进入保护模式。
4.设计理念
宁可保留错误的服务注册信息,也不盲目的注销掉任何健康的服务实例。
5.如何关闭自我保护
Eureka默认开启自我保护机制
首先关闭集群搭建,只开启7001
Eureka-Server-7001关闭自我保护模式,同时将心跳时间修改为2s(2s内没有收到就销毁)
Eureka-Client关闭自我保护:
测试:自我保护模式已关闭
这时关闭8001项目,Eureka会立马删除服务: