• Eureka应用及高可用集群


    1. 单实例Eureka Server—>访问管理界面—>Eureka Server集群
    2. 服务提供者(简历微服务注册到集群)
    3. 服务消费者(自动投递微服务注册到集群/从Eureka Server集群获取服务信息)
    4. 完成调用

    1、搭建单例Eureka Server服务注册中心

    lagou-service-resume 8080-----
    lagou-service-autodeliver 8090----
    lagou-cloud-eureka-server 8761----

    基于Maven构建SpringBoot⼯程,在SpringBoot⼯程之上搭建EurekaServer服务(lagou-cloud-eureka-server-8761)

    (1)lagou-parent中引入Spring Cloud 依赖

            Spring Cloud 是一个综合的项目,下面有很多子项目,比如eureka子项目(版本号 1.x.x)

    1. <dependencyManagement>
    2. <dependencies>
    3. <dependency>
    4. <groupId>org.springframework.cloudgroupId>
    5. <artifactId>spring-cloud-dependenciesartifactId>
    6. <version>Greenwich.RELEASEversion>
    7. <type>pomtype>
    8. <scope>importscope>
    9. dependency>
    10. dependencies>
    11. dependencyManagement>

    (2)pom.xml

    1. "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>lagou-parentartifactId>
    7. <groupId>com.lagougroupId>
    8. <version>1.0-SNAPSHOTversion>
    9. parent>
    10. <modelVersion>4.0.0modelVersion>
    11. <artifactId>lagou-cloud-eureka-server-8761artifactId>
    12. <dependencies>
    13. <dependency>
    14. <groupId>org.springframework.cloudgroupId>
    15. <artifactId>spring-cloud-starter-netflix-eureka-serverartifactId>
    16. dependency>
    17. dependencies>
    18. <properties>
    19. <maven.compiler.source>11maven.compiler.source>
    20. <maven.compiler.target>11maven.compiler.target>
    21. properties>
    22. project>

    注意:在父工程的pom文件中手动引入jaxb的jar,因为Jdk9之后默认没有加载该模块,EurekaServer使用到,所以需要手动导入,否则EurekaServer服务无法启动。

      父工程pom.xml

    1. <dependency>
    2. <groupId>com.sun.xml.bindgroupId>
    3. <artifactId>jaxb-coreartifactId>
    4. <version>2.2.11version>
    5. dependency>
    6. <dependency>
    7. <groupId>javax.xml.bindgroupId>
    8. <artifactId>jaxb-apiartifactId>
    9. dependency>
    10. <dependency>
    11. <groupId>com.sun.xml.bindgroupId>
    12. <artifactId>jaxb-implartifactId>
    13. <version>2.2.11version>
    14. dependency>
    15. <dependency>
    16. <groupId>org.glassfish.jaxbgroupId>
    17. <artifactId>jaxb-runtimeartifactId>
    18. <version>2.2.10-b140310.1920version>
    19. dependency>
    20. <dependency>
    21. <groupId>javax.activationgroupId>
    22. <artifactId>activationartifactId>
    23. <version>1.1.1version>
    24. dependency>

    (3)application.yml

    1. # Eureka server 端口号
    2. server:
    3. port: 8761
    4. spring:
    5. application:
    6. name: lagou-cloud-eureka-server #应用名称,应用名称会在Eureka中作为服务名称
    7. # eureka 客户端配置(和server交互),Eureka Server 其实也是一个Client
    8. eureka:
    9. instance:
    10. hostname: localhost # 当前Eureka实例的主机名
    11. client:
    12. service-url: # 配置客户端所交互的Eureka Server的地址
    13. defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka
    14. register-with-eureka: false # 当前自己就是Server,不需要注册自己
    15. fetch-registry: false # 获取注册中心的服务信息,自己就是Server,不需要从Eureka Server获取服务信息,默认为true

    (4)SpringBoot启动类,使用@EnableEurekaServer声明当前项目为EurekaServer服务

    1. package com.lagou.edu;
    2. import org.springframework.boot.SpringApplication;
    3. import org.springframework.boot.autoconfigure.SpringBootApplication;
    4. import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
    5. @SpringBootApplication
    6. // 声明当前项目为Eureka服务
    7. @EnableEurekaServer
    8. public class LagouEurekaServerApp8761 {
    9. public static void main(String[] args) {
    10. SpringApplication.run(LagouEurekaServerApp8761.class, args);
    11. }
    12. }
    • 执行启动类LagouCloudEurekaServerApplication的main函数
    • 访问http://127.0.0.1:8761,如果看到如下页面(Eureka注册中心后台),则表明EurekaServer发布成功

    2、搭建Eureka Server HA高可用集群 

       在互联网应用中,服务实例很少有单个的。 

            即使微服务消费者会缓存服务列表,但是如果EurekaServer只有一个实例,该实例挂掉,正好微服务消费者本地缓存列表中的服务实例也不可用,那么这个时候整个系统都受影响。

            在生产环境中,我们会配置Eureka Server集群实现高可用。Eureka Server集群之中的节点通过点对点(P2P)通信的方式共享服务注册表。我们开启两台 EurekaServer 以搭建集群。

    (1) 修改本机host属性

            由于是在个人计算机中进行测试很难模拟多主机的情况,Eureka配置server集群时需要执行host地址。 所以需要修改个人电脑中host地址

    127.0.0.1 LagouCloudEurekaServerA
    127.0.0.1 LagouCloudEurekaServerB

    (2)建立Eureka Server两个模块

    8761模块配置文件 

    1. # Eureka server 端口号
    2. server:
    3. port: 8761
    4. spring:
    5. application:
    6. name: lagou-cloud-eureka-server #应用名称,应用名称会在Eureka中作为服务名称
    7. # eureka 客户端配置(和server交互),Eureka Server 其实也是一个Client
    8. eureka:
    9. instance:
    10. hostname: LagouCloudEurekaServerA # 当前Eureka实例的主机名
    11. client:
    12. service-url:
    13. # 配置客户端所交互的Eureka Server的地址 (Eureka Server集群中每一个Server其实相对于其他Server来说都是Client)
    14. # 集群模式下,defaultZone应该指向其他Eureka Server,如果有更多其他Server实例,逗号拼接即可
    15. defaultZone: http://LagouCloudEurekaServerB:8762/eureka
    16. register-with-eureka: true # 集群模式下改为true
    17. fetch-registry: true # 获取注册中心的服务信息,默认为true

    8762配置文件

    1. # Eureka server 端口号
    2. server:
    3. port: 8762
    4. spring:
    5. application:
    6. name: lagou-cloud-eureka-server #应用名称,应用名称会在Eureka中作为服务名称
    7. # eureka 客户端配置(和server交互),Eureka Server 其实也是一个Client
    8. eureka:
    9. instance:
    10. hostname: localhost # 当前Eureka实例的主机名
    11. client:
    12. service-url: # 配置客户端所交互的Eureka Server的地址
    13. defaultZone: http://LagouCloudEurekaServerA:8761/eureka
    14. register-with-eureka: true
    15. fetch-registry: true # 获取注册中心的服务信息,默认为true

    (3)访问两个EurekaServer的管理台页面

            http://lagoucloudeurekaservera:8761/和http://lagoucloudeurekaserverb:8762/会发现注册中心LAGOU-CLOUDEUREKA-SERVER 已经有两个节点,并且 registered-replicas (相邻集群复制节点)中已经包含对方

    3、微服务提供者—>注册到Eureka Server集群

    注册简历微服务

    (1)父工程中引入spring-cloud-commons依赖

    1. <dependency>
    2. <groupId>org.springframework.cloudgroupId>
    3. <artifactId>spring-cloud-commonsartifactId>
    4. dependency>

    (2)pom文件引入坐标,添加eureka client的相关坐标

    1. "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>lagou-parentartifactId>
    7. <groupId>com.lagougroupId>
    8. <version>1.0-SNAPSHOTversion>
    9. parent>
    10. <modelVersion>4.0.0modelVersion>
    11. <artifactId>lagou-service-resumeartifactId>
    12. <dependencies>
    13. <dependency>
    14. <groupId>com.lagougroupId>
    15. <artifactId>lagou-service-commonartifactId>
    16. <version>1.0-SNAPSHOTversion>
    17. dependency>
    18. <dependency>
    19. <groupId>org.springframework.cloudgroupId>
    20. <artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
    21. dependency>
    22. dependencies>
    23. <properties>
    24. <maven.compiler.source>11maven.compiler.source>
    25. <maven.compiler.target>11maven.compiler.target>
    26. properties>
    27. project>

    (3)配置application.yml文件

        在application.yml 中添加Eureka Server高可用集群的地址及相关配置

    1. server:
    2. port: 8080
    3. spring:
    4. application:
    5. name: lagou-service-resume
    6. datasource:
    7. driver-class-name: com.mysql.jdbc.Driver
    8. url: jdbc:mysql://8.142.8.105:3306/lagou?useUnicode=true&characterEncoding=utf8&useSSL=false
    9. username: root
    10. password: g5201314yj
    11. jpa:
    12. database: MySQL
    13. show-sql: true
    14. hibernate:
    15. naming:
    16. physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl #避免将驼峰命名转换为下划线命名
    17. # 注册到Eureka服务中心
    18. eureka:
    19. client:
    20. service-url: # eureka server的路径
    21. # 注册到集群,就把多个EurekaServer地址使用逗号连接起来即可;注册到单实例(非集群模式),那就写一个
    22. defaultZone: http://LagouCloudEurekaServerB:8762/eureka,http://LagouCloudEurekaServerA:8761/eureka
    23. instance:
    24. prefer-ip-address: true # 服务实例中显示ip,而不是显示主机名(兼容老的Eureka版本)
    25. # 自定义实例显示格式,加上版本号,便于多版本管理,注意是ip-address,早期版本是ipAddress
    26. instance-id: ${spring.cloud.client.ipaddress}:${spring.application.name}:${server.port}:@project.version@

    经验:自定义实例显示格式,加上版本号,便于多版本管理

    (4)启动类添加注解

    注意: 

    • 从Spring Cloud Edgware版本开始,@EnableDiscoveryClient 或@EnableEurekaClient 可省略。只需加 上相关依赖,并进行相应配置,即可将微服务注册到服务发现组件上。
    • @EnableDiscoveryClient和@EnableEurekaClient⼆者的功能是一样的。但是如果选用的是eureka服务器,那么就推荐@EnableEurekaClient,如果是其他的注册中心,那么推荐使用@EnableDiscoveryClient,考虑到通用性,后期我们可以使用@EnableDiscoveryClient

    (5)启动类执行,在Eureka Server后台界面可以看到注册的服务实例

    说明:其他微服务注册可参照执行

    instance-id添加上版本号的效果 

    4、微服务消费者—>注册到Eureka Server集群 

    此处自动投递微服务是消费者

    (1)pom文件引入坐标,添加eureka client的相关坐标

    1. <dependency>
    2. <groupId>org.springframework.cloudgroupId>
    3. <artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
    4. dependency>

    (2)配置application.yml文件

    1. server:
    2. port: 8090
    3. # 注册到Eureka服务中心
    4. eureka:
    5. client:
    6. service-url:
    7. # 注册到集群,就把多个EurekaServer地址使用逗号连接起来即可;注册到单实例(非集群模式),那就写一个
    8. defaultZone: http://LagouCloudEurekaServerB:8762/eureka,http://LagouCloudEurekaServerA:8761/eureka
    9. instance:
    10. prefer-ip-address: true # 服务实例中显示ip,而不是显示主机名(兼容老的Eureka版本)
    11. # 自定义实例显示格式,加上版本号,便于多版本管理,注意是ip-address,早期版本是ipAddress
    12. instance-id: ${spring.cloud.client.ipaddress}:${spring.application.name}:${server.port}:@project.version@
    13. spring:
    14. application:
    15. name: lagou-service-autodeliver

    (3)在启动类添加注解@EnableDiscoveryClient,开启服务发现

    1. package com.lagou.edu;
    2. import org.springframework.boot.SpringApplication;
    3. import org.springframework.boot.autoconfigure.SpringBootApplication;
    4. import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
    5. import org.springframework.context.annotation.Bean;
    6. import org.springframework.web.client.RestTemplate;
    7. @SpringBootApplication
    8. @EnableDiscoveryClient
    9. public class AutoDeliverApplication {
    10. public static void main(String[] args) {
    11. SpringApplication.run(AutoDeliverApplication.class, args);
    12. }
    13. // 使用RestTemplate模板对象远程调用
    14. @Bean
    15. public RestTemplate gerRestTemplate() {
    16. return new RestTemplate();
    17. }
    18. }

    (4)controller改造

    1. package com.lagou.edu.controller;
    2. import org.springframework.beans.factory.annotation.Autowired;
    3. import org.springframework.cloud.client.ServiceInstance;
    4. import org.springframework.cloud.client.discovery.DiscoveryClient;
    5. import org.springframework.web.bind.annotation.GetMapping;
    6. import org.springframework.web.bind.annotation.PathVariable;
    7. import org.springframework.web.bind.annotation.RequestMapping;
    8. import org.springframework.web.bind.annotation.RestController;
    9. import org.springframework.web.client.RestTemplate;
    10. import java.util.List;
    11. @RestController
    12. @RequestMapping("/autodeliver")
    13. public class AutoDeliverController {
    14. @Autowired
    15. private RestTemplate restTemplate;
    16. // /autodeliver/checkState/
    17. /* @GetMapping("/checkState/{userId}")
    18. public Integer findResumeOpenState(@PathVariable Long userId) {
    19. // 调用远程服务->简历微服务接口 RestTemplate
    20. Integer forObject = restTemplate.getForObject("http://localhost:8080/resume/openstate/" + userId, Integer.class);
    21. return forObject;
    22. } */
    23. @Autowired
    24. private DiscoveryClient discoveryClient;
    25. /**
    26. * 改造:服务注册到Eureka之后的改造
    27. *
    28. * @param userId
    29. * @return
    30. */
    31. @GetMapping("/checkState/{userId}")
    32. public Integer findResumeOpenState(@PathVariable Long userId) {
    33. // 从Eureka Server中获取我们关注的那个服务的实例信息以及接口信息
    34. // 1、从Eureka Server中获取lagou-service-resume服务的实例信息(使用客户端对象做这件事情)
    35. List instances = discoveryClient.getInstances("lagou-service-resume");
    36. // 2、如果有多个实例,选择第一个使用(负载均衡的过程)
    37. ServiceInstance serviceInstance = instances.get(0);
    38. // 3、从元数据信息中获取host、port
    39. String host = serviceInstance.getHost();
    40. int port = serviceInstance.getPort();
    41. String url = "http://" + host + ":" + port + "/resume/openstate/" + userId;
    42. System.out.println("===========>从Eureka Server集群获取服务私立拼接的url:" + url);
    43. // 调用远程服务->简历微服务接口 RestTemplate
    44. Integer forObject = restTemplate.getForObject(url, Integer.class);
    45. return forObject;
    46. }
    47. }

    (5)访问:http://localhost:8080/resume/openstate/1545132

      发现调用成功

    注:

       项目模块

    详细代码:

            链接:https://pan.baidu.com/s/15nAeIuJNTj1JXKW2CcVD_A?pwd=9tjz 
            提取码:9tjz 

  • 相关阅读:
    Java多线程开发系列之五:Springboot 中异步请求方法的使用
    JavaEE-操作系统
    ubuntu 扩容逻辑卷
    丁鹿学堂:前端开发基础知识之像素详解
    7年阿里测试岗,我眼中的阿里虽然不完美,但值得去学5年
    关于 useEffect(() => { handlePostRequest(); }, []);执行2次(已解决)
    删除重复元素 -- 双指针
    工程(十四)——ubuntu20.04 PL-VINS
    Python + Django4 搭建个人博客(十三):更新文章功能页面实现
    java继承
  • 原文地址:https://blog.csdn.net/weixin_52851967/article/details/126403796