赞
踩
服务治理,服务注册与发现
Spring Cloud封装了Netflix公司开发的Eureka模块来实现服务治理
在传统RPC远程调用框架中,管理每个服务与服务之间的依赖关系比较复杂,管理比较复杂,所以需要使用服务治理,管理服务与服务之间的依赖关系,可以实现服务调用、负载均衡、容错等、实现服务发现与注册。
Eureka采用了CS的设计架构,Eureka Server作为服务注册功能的服务器,它是服务注册中心。而系统中中的其他微服务,使用Eureka的客户端连接到Eureka Server并维持心跳连接。这样系统的维护人员可以通过Eureka Server来监控系统中各个微服务是否正常运行。
在服务注册与发现中,有一个注册中心。当服务器启动的时候,会把自己的服务器的信息 比如 服务器通讯地址等以别名的方式注册到注册中心上。另一方(消费者|服务提供者),以该别名的方式去注册中心上获取实际的通讯地址,然后再实现本地RPC调用RPC远程调用框架核心设计思想:在于注册中心,因为注册中心管理每个服务与每个服务之间的一个依赖关系(服务治理概念)。在任何RPC远程调用框架中,都会有一个注册中心(存放服务地址相关信息(接口地址))
提供服务注册服务
各个微服务节点通过配置启动后,会在EurekaServer中进行注册,这样EurekaServer中的服务注册表中将会存储所有可用服务器的信息,服务节点的信息可以在界面中看到。
是一个JAVA客户端,用于简化Eureka Server的交互,客户端也具备一个内置的,使用轮询(round-robin)负载算法的负载均衡器。在应用启动后,将会向Eureka Server发送心跳(默认周期为30秒)。如果Eureka Server在多个心跳周期内没有接收到某个节点的心跳,EurekaServer会从服务注册表中把这个服务节点移除(默认90秒)
项目名:cloud-eureka-server7001
- <?xml version="1.0" encoding="UTF-8"?>
- <project xmlns="http://maven.apache.org/POM/4.0.0"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <parent>
- <artifactId>mscloud03</artifactId>
- <groupId>com.atguigu.springcloud</groupId>
- <version>1.0-SNAPSHOT</version>
- </parent>
- <modelVersion>4.0.0</modelVersion>
-
- <artifactId>cloud-eureka-server7001</artifactId>
-
- <dependencies>
- <!--eureka-server-->
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
- </dependency>
- <!-- 引入自己定义的api通用包,可以使用Payment支付Entity -->
- <dependency>
- <groupId>com.atguigu.springcloud</groupId>
- <artifactId>cloud-api-commons</artifactId>
- <version>${project.version}</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>
-
- </project>
- <!-- 以前的老版本(当前使用2018)-->
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-eureka</artifactId>
- </dependency>
- <!-- 现在新版本(当前使用2020.2) -->
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
- </dependency>
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/
- package com.gzf.springcloud;
-
-
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
- import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
- import org.springframework.context.annotation.Configuration;
-
- @SpringBootApplication
- @EnableEurekaServer //代表是EurekaServer服务端
- public class EurekaMain7001 {
-
- public static void main(String[] args) {
- SpringApplication.run(EurekaMain7001.class);
- }
- }
名字:cloud-provider-payment8001
- <?xml version="1.0" encoding="UTF-8"?>
- <project xmlns="http://maven.apache.org/POM/4.0.0"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <parent>
- <artifactId>cloud2020</artifactId>
- <groupId>com.gzf.springcloud</groupId>
- <version>1.0-SNAPSHOT</version>
- </parent>
- <modelVersion>4.0.0</modelVersion>
-
- <artifactId>cloud-provider-payment8001</artifactId>
-
- <properties>
- <maven.compiler.source>8</maven.compiler.source>
- <maven.compiler.target>8</maven.compiler.target>
- </properties>
-
- <dependencies>
- <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.mybatis.spring.boot</groupId>
- <artifactId>mybatis-spring-boot-starter</artifactId>
- </dependency>
- <dependency>
- <groupId>com.alibaba</groupId>
- <artifactId>druid-spring-boot-starter</artifactId>
- <version>1.1.10</version>
- </dependency>
- <!--mysql-connector-java-->
- <dependency>
- <groupId>mysql</groupId>
- <artifactId>mysql-connector-java</artifactId>
- </dependency>
- <!--jdbc-->
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-jdbc</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>
- <optional>true</optional>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-test</artifactId>
- <scope>test</scope>
- </dependency>
- <dependency>
- <groupId>com.gzf.springcloud</groupId>
- <artifactId>cloud-api-commons</artifactId>
- <version>1.0-SNAPSHOT</version>
- </dependency>
- </dependencies>
- <!-- 新增的依赖 Eureka客户端-->
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
- </dependency>
-
- </project>
注意依赖
- <!-- 以前老版本,别再使用 -->
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-eureka</artifactId>
- </dependency>
- <!-- 现在新版本,当前使用 -->
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
- </dependency>
server: port: 8001 spring: application: name: cloud-payment-service datasource: type: com.alibaba.druid.pool.DruidDataSource # 当前数据源操作类型 driver-class-name: com.mysql.jdbc.Driver # mysql驱动包 url: jdbc:mysql://localhost:3306/db2019?useUnicode=true&characterEncoding=utf-8&useSSL=false username: root password: 123456 mybatis: mapperLocations: classpath:mapper/*.xml type-aliases-package: com.atguigu.springcloud.entities # 所有Entity别名类所在包 #新增Eureka配置 eureka: client: #表示是否将自己注册进EurekaServer默认为true。 register-with-eureka: true #是否从EurekaServer抓取已有的注册信息,默认为true。单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡 fetchRegistry: true service-url: defaultZone: http://localhost:7001/eureka
- package com.gzf.springcloud;
-
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
- import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
-
- @SpringBootApplication
- @EnableEurekaClient // 新增注解 代表是EnableEureka的客户端
- public class PaymentMain8001 {
- public static void main(String[] args) {
- SpringApplication.run(PaymentMain8001.class);
- }
- }
- import com.gzf.springcloud.service.PaymentService;
-
-
- import jdk.nashorn.internal.runtime.logging.Logger;
- import lombok.extern.slf4j.Slf4j;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.cloud.client.ServiceInstance;
- import org.springframework.cloud.client.discovery.DiscoveryClient;
- import org.springframework.web.bind.annotation.*;
-
- import javax.annotation.Resource;
- import java.util.List;
-
- @RestController
- @Slf4j
- public class PaymentController{
-
- @GetMapping("/hello")
- public String getPaymentById() {
- return "Hello Eureka";
- }
-
- }
名称:cloud-consumer-order80
- <?xml version="1.0" encoding="UTF-8"?>
- <project xmlns="http://maven.apache.org/POM/4.0.0"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <parent>
- <artifactId>cloud2020</artifactId>
- <groupId>com.gzf.springcloud</groupId>
- <version>1.0-SNAPSHOT</version>
- </parent>
- <modelVersion>4.0.0</modelVersion>
-
- <artifactId>cloud-consumer-order80</artifactId>
-
- <properties>
- <maven.compiler.source>8</maven.compiler.source>
- <maven.compiler.target>8</maven.compiler.target>
- </properties>
-
- <dependencies>
- <!-- 新增的依赖 代表是eureka客户端-->
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
- </dependency>
- <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>
- <optional>true</optional>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-test</artifactId>
- <scope>test</scope>
- </dependency>
- <dependency>
- <groupId>com.gzf.springcloud</groupId>
- <artifactId>cloud-api-commons</artifactId>
- <version>1.0-SNAPSHOT</version>
- </dependency>
- </dependencies>
- </project>
# 端口号 server: port: 80 # 服务名称 spring: application: name: cloud-order-service # eureka配置 eureka: client: #表示是否将自己注册进EurekaServer默认为true。 register-with-eureka: true #是否从EurekaServer抓取已有的注册信息,默认为true。单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡 fetchRegistry: true service-url: defaultZone: http://localhost:7001/eureka
- package com.gzf.springcloud;
-
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
- import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
-
- @SpringBootApplication
- @EnableEurekaClient //新增注解 代表是Eureka客户端
- public class OrderMain80 {
-
- public static void main(String[] args) {
- SpringApplication.run(OrderMain80.class);
- }
- }
- package com.gzf.springcloud.config;
-
- import org.springframework.cloud.client.loadbalancer.LoadBalanced;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
- import org.springframework.web.client.RestTemplate;
-
- /**
- 远程调用工具
- */
- @Configuration
- public class ApplicationContextConfig {
-
- @Bean
- public RestTemplate getRestTemplate(){
- return new RestTemplate();
- }
- }
- package com.gzf.springcloud.controller;
-
- import com.gzf.springcloud.entities.CommonResult;
- import com.gzf.springcloud.entities.Payment;
- import lombok.extern.slf4j.Slf4j;
- import org.springframework.http.ResponseEntity;
- import org.springframework.web.bind.annotation.*;
- import org.springframework.web.client.RestTemplate;
-
- import javax.annotation.Resource;
-
- @RestController
- @Slf4j
- public class OrderController {
-
- public static final String PAYMENT_URL="http://localhost:8001/";
-
- @GetMapping("/hello")
- public String getPaymentById() {
- return restTemplate.getForObject(PAYMENT_URL+"hello",String.class);
- }
总结来说:防止单点故障,实现高可用,即使一个Eureka宕机,其他的也可以使用
项目名:cloud-eureka-server7002
- <?xml version="1.0" encoding="UTF-8"?>
- <project xmlns="http://maven.apache.org/POM/4.0.0"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <parent>
- <artifactId>cloud2020</artifactId>
- <groupId>com.gzf.springcloud</groupId>
- <version>1.0-SNAPSHOT</version>
- </parent>
- <modelVersion>4.0.0</modelVersion>
-
- <artifactId>cloud-eureka-server7002</artifactId>
-
- <properties>
- <maven.compiler.source>8</maven.compiler.source>
- <maven.compiler.target>8</maven.compiler.target>
- </properties>
-
- <dependencies>
- <!--eureka-server-->
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
- </dependency>
- <!-- 引入自己定义的api通用包,可以使用Payment支付Entity -->
- <dependency>
- <groupId>com.gzf.springcloud</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>
- </project>
注意一句话,互相注册,相互守望
两个erueka服务端看作一个整体,编写yml配置是关键,单机和集群的写法不一样
下图是以前单机的
但是现在是两台机器,如果两个eureka服务端的实例名称都叫做 localhost,分不清,容易出错,主机名字要有一点区分,既然相互注册,相互守望,那么7001就要注册到7002,7002就要注册到7001
修改yml之前,需要修改映射配置文件
修改映射配置文件
找到C:Windows\System32\drivers\etc路径下的hosts文件
修改文件
1. 修改7001配置文件
修改前
修改后
server:
port: 7001
eureka:
instance:
hostname: eureka7001.com #eureka服务端的实例名称
client:
register-with-eureka: false #false表示不向注册中心注册自己。
fetch-registry: false #false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
service-url:
defaultZone: http://eureka7002.com:7002/eureka/ #相互注册 相互守望
编写7002配置文件
server: port: 7002 eureka: instance: hostname: eureka7002.com #eureka服务端的实例名称 client: register-with-eureka: false #false表示不向注册中心注册自己。 fetch-registry: false #false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务 service-url: defaultZone: http://eureka7001.com:7001/eureka/ #相互注册 相互守望
- package com.gzf.springcloud;
-
-
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
- import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
-
- @SpringBootApplication
- @EnableEurekaServer
- public class EurekaMain7002 {
-
- public static void main(String[] args) {
- SpringApplication.run(EurekaMain7002.class);
- }
- }
7001效果图
7002效果图
互相注册,互相守望
修改配置文件yml即可
80修改前
修改后
spring: application: name: cloud-order-service eureka: client: #表示是否将自己注册进EurekaServer默认为true。 register-with-eureka: true #是否从EurekaServer抓取已有的注册信息,默认为true。单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡 fetchRegistry: true service-url: defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka # 集群版
8001修改
修改前
修改后
server:
port: 8001
spring:
application:
name: cloud-payment-service
datasource:
type: com.alibaba.druid.pool.DruidDataSource # 当前数据源操作类型
driver-class-name: com.mysql.jdbc.Driver # mysql驱动包
url: jdbc:mysql://localhost:3306/db2019?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC
username: root
password: 123456
#新增Eureka配置
eureka:
client:
#表示是否将自己注册进EurekaServer默认为true。
register-with-eureka: true
#是否从EurekaServer抓取已有的注册信息,默认为true。单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡
fetchRegistry: true
service-url:
defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka # 集群版
mybatis:
mapperLocations: classpath:mapper/*.xml
type-aliases-package: com.atguigu.springcloud.entities # 所有Entity别名类所在包
EurekaServer怕宕机,搭建集群实现高可用
EurekaClient也怕宕机,也需要搭建集群是高可用,防止单点故障
名字:cloud-provider-payment8002 和 cloud-provider-payment8001 属于一个服务
- <?xml version="1.0" encoding="UTF-8"?>
- <project xmlns="http://maven.apache.org/POM/4.0.0"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <parent>
- <artifactId>cloud2020</artifactId>
- <groupId>com.gzf.springcloud</groupId>
- <version>1.0-SNAPSHOT</version>
- </parent>
- <modelVersion>4.0.0</modelVersion>
-
- <artifactId>cloud-provider-payment8002</artifactId>
-
- <dependencies>
- <!--eureka-client-->
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
- </dependency>
- <dependency><!-- 引入自己定义的api通用包,可以使用Payment支付Entity -->
- <groupId>com.gzf.springcloud</groupId>
- <artifactId>cloud-api-commons</artifactId>
- <version>${project.version}</version>
- </dependency>
- <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.mybatis.spring.boot</groupId>
- <artifactId>mybatis-spring-boot-starter</artifactId>
- </dependency>
- <dependency>
- <groupId>com.alibaba</groupId>
- <artifactId>druid-spring-boot-starter</artifactId>
- <version>1.1.10</version>
- </dependency>
- <!--mysql-connector-java-->
- <dependency>
- <groupId>mysql</groupId>
- <artifactId>mysql-connector-java</artifactId>
- </dependency>
- <!--jdbc-->
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-jdbc</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>
- <optional>true</optional>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-test</artifactId>
- <scope>test</scope>
- </dependency>
- </dependencies>
-
- </project>
server: port: 8002 spring: application: name: cloud-payment-service datasource: type: com.alibaba.druid.pool.DruidDataSource # 当前数据源操作类型 driver-class-name: org.gjt.mm.mysql.Driver # mysql驱动包 url: jdbc:mysql://localhost:3306/db2019?useUnicode=true&characterEncoding=utf-8&useSSL=false username: root password: 123456 eureka: client: #表示是否将自己注册进EurekaServer默认为true。 register-with-eureka: true #是否从EurekaServer抓取已有的注册信息,默认为true。单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡 fetchRegistry: true service-url: defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka # 集群版 mybatis: mapperLocations: classpath:mapper/*.xml type-aliases-package: com.atguigu.springcloud.entities # 所有Entity别名类所在包
注意:两个服务spring.application.name 服务实例名一致才能搭建集群,但端口号不能一致
我们来到服务调用方,发现根本调用不到8002,因为路径是写死的,如何解决?
做负载均衡
修改80端配置类
- package com.gzf.springcloud.config;
-
- import org.springframework.cloud.client.loadbalancer.LoadBalanced;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
- import org.springframework.web.client.RestTemplate;
-
- @Configuration
- public class ApplicationContextConfig {
-
- @Bean
- @LoadBalanced //开启restTemplate负载均衡
- public RestTemplate getRestTemplate(){
- return new RestTemplate();
- }
- }
新增注解 @LoadBalanced:开启restTemplate负载均衡
修改controller
- package com.gzf.springcloud.controller;
-
- import com.gzf.springcloud.entities.CommonResult;
- import com.gzf.springcloud.entities.Payment;
- import lombok.extern.slf4j.Slf4j;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.web.bind.annotation.*;
- import org.springframework.web.client.RestTemplate;
-
- import javax.annotation.Resource;
- import javax.print.DocFlavor;
-
- @RestController
- @Slf4j
- public class OrderController {
-
- // public static final String PAYMENT_URL="http://localhost:8001/payment/";
-
- //将IP改成服务名
- public static final String PAYMENT_URL="http://CLOUD-PAYMENT-SERVICE:8001/payment/";
- @Resource
- private RestTemplate restTemplate;
-
- @PostMapping("/hello")
- public String create() {
- return restTemplate.postForObject(PAYMENT_URL+"hello",payment,String.class);
- }
将原来写死的URL修改成 服务实例名
1. 显示主机名问题
可以配置隐藏,只暴露服务名,不暴露主机名(localhost)
修改服务提供者 8001,8002
yml配置文件 新增实例id: eureka.instance.instance-id
server:
port: 8001
spring:
application:
name: cloud-payment-service
datasource:
type: com.alibaba.druid.pool.DruidDataSource # 当前数据源操作类型
driver-class-name: com.mysql.jdbc.Driver # mysql驱动包
url: jdbc:mysql://localhost:3306/db2019?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC
username: root
password: 123456
#新增Eureka配置
eureka:
client:
#表示是否将自己注册进EurekaServer默认为true。
register-with-eureka: true
#是否从EurekaServer抓取已有的注册信息,默认为true。单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡
fetchRegistry: true
service-url:
defaultZone: ttp://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka # 集群版
instance:
instance-id: payment8001
mybatis:
mapperLocations: classpath:mapper/*.xml
type-aliases-package: com.atguigu.springcloud.entities # 所有Entity别名类所在包
8002 按照这个改即可
修改后效果图
2. 访问信息无ip地址问题
1.修改服务提供者 8001,8002
新增实例id: eureka.instance.prefer-ip-address = true 新增 问路径可以显示IP地址 配置
server:
port: 8001
spring:
application:
name: cloud-payment-service
datasource:
type: com.alibaba.druid.pool.DruidDataSource # 当前数据源操作类型
driver-class-name: com.mysql.jdbc.Driver # mysql驱动包
url: jdbc:mysql://localhost:3306/db2019?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC
username: root
password: 123456
#新增Eureka配置
eureka:
client:
#表示是否将自己注册进EurekaServer默认为true。
register-with-eureka: true
#是否从EurekaServer抓取已有的注册信息,默认为true。单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡
fetchRegistry: true
service-url:
defaultZone: ttp://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka # 集群版
instance:
instance-id: payment8001
prefer-ip-address: true #访问路径可以显示IP地址
mybatis:
mapperLocations: classpath:mapper/*.xml
type-aliases-package: com.atguigu.springcloud.entities # 所有Entity别名类所在包
2.修改后
就是一个新的注解标签叫@EnableDiscovery
现在我们已经完成了五个微服务的调用和实现
不排除微服务自身要对外提供一种功能,例如:我的IP是多少,我的服务名称是多少,我的端口号是多少。总体来说就是现在注册到Eureka这么多微服务,每个微服务他们各自的信息是什么?
总体来说就是:对于注册进eureka里面的微服务,可以通过服务发现来获取到该服务的信息
@EnableDiscoveryClient:代表开启Discovery 服务发现
- package com.gzf.springcloud;
-
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
- import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
- import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
-
- @SpringBootApplication
- @EnableEurekaClient // 代表是EnableEureka的客户端
- @EnableDiscoveryClient // 代表开启Discovery 服务发现
- public class PaymentMain8001 {
- public static void main(String[] args) {
- SpringApplication.run(PaymentMain8001.class);
- }
- }
- package com.gzf.springcloud.controller;
-
-
- import com.gzf.springcloud.entities.CommonResult;
- import com.gzf.springcloud.entities.Payment;
- import com.gzf.springcloud.service.PaymentService;
-
-
- import jdk.nashorn.internal.runtime.logging.Logger;
- import lombok.extern.slf4j.Slf4j;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.cloud.client.ServiceInstance;
- import org.springframework.cloud.client.discovery.DiscoveryClient;
- import org.springframework.web.bind.annotation.*;
-
- import javax.annotation.Resource;
- import java.util.List;
-
- @RestController
- @Slf4j
- public class PaymentController{
-
- //服务发现
- @Resource
- private DiscoveryClient discoveryClient;
-
- @GetMapping(value = "/discovery")
- public CommonResult<Object> discovery(){
- //在这个eureka注册,登陆好的微服务有哪些
- List<String> services = discoveryClient.getServices();
-
- for (String service : services) {
- log.info("service = " + service);
- }
-
- //在这个eureka注册的,此服务实例名称,有多少具体的ID可以获得(此时有两个8001和8002)
- List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-PAYMENT-SERVICE");
- for (ServiceInstance instance : instances) {
- //服务ID 服务ip 端口号
- log.info(instance.getServiceId()+"\t"+instance.getHost()+"\t"
- +instance.getPort()+"\t");
- }
- return new CommonResult<>().success(this.discoveryClient);
- }
- }
输出:
service = cloud-payment-service service = cloud-order-service CLOUD-PAYMENT-SERVICE 192.168.19.1 8001 CLOUD-PAYMENT-SERVICE 192.168.19.1 8002
JSON:
{ "code": 200, "message": "", "data": { "services": [ "cloud-payment-service", "cloud-order-service" ], "order": 0 } }
看到这段话 就说明Eureka进入了自我保护机制,
某一时刻某一个微服务不能用了Eureka不会立刻清理,依旧会对服务信息进行保存,属于CAP里的AP分支
默认情况下,如果EurekaServer在一定时间内没有接收到某个微服务实例的心跳,EurekaServer将会注销该实例(默认90秒)。但是当网络分区故障发生(延时、卡顿、拥挤)时,微服务与EurekaServer之间无法正常通信,以上行为可能变得非常危险了——因为微服务本身其实是健康的,此时本不应该注销这个微服务。Eureka通过“自我保护模式”来解决这个问题——当EurekaServer节点在短时间内丢失过多客户端时(可能发生了网络分区故障),那么这个节点就会进入自我保护模式。
在自我保护模式中,Eureka Server会保护服务注册表中的信息,不再注销任何服务实例。 它的设计哲学就是宁可保留错误的服务注册信息,也不盲目注销任何可能健康的服务实例。
综上,自我保护模式是一种应对网络异常的安全保护措施。它的架构哲学是宁可同时保留所有微服务(健康的微服务和不健康的微服务都会保留)也不盲目注销任何健康的微服务。使用自我保护模式,可以让Eureka集群更加的健壮、稳定。
A:高可用
P:分区容错性
出厂默认是开启的
eureka.server.enable-self-preservation=true
server:
port: 7001
spring:
application:
name: eureka-cluster-server
eureka:
instance:
hostname: eureka7001.com
client:
register-with-eureka: false
fetch-registry: false
service-url:
#defaultZone: http://eureka7002.com:7002/eureka,http://eureka7003.com:7003/eureka
defaultZone: http://eureka7001.com:7001/eureka
server:
#关闭自我保护机制,保证不可用服务被及时踢除
enable-self-preservation: false
eviction-interval-timer-in-ms: 2000
eureka.instance.lease-renewal-interval-in-seconds=30 eureka.instance.lease-expiration-duration-in-seconds=90
server:
port: 8001
###服务名称(服务注册到eureka名称)
spring:
application:
name: cloud-provider-payment
eureka:
client: #服务提供者provider注册进eureka服务列表内
service-url:
register-with-eureka: true
fetch-registry: true
# cluster version
#defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka,http://eureka7003.com:7003/eureka
# singleton version
defaultZone: http://eureka7001.com:7001/eureka
#心跳检测与续约时间
#开发时设置小些,保证服务关闭后注册中心能即使剔除服务
instance:
#Eureka客户端向服务端发送心跳的时间间隔,单位为秒(默认是30秒)
lease-renewal-interval-in-seconds: 1
#Eureka服务端在收到最后一次心跳后等待时间上限,单位为秒(默认是90秒),超时将剔除服务
lease-expiration-duration-in-seconds: 2
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。