第五章 Eureka服务注册与发现(尚硅谷SpringCloud)

闲聊 闲聊 1032 人阅读 | 0 人回复

<
文章目次



1、Eureka根柢常识


  • 甚么是效劳管理
  1.springcloud启拆了Netflix公司开辟的Eureka模块去完成效劳管理。
2.正在传统的rpc长途垂钓共框架中,办理每一个效劳取效劳之间依靠干系比力庞大,办理比力庞大,以是需求利用效劳管理,办理效劳取效劳之间依靠干系,能够完成效劳挪用、背载平衡、容错等,完成效劳发明取注册。

  • 甚么是效劳注册取发明
  1.Eureka接纳了CS的设想架构,Eureka Server做为效劳注册功用的效劳器,它是效劳注册中间。而体系中的其他微效劳,利用Eureka的客户端毗连诶到Eureka Server并保持心跳毗连。如许体系的保护职员就能够经由过程Eureka Server去监控体系中各个微效劳能否一般运转。
2.正在效劳注册取发明中,有一个注册中间。当效劳器启动的时分,会把当前本人效劳器的疑息比如:效劳地点通信地点等以别号方法注册到注册中间上。另外一圆(消耗者|效劳供给者),以该别号的方法来注册中间上获得实践的效劳通信地点,然后再完成当地RPC挪用RPC长途挪用框架中心设想思惟:正在于注册中间,由于利用注册中间办理每一个效劳取效劳之间的一个依靠干系(效劳管理观点)。正在任何rpc长途框架中,城市有一个注册中间(寄存效劳地点相干疑息(接心地点))
ps:下左图是Eureka体系架构,左图是Dubbo的架构,请比照:
150009sh9zy5mmuht6sb6a.jpg


  • Eureka包含两组件:Eureka Server战Eureka Client
  1.Eureka Server供给效劳注册效劳:
问:各个微效劳节面经由过程设置启动后,会正在EurekaServer中停止注册,如许EurekaServer中的效劳注册表中将会存储一切可用效劳节面的疑息,效劳节面的疑息能够正在界里中曲寓目到。
2.Eureka Client经由过程注册中间停止会见:
问:是一个java客户端,用于简化Eureka Server的交互,客户端同时也具有一个内乱置的、利用轮询(round-robin)背载算法的背载平衡器。正在使用启动后,将会背Eureka Server收收心跳(默许周期为30秒)。假如Eureka Server正在多个心跳周期内乱出有领受到某个节面的心跳,EurekaServer将会从效劳注册表中把那个效劳节面移除(默许90秒)。
2、单机Eureka构建步调

1.IDEA天生eurekaServer端效劳注册中间
相似物业公司

ps:eureka Server效劳端装置
1.成立module
  cloud-eureka-server7001
2.改pom
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5.     <parent>
  6.         <artifactId>mscloud03</artifactId>
  7.         <groupId>com.atguigu.springcloud</groupId>
  8.         <version>1.0-SNAPSHOT</version>
  9.     </parent>
  10.     <modelVersion>4.0.0</modelVersion>
  11.     <artifactId>cloud-eureka-server7001</artifactId>
  12.     <dependencies>
  13.         <!--eureka-server-->
  14.         <dependency>
  15.             <groupId>org.springframework.cloud</groupId>
  16.             <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
  17.         </dependency>
  18.         <!-- 引进本人界说的api通用包,可使用Payment付出Entity -->
  19.         <dependency>
  20.             <groupId>com.atguigu.springcloud</groupId>
  21.             <artifactId>cloud-api-commons</artifactId>
  22.             <version>${project.version}</version>
  23.         </dependency>
  24.         <!--boot web actuator-->
  25.         <dependency>
  26.             <groupId>org.springframework.boot</groupId>
  27.             <artifactId>spring-boot-starter-web</artifactId>
  28.         </dependency>
  29.         <dependency>
  30.             <groupId>org.springframework.boot</groupId>
  31.             <artifactId>spring-boot-starter-actuator</artifactId>
  32.         </dependency>
  33.         <!--普通通用设置-->
  34.         <dependency>
  35.             <groupId>org.springframework.boot</groupId>
  36.             <artifactId>spring-boot-devtools</artifactId>
  37.             <scope>runtime</scope>
  38.             <optional>true</optional>
  39.         </dependency>
  40.         <dependency>
  41.             <groupId>org.projectlombok</groupId>
  42.             <artifactId>lombok</artifactId>
  43.         </dependency>
  44.         <dependency>
  45.             <groupId>org.springframework.boot</groupId>
  46.             <artifactId>spring-boot-starter-test</artifactId>
  47.             <scope>test</scope>
  48.         </dependency>
  49.         <dependency>
  50.             <groupId>junit</groupId>
  51.             <artifactId>junit</artifactId>
  52.         </dependency>
  53.     </dependencies>
  54. </project>
复造代码
ps:1.x战2.x的比照分析
  1. 从前的老版本(当前利用2018)
  2. <dependency>
  3.         <groupId>org.springframework.cloud</groupId>
  4.         <artifactId>spring-cloud-starter-eureka</artifactId>
  5. </dependency>
  6. 如今新版本(当前利用2020.2)
  7. <dependency>
  8.     <groupId>org.springframework.cloud</groupId>
  9.     <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
  10. </dependency>
复造代码
3.写yml
  1. server:
  2.   port: 7001
  3. eureka:
  4.   instance:
  5.     hostname: localhost #eureka效劳真个真例称号
  6.   client:
  7.     #false暗示没有背注册中间注册本人。
  8.     register-with-eureka: false
  9.     #false暗示本人端便是注册中间,我的职责便是保护效劳真例,其实不需求来检索效劳
  10.     fetch-registry: false
  11.     service-url:
  12.     #设置取Eureka Server交互的地点查询效劳战注册效劳皆需求依靠那个地点。
  13.       defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
复造代码
4.主启动类
  1. package com.atguigu.springcloud;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
  5. /**
  6. * @auther
  7. * @create 2020-01-28 20:40
  8. */
  9. @SpringBootApplication
  10. @EnableEurekaServer
  11. public class EurekaMain7001
  12. {
  13.     public static void main(String[] args)
  14.     {
  15.         SpringApplication.run(EurekaMain7001.class,args);
  16.     }
  17. }
复造代码
5.测试
  网址:http://localhost:7001/
成果:能够胜利进进eureka界里分析装置胜利
150010jbwnqe1cpd4td1q6.jpg

2.EurekaClient端cloud-provider-payment8001
将注册进EurekaServer成为效劳供给者provider,相似尚硅谷黉舍对中供给授课效劳

1.成立module
  cloud-provider-payment8001
2.改pom
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5.     <parent>
  6.         <artifactId>cloud2020</artifactId>
  7.         <groupId>com.itcast.springcloud</groupId>
  8.         <version>1.0-SNAPSHOT</version>
  9.     </parent>
  10.     <modelVersion>4.0.0</modelVersion>
  11.     <artifactId>cloud-provider-payment8001</artifactId>
  12.     <dependencies>
  13.         <!--eureka-client-->
  14.         <dependency>
  15.             <groupId>org.springframework.cloud</groupId>
  16.             <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  17.         </dependency>
  18.         <dependency><!-- 引进本人界说的api通用包,可使用Payment付出Entity -->
  19.             <groupId>com.itcast.springcloud</groupId>
  20.             <artifactId>cloud-api-commons</artifactId>
  21.             <version>${project.version}</version>
  22.         </dependency>
  23.         <dependency>
  24.             <groupId>org.springframework.boot</groupId>
  25.             <artifactId>spring-boot-starter-web</artifactId>
  26.         </dependency>
  27.         <dependency>
  28.             <groupId>org.springframework.boot</groupId>
  29.             <artifactId>spring-boot-starter-actuator</artifactId>
  30.         </dependency>
  31.         <dependency>
  32.             <groupId>org.mybatis.spring.boot</groupId>
  33.             <artifactId>mybatis-spring-boot-starter</artifactId>
  34.         </dependency>
  35.         <dependency>
  36.             <groupId>com.alibaba</groupId>
  37.             <artifactId>druid-spring-boot-starter</artifactId>
  38.             <version>1.1.10</version>
  39.         </dependency>
  40.         <!--mysql-connector-java-->
  41.         <dependency>
  42.             <groupId>mysql</groupId>
  43.             <artifactId>mysql-connector-java</artifactId>
  44.         </dependency>
  45.         <!--jdbc-->
  46.         <dependency>
  47.             <groupId>org.springframework.boot</groupId>
  48.             <artifactId>spring-boot-starter-jdbc</artifactId>
  49.         </dependency>
  50.         <dependency>
  51.             <groupId>org.springframework.boot</groupId>
  52.             <artifactId>spring-boot-devtools</artifactId>
  53.             <scope>runtime</scope>
  54.             <optional>true</optional>
  55.         </dependency>
  56.         <dependency>
  57.             <groupId>org.projectlombok</groupId>
  58.             <artifactId>lombok</artifactId>
  59.             <optional>true</optional>
  60.         </dependency>
  61.         <dependency>
  62.             <groupId>org.springframework.boot</groupId>
  63.             <artifactId>spring-boot-starter-test</artifactId>
  64.             <scope>test</scope>
  65.         </dependency>
  66.     </dependencies>
  67. </project>
复造代码
3.写yml
  1. server:
  2.   port: 8001
  3. #效劳名
  4. spring:
  5.   application:
  6.     name: cloud-payment-service
  7.   datasource:
  8.     type: com.alibaba.druid.pool.DruidDataSource            # 当前数据源操纵范例
  9.     driver-class-name: org.gjt.mm.mysql.Driver              # mysql驱动包 com.mysql.jdbc.Driver
  10.     url: jdbc:mysql://localhost:3306/db2019?useUnicode=true&characterEncoding=utf-8&useSSL=false
  11.     username: root
  12.     password: root
  13. eureka:
  14.   client:
  15.     #暗示能否将本人注册进EurekaServer默许为true。
  16.     register-with-eureka: true
  17.     #能否从EurekaServer抓与已有的注册疑息,默许为true。单节面无所谓,散群必需设置为true才气共同ribbon利用背载平衡
  18.     fetchRegistry: true
  19.     service-url:
  20.       defaultZone: http://localhost:7001/eureka
  21. mybatis:
  22.   mapperLocations: classpath:mapper/*.xml
  23.   type-aliases-package: com.itcast.springcloud.entities    # 一切Entity别号类地点包
复造代码
4.主启动
  1. package com.itcast.springcloud;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
  5. /**
  6. * @author henry
  7. * @version 1.0
  8. * @date 2021/8/28 0028 16:33
  9. */
  10. @SpringBootApplication
  11. @EnableEurekaClient
  12. public class PaymentMain8001 {
  13.     public static void main(String[] args) {
  14.         SpringApplication.run(PaymentMain8001.class,args);
  15.     }
  16. }
复造代码
5.测试
  1.先启动EurekaServer
2.http://localhost/7001
成果:
150010k5sfpth0hkph5sft.jpg

6.微效劳注册名设置分析
150011vegg76f9w6wwb9nw.jpg

7.自我保护机造
150011o3ssms9ss3tsgxtm.jpg

3.EurekaClient端cloud-consumer-order80
将注册进EurekaServer成为效劳消耗者consumer,相似去尚硅谷上课消耗的列位同窗

1.成立模块
  cloud-consumer-order80
2.该pom文件
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5.     <parent>
  6.         <artifactId>mscloud03</artifactId>
  7.         <groupId>com.atguigu.springcloud</groupId>
  8.         <version>1.0-SNAPSHOT</version>
  9.     </parent>
  10.     <modelVersion>4.0.0</modelVersion>
  11.     <artifactId>cloud-consumer-order80</artifactId>
  12.     <dependencies>
  13.         <dependency>
  14.             <groupId>org.springframework.cloud</groupId>
  15.             <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  16.         </dependency>
  17.         <dependency><!-- 引进本人界说的api通用包,可使用Payment付出Entity -->
  18.             <groupId>com.atguigu.springcloud</groupId>
  19.             <artifactId>cloud-api-commons</artifactId>
  20.             <version>${project.version}</version>
  21.         </dependency>
  22.         <dependency>
  23.             <groupId>org.springframework.boot</groupId>
  24.             <artifactId>spring-boot-starter-web</artifactId>
  25.         </dependency>
  26.         <dependency>
  27.             <groupId>org.springframework.boot</groupId>
  28.             <artifactId>spring-boot-starter-actuator</artifactId>
  29.         </dependency>
  30.         <dependency>
  31.             <groupId>org.springframework.boot</groupId>
  32.             <artifactId>spring-boot-devtools</artifactId>
  33.             <scope>runtime</scope>
  34.             <optional>true</optional>
  35.         </dependency>
  36.         <dependency>
  37.             <groupId>org.projectlombok</groupId>
  38.             <artifactId>lombok</artifactId>
  39.             <optional>true</optional>
  40.         </dependency>
  41.         <dependency>
  42.             <groupId>org.springframework.boot</groupId>
  43.             <artifactId>spring-boot-starter-test</artifactId>
  44.             <scope>test</scope>
  45.         </dependency>
  46.     </dependencies>
  47. </project>
复造代码
3.修正yml文件
  1. server:
  2.   port: 80
  3. spring:
  4.   application:
  5.     name: cloud-order-service
  6. eureka:
  7.   client:
  8.     #暗示能否将本人注册进EurekaServer默许为true。
  9.     register-with-eureka: true
  10.     #能否从EurekaServer抓与已有的注册疑息,默许为true。单节面无所谓,散群必需设置为true才气共同ribbon利用背载平衡
  11.     fetchRegistry: true
  12.     service-url:
  13.       defaultZone: http://localhost:7001/eureka
复造代码
4.修正主启动类
  1. package com.itcast.springcloud;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
  5. /**
  6. * @author henry
  7. * @version 1.0
  8. * @date 2021/8/28 0028 21:11
  9. */
  10. @SpringBootApplication
  11. @EnableEurekaClient
  12. public class OrderMain80 {
  13.     public static void main(String[] args) {
  14.         SpringApplication.run(OrderMain80.class,args);
  15.     }
  16. }
复造代码
5.测试
  1.先启动EurekaServer,7001效劳
2.再启动效劳供给者provider,8001效劳
3.eureka效劳器疑息以下:
150011guavaff6sk7avha7.jpg

4.输进网址:http://localhost/consumer/payment/get/31展现的成果以下分析效劳注册胜利:
150012pl0tmklobno5y9mn.jpg

4.bug
  1.项目提醒以下毛病:
Failed to bind properties under ‘eureka.client.service-url’ to java.util.Map
2.缘故原由:
150012j22h0ctzub0g7sr7.png

3、散群Eurake构建步调

1.Eureka散群道理分析:
150012mc4ksypelsell94e.jpg

标题问题:微效劳RPC长途效劳挪用最中心的是甚么
  下可用:下可用,试念您的注册中间只要一个only one,它出毛病了那便“斥责斥责”了,会招致全部微效劳情况不成用,以是
打点法子:拆建Eureka注册中间散群,完成背载平衡+毛病容错。
ps:散群的注册道理(一句话):
  互相注册,互相守视
150012c1ksisy5yos0io1y.png

2.EurekaServer散群情况构建步调:
1.新建cloud-eureka-server7002
  参考cloud-eureka-server7001
2.改pom
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5.     <parent>
  6.         <artifactId>mscloud03</artifactId>
  7.         <groupId>com.atguigu.springcloud</groupId>
  8.         <version>1.0-SNAPSHOT</version>
  9.     </parent>
  10.     <modelVersion>4.0.0</modelVersion>
  11.     <artifactId>cloud-eureka-server7002</artifactId>
  12.     <dependencies>
  13.         <!--eureka-server-->
  14.         <dependency>
  15.             <groupId>org.springframework.cloud</groupId>
  16.             <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
  17.         </dependency>
  18.         <!-- 引进本人界说的api通用包,可使用Payment付出Entity -->
  19.         <dependency>
  20.             <groupId>com.atguigu.springcloud</groupId>
  21.             <artifactId>cloud-api-commons</artifactId>
  22.             <version>${project.version}</version>
  23.         </dependency>
  24.         <!--boot web actuator-->
  25.         <dependency>
  26.             <groupId>org.springframework.boot</groupId>
  27.             <artifactId>spring-boot-starter-web</artifactId>
  28.         </dependency>
  29.         <dependency>
  30.             <groupId>org.springframework.boot</groupId>
  31.             <artifactId>spring-boot-starter-actuator</artifactId>
  32.         </dependency>
  33.         <!--普通通用设置-->
  34.         <dependency>
  35.             <groupId>org.springframework.boot</groupId>
  36.             <artifactId>spring-boot-devtools</artifactId>
  37.             <scope>runtime</scope>
  38.             <optional>true</optional>
  39.         </dependency>
  40.         <dependency>
  41.             <groupId>org.projectlombok</groupId>
  42.             <artifactId>lombok</artifactId>
  43.         </dependency>
  44.         <dependency>
  45.             <groupId>org.springframework.boot</groupId>
  46.             <artifactId>spring-boot-starter-test</artifactId>
  47.             <scope>test</scope>
  48.         </dependency>
  49.         <dependency>
  50.             <groupId>junit</groupId>
  51.             <artifactId>junit</artifactId>
  52.         </dependency>
  53.     </dependencies>
  54. </project>
复造代码
3.修正映照设置
  1.找到C:\Windows\System32\drivers\etc途径下的hosts文件
150013par4v6im4kn41r64.png

2.修正映照设置增加进hosts文件
127.0.0.1 eureka7001.com
127.0.0.1 eureka7002.com
4.写yml(从前单机)
ps:次要修正hostname参数战defaultZone参数
7001yml:
  1. server:
  2.   port: 7001
  3. eureka:
  4.   instance:
  5.     hostname: eureka7001.com #eureka效劳真个真例称号
  6.   client:
  7.     register-with-eureka: false     #false暗示没有背注册中间注册本人。
  8.     fetch-registry: false     #false暗示本人端便是注册中间,我的职责便是保护效劳真例,其实不需求来检索效劳
  9.     service-url:
  10.       defaultZone: http://eureka7002.com:7002/eureka/
复造代码
7002yml:
  1. server:
  2.   port: 7002
  3. eureka:
  4.   instance:
  5.     hostname: eureka7002.com #eureka效劳真个真例称号
  6.   client:
  7.     register-with-eureka: false     #false暗示没有背注册中间注册本人。
  8.     fetch-registry: false     #false暗示本人端便是注册中间,我的职责便是保护效劳真例,其实不需求来检索效劳
  9.     service-url:
  10.       defaultZone: http://eureka7001.com:7001/eureka/
复造代码
5.主启动类
  1. /**
  2. * @author henry
  3. * @version 1.0
  4. * @date 2021/9/4 0004 21:31
  5. */
  6. @SpringBootApplication
  7. @EnableEurekaServer
  8. public class EurekaMain7002 {
  9.     public static void main(String[] args) {
  10.         SpringApplication.run(EurekaMain7002.class,args);
  11.     }
  12. }
复造代码
6.测试
经由过程以下以下域名能够胜利会见(7002端心同理,互相注册)
150013shv7jzstsuoihses.jpg

3.将付出效劳8001微效劳公布到上里2台Eureka散群设置中:
1.修正yml文件
  1. 修正参数:
  2. defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka  # 散群版
复造代码
4.将定单效劳80微效劳公布到上里2台Eureka散群设置中:
1.修正yml文件
  1. 修正参数:
  2. defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka  # 散群版
复造代码
5.测试01:
  1.先要启动EurekaServer,7001/7002效劳
2.再要启动效劳供给者provider,8001
3.再要启动消耗者,80
4.输进http://eureka7002.com:7002/战http://eureka7001.com:7001/能够看到8001效劳战80效劳皆注册到效劳中间了
5.输进网址http://localhost/consumer/payment/get/31能够一般返回数据
6.付出效劳供给者8001散群情况的拆建:
1.新建模块cloud-provider-payment8002
  参考cloud-provider-payment8001
2.改pom(参考8001)
3.写yml(参考8001)
4.主启动(参考8001)
5.营业类(间接从8001粘)
6.修正8001/8002的Controller(次要是将当前效劳名注进,并挨印出去能够曲寓目到挪用的是哪一个效劳)
  1. package com.itcast.springcloud.controller;
  2. import com.itcast.springcloud.entities.CommonResult;
  3. import com.itcast.springcloud.entities.Payment;
  4. import com.itcast.springcloud.service.PaymentService;
  5. import lombok.extern.slf4j.Slf4j;
  6. import org.springframework.beans.factory.annotation.Value;
  7. import org.springframework.web.bind.annotation.*;
  8. import javax.annotation.Resource;
  9. /**
  10. * @author rui
  11. * @version 1.0
  12. * @date 2021/8/28 0028 17:53
  13. */
  14. @RestController
  15. @Slf4j     //挨印日记
  16. public class PaymentController {
  17.     @Resource
  18.     private PaymentService paymentService;
  19.     @Value("${server.port}")
  20.     private String serverPort;
  21.     /*接纳restful气势派头恳求*/
  22.     @PostMapping(value = "/payment/create")
  23.     public CommonResult create(@RequestBody Payment payment){
  24.        int result  = paymentService.create(payment);
  25.        log.info("*****插进成果:"+result);
  26.        if (result > 0){
  27.            return new CommonResult(200,"插进数据库胜利,serverPort:"+serverPort,result);
  28.        }else {
  29.            return new CommonResult(444,"插进数据库失利",null);
  30.        }
  31.     }
  32.     @GetMapping(value = "/payment/get/{id}")
  33.     public CommonResult getPaymentById(@PathVariable("id") Long id){
  34.         Payment payment  = paymentService.getPaymentById(id);
  35.         log.info("*****插进成果:"+payment);
  36.         if (payment != null){
  37.             return new CommonResult(200,"查询胜利,serverPort:"+serverPort,payment);
  38.         }else {
  39.             return new CommonResult(444,"出有对应记载,查询ID:"+id,null);
  40.         }
  41.     }
  42. }
复造代码
7.背载平衡:
1.呈现的bug(消耗者类中定单效劳会见地点不克不及写逝世)
  1. //private static final String PAYMENT_URL = "http://localhost:8001";
  2.     // 经由过程正在eureka上注册过的微效劳称号挪用
  3.     private static final String PAYMENT_URL = "http://CLOUD-PAYMENT-SERVICE";
复造代码
2.利用@LoadBalanced注解付与RestTemplate背载平衡的才能
3.ApplicationContextBean(增加@LoadBalanced注解)
  1. package com.itcast.springcloud.config;
  2. import org.springframework.cloud.client.loadbalancer.LoadBalanced;
  3. import org.springframework.context.annotation.Bean;
  4. import org.springframework.context.annotation.Configuration;
  5. import org.springframework.web.client.RestTemplate;
  6. /**
  7. * @author henry
  8. * @version 1.0
  9. * @date 2021/8/28 0028 21:23
  10. */
  11. @Configuration
  12. public class ApplicationContextConfig {
  13.     @Bean
  14.     @LoadBalanced //利用@LoadBalanced注解付与RestTemplate背载平衡的才能
  15.     public RestTemplate getRestTemplate(){
  16.         return new RestTemplate();
  17.     }
  18. }
  19. //以上的@bean注解相似于以下的设置文件,将此工具参加到spring容器中
  20. //applicationtext.xml <bean id = "" class="">
复造代码
8.测试02:
  1.先要启动EurekaServer,7001/7002效劳
2.再要启动效劳供给者provider,8001/8002效劳
3.http://localhost/consumer/payment/get/31
4.成果:
背载平衡结果到达(8001/8002端心瓜代呈现)
5.Ribbon战Eureka整开后Consumer能够间接挪用效劳而不用再体贴地点战端标语,且该效劳另有背载功用了。(前面的章节)
4、actuator微效劳疑息完竣

1.主机称号:效劳称号修正
1.当前标题问题:
150013atzlalqa0wzzxzq8.jpg

露有主机名
2.修正修正cloud-provider-payment8001的yml文件
ps:增加了instance属性:
  1. eureka:
  2.   client:
  3.     #暗示能否将本人注册进EurekaServer默许为true。
  4.     register-with-eureka: true
  5.     #能否从EurekaServer抓与已有的注册疑息,默许为true。单节面无所谓,散群必需设置为true才气共同ribbon利用背载平衡
  6.     fetchRegistry: true
  7.     service-url:
  8.       defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka  # 散群版
  9.       #defaultZone: http://localhost:7001/eureka  # 单机版
  10.   instance:
  11.     instance-id: payment8001
复造代码
3.修正后形态
150014y9x3j06rnn3hirm9.jpg

2.会见疑息有IP疑息提醒
1.当前标题问题:
出有ip提醒
2.修正cloud-provider-payment8001
ps:正在instance属性下增加prefer-ip-address属性
  1. eureka:
  2.   client:
  3.     #暗示能否将本人注册进EurekaServer默许为true。
  4.     register-with-eureka: true
  5.     #能否从EurekaServer抓与已有的注册疑息,默许为true。单节面无所谓,散群必需设置为true才气共同ribbon利用背载平衡
  6.     fetchRegistry: true
  7.     service-url:
  8.       defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka  # 散群版
  9.       #defaultZone: http://localhost:7001/eureka  # 单机版
  10.   instance:
  11.     instance-id: payment8001    prefer-ip-address: true     #会见途径能够表示IP地点
复造代码
3.修正以后
150014wdrudg7o8d33tgo7.jpg

5、效劳发明Discovery

1.关于注册进eureka内里的微效劳,能够经由过程效劳发明去得到该效劳的疑息
2.修正cloud-provider-payment8001的Controller
ps:增加discovery获得效劳疑息
  1. package com.atguigu.springcloud.controller;
  2. import com.atguigu.springcloud.entities.CommonResult;
  3. import com.atguigu.springcloud.entities.Payment;
  4. import com.atguigu.springcloud.service.PaymentService;
  5. import lombok.extern.slf4j.Slf4j;
  6. import org.springframework.beans.factory.annotation.Value;
  7. import org.springframework.cloud.client.ServiceInstance;
  8. import org.springframework.cloud.client.discovery.DiscoveryClient;
  9. import org.springframework.web.bind.annotation.*;
  10. import javax.annotation.Resource;
  11. import javax.servlet.http.HttpServletRequest;
  12. import java.util.List;
  13. import java.util.concurrent.TimeUnit;
  14. /**
  15. * @auther zzyy
  16. * @create 2020-01-27 21:17
  17. */
  18. @RestController
  19. @Slf4j
  20. public class PaymentController
  21. {
  22.     @Value("${server.port}")
  23.     private String serverPort;
  24.     @Resource
  25.     private PaymentService paymentService;
  26.     @Resource
  27.     private DiscoveryClient discoveryClient;
  28.     @PostMapping(value = "/payment/create")
  29.     public CommonResult create(@RequestBody Payment payment)
  30.     {
  31.         int result = paymentService.create(payment);
  32.         log.info("*****插进操纵返回成果:" + result);
  33.         if(result > 0)
  34.         {
  35.             return new CommonResult(200,"插进胜利,返回成果"+result+"\t 效劳端心:"+serverPort,payment);
  36.         }else{
  37.             return new CommonResult(444,"插进失利",null);
  38.         }
  39.     }
  40.     @GetMapping(value = "/payment/get/{id}")
  41.     public CommonResult<Payment> getPaymentById(@PathVariable("id") Long id)
  42.     {
  43.         Payment payment = paymentService.getPaymentById(id);
  44.         log.info("*****查询成果:{}",payment);
  45.         if (payment != null) {
  46.             return new CommonResult(200,"查询胜利"+"\t 效劳端心:"+serverPort,payment);
  47.         }else{
  48.             return new CommonResult(444,"出有对应记载,查询ID: "+id,null);
  49.         }
  50.     }
  51.     @GetMapping(value = "/payment/discovery")
  52.     public Object discovery()
  53.     {
  54.         List<String> services = discoveryClient.getServices();
  55.         for (String element : services) {
  56.             System.out.println(element);
  57.         }
  58.         List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-PAYMENT-SERVICE");
  59.         for (ServiceInstance element : instances) {
  60.             System.out.println(element.getServiceId() + "\t" + element.getHost() + "\t" + element.getPort() + "\t"
  61.                     + element.getUri());
  62.         }
  63.         return this.discoveryClient;
  64.     }
  65. }
复造代码
3.8001主启动类
  增加@EnableDiscoveryClient注解
4.自测
  1.先要启动EurekaServer
2.再启动8001主启动类,需求稍等一会女
3.http://localhost:8001/payment/discovery
幻想成果:
前台呈现以下两效劳:
150014j06fb117xxxdmqmk.png

布景能够挨印出响应日志:
150015ugewfn8ugznzzg4r.jpg

6、eureka自我保护

1.毛病征象
  1.概述
保护形式次要用于一组客户端战Eureka Server之间存正在收集分区场景下的保护。一旦进进保护形式,Eureka Server将会测验考试保护其效劳注册表中的疑息,没有再删除效劳注册表中的数据,也便是没有会登记任何微效劳。
2.假如正在Eureka Server的尾页看到以下那段提醒,则分析Eureka进进了保护形式:
EMERGENCY! EUREKA MAY BE INCORRECTLY CLAIMING INSTANCES ARE UP WHEN THEY’RE NOT.
RENEWALS ARE LESSER THAN THRESHOLD AND HENCE THE INSTANCES ARE NOT BEING EXPIRED JUST TO BE SAFE
150015li4fzv1tnjfdjvbm.png

2.招致缘故原由
  1.为何会发生Eureka自我保护机造?
为了避免EurekaClient能够一般运转,可是 取 EurekaServer收集欠亨情况下,EurekaServer没有会立刻将EurekaClient效劳剔除
2.甚么是自我保护形式?
默许情况下,假如EurekaServer正在必然工夫内乱出有领受到某个微效劳真例的心跳,EurekaServer将会登记该真例(默许90秒)。可是当收集分区毛病发作(延时、卡顿、拥堵)时,微效劳取EurekaServer之间没法一般通讯,以上举动大要变得十分伤害了——由于微效劳本人实际上是健康的,此时本不该该登记那个微效劳。Eureka经由过程“自我保护形式”去打点那个标题问题——当EurekaServer节面正在短工夫内乱丧失过量客户端时(大要发作了收集分区毛病),那末那个节面便会进进自我保护形式。
150015ve6jx7wwe6llo4zz.jpg

3.正在自我保护形式中,Eureka Server会保护效劳注册表中的疑息,没有再登记任何效劳真例。
它的设想哲教便是宁肯保留毛病的效劳注册疑息,也没有自觉登记任何大要健康的效劳真例。一句话解说:好逝世没有如好在世
4.综上,自我保护形式是一种应对收集非常的宁静保护步伐。它的架构哲教是宁肯同时保留一切微效劳(健康的微效劳战没有健康的微效劳城市保留)也没有自觉登记任何健康的微效劳。利用自我保护形式,可让Eureka散群愈加的巩固、不变。
ps:1.一句话:某时辰一个微效劳不成用了,Eureka没有会like清算,照旧会对微效劳的疑息停止保留
2.属于CAP内里对的AP分收
3.怎样抑制自我保护
1.注册中间eureakeServer端7001
  1.出厂默许,自我保护机造是开启的
2.利用eureka.server.enable-self-preservation = false 能够禁用自我保护形式
150016v0n0i4tbfbyn0n9b.jpg

3.封闭结果
150016ijhhmr37d7ham7nj.jpg

4.正在eurekaServer端7001处设置封闭自我保护机造
2.消费者客户端eureakeClient端8001
  1.默许
eureka.instance.lease-renewal-interval-in-seconds=30(默许30秒)
eureka.instance.lease-expiration-duration-in-seconds=90(默许90秒)
2.设置
150017ty8sw78bp51wmvyp.jpg

3.测试
7001战8001皆设置完成;
先启动7001再启动8001
150017n0wnbbuu2z6cxug0.jpg

  先封闭8001(即刻被删除)
150017luyulyvvhx0xezf5.jpg


免责声明:假如进犯了您的权益,请联络站少,我们会实时删除侵权内乱容,感谢协作!
1、本网站属于个人的非赢利性网站,转载的文章遵循原作者的版权声明,如果原文没有版权声明,按照目前互联网开放的原则,我们将在不通知作者的情况下,转载文章;如果原文明确注明“禁止转载”,我们一定不会转载。如果我们转载的文章不符合作者的版权声明或者作者不想让我们转载您的文章的话,请您发送邮箱:Cdnjson@163.com提供相关证明,我们将积极配合您!
2、本网站转载文章仅为传播更多信息之目的,凡在本网站出现的信息,均仅供参考。本网站将尽力确保所提供信息的准确性及可靠性,但不保证信息的正确性和完整性,且不对因信息的不正确或遗漏导致的任何损失或损害承担责任。
3、任何透过本网站网页而链接及得到的资讯、产品及服务,本网站概不负责,亦不负任何法律责任。
4、本网站所刊发、转载的文章,其版权均归原作者所有,如其他媒体、网站或个人从本网下载使用,请在转载有关文章时务必尊重该文章的著作权,保留本网注明的“稿件来源”,并自负版权等法律责任。
回复 关闭延时

使用道具 举报

 
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则