• Spring cloud搭建Eureka高可用注册中心


    注册中心在微服务中是必不可少的一部分,主要用来实现服务自治的功能,本文则主要记载使用Netflix提供的Eureka作为注册中心,来实现服务自治的功能。

    实际上Eureka的集群搭建方法很简单:每一台Eureka只需要在配置中指定另外多个Eureke的地址,就可以实现一个集群的搭建了

    例如:

    两节点

      -- 节点1注册到节点2

      --节点2注册到节点1

    三节点

      --节点1注册到节点2,3

      --节点2注册到节点1,3

      --节点3注册到节点1,2

    我做的是两个Eureka节点构建注册中心,然后一个producer和一个customer分别进行提供服务和请求服务:见图

     

    节点1:

    先新建一个Maven项目,添加依赖(基于jdk11,如果不是jdk11,可以不添加)

    pom.xml主要代码

     1 <parent>
     2         <groupId>org.springframework.boot</groupId>
     3         <artifactId>spring-boot-starter-parent</artifactId>
     4         <version>1.5.4.RELEASE</version>
     5         <relativePath/>
     6     </parent>
     7 
     8     <properties>
     9         <java.version>11</java.version>
    10     </properties>
    11 
    12     <!-- 依赖 -->
    13     <dependencies>
    14         <!-- Eureka -->
    15         <dependency>
    16             <groupId>org.springframework.cloud</groupId>
    17             <artifactId>spring-cloud-starter-eureka-server</artifactId>
    18         </dependency>
    19 
    20         <!--java 11 缺少的模块 javax.xml.bind-->
    21         <dependency>
    22             <groupId>javax.xml.bind</groupId>
    23             <artifactId>jaxb-api</artifactId>
    24             <version>2.3.0</version>
    25         </dependency>
    26         <dependency>
    27             <groupId>com.sun.xml.bind</groupId>
    28             <artifactId>jaxb-impl</artifactId>
    29             <version>2.3.0</version>
    30         </dependency>
    31         <dependency>
    32             <groupId>org.glassfish.jaxb</groupId>
    33             <artifactId>jaxb-runtime</artifactId>
    34             <version>2.3.0</version>
    35         </dependency>
    36         <dependency>
    37             <groupId>javax.activation</groupId>
    38             <artifactId>activation</artifactId>
    39             <version>1.1.1</version>
    40         </dependency>
    41 
    42         <!-- 添加 Spring-Security 不需要则不添加-->
    43         <dependency>
    44             <groupId>org.springframework.boot</groupId>
    45             <artifactId>spring-boot-starter-security</artifactId>
    46         </dependency>
    47     </dependencies>
    48 
    49     <!-- Spring Cloud -->
    50     <dependencyManagement>
    51         <dependencies>
    52             <dependency>
    53                 <groupId>org.springframework.cloud</groupId>
    54                 <artifactId>spring-cloud-dependencies</artifactId>
    55                 <version>Dalston.SR4</version>
    56                 <type>pom</type>
    57                 <scope>import</scope>
    58             </dependency>
    59         </dependencies>
    60     </dependencyManagement>

    application.yml

    ---
    server:
      ###启动端口
      port: 8761
    spring:
      ###启动的配置文件名
      profiles: eureka1
      ###应用名
      application:
        name: master
    eureka:
      instance:
        hostname: eureka1
      client:
        ###不向注册中心注册自己
        register-with-eureka: false
        ###不需要检索服务
        fetch-registry: false
        serviceUrl:
          defaultZone: http://127.0.0.1:8762/eureka/
      server:
        ###关闭自我保护模式
        enable-self-preservation: false
        ###清理的间隔为5s
        eviction-interval-timer-in-ms: 5000
    security:
      basic:
        ###暂时关闭安全认证
        enabled: false
    
    ---
    server:
      ###启动端口
      port: 8762
    spring:
      ###启动的配置文件名
      profiles: eureka2
      application:
        ###应用名
        name: slaveone
    eureka:
      instance:
        hostname: eureka2
      client:
        ###不向注册中心注册自己
        register-with-eureka: false
        ###不需要检索服务
        fetch-registry: false
        serviceUrl:
          defaultZone: http://127.0.0.1:8761/eureka/
      server:
        ###关闭自我保护模式
        enable-self-preservation: false
        ###清理的间隔为5s
        eviction-interval-timer-in-ms: 5000
    security:
      basic:
        ###暂时关闭安全认证
        enabled: false

    启动类:EurekaServerApplication.class

    1 @EnableEurekaServer
    2 @SpringBootApplication
    3 public class EurekaServerApplication {
    4 
    5     public static void main(String [] args){
    6         SpringApplication.run(EurekaServerApplication.class, args);
    7     }
    8 }

    启动参数 IDEA设置    --spring.profiles.active=eureka1

    点击这个:

    同理另外一个节点的启动类,application.yml和pom.xml文件与节点1相同

    启动参数改为--spring.profiles.active=eureka2

     

    此时,浏览器输入 http://127.0.0.1:8761/发现如下:

    至此注册中心已经搭建完成

    producer服务注册:

    主要的pom.xml文件

     1     <properties>
     2         <java.version>11</java.version>
     3     </properties>
     4 
     5     <parent>
     6         <groupId>org.springframework.boot</groupId>
     7         <artifactId>spring-boot-starter-parent</artifactId>
     8         <version>1.5.4.RELEASE</version>
     9         <relativePath/>
    10     </parent>
    11 
    12     <!-- 依赖 -->
    13     <dependencies>
    14         <!-- Eureka -->
    15         <dependency>
    16             <groupId>org.springframework.cloud</groupId>
    17             <artifactId>spring-cloud-starter-eureka</artifactId>
    18         </dependency>
    19         <!--java 11 缺少的模块 javax.xml.bind-->
    20         <dependency>
    21             <groupId>javax.xml.bind</groupId>
    22             <artifactId>jaxb-api</artifactId>
    23             <version>2.3.0</version>
    24         </dependency>
    25         <dependency>
    26             <groupId>com.sun.xml.bind</groupId>
    27             <artifactId>jaxb-impl</artifactId>
    28             <version>2.3.0</version>
    29         </dependency>
    30         <dependency>
    31             <groupId>org.glassfish.jaxb</groupId>
    32             <artifactId>jaxb-runtime</artifactId>
    33             <version>2.3.0</version>
    34         </dependency>
    35         <dependency>
    36             <groupId>javax.activation</groupId>
    37             <artifactId>activation</artifactId>
    38             <version>1.1.1</version>
    39         </dependency>
    40 
    41         <!-- Actuator 健康检查 -->
    42         <dependency>
    43             <groupId>org.springframework.boot</groupId>
    44             <artifactId>spring-boot-starter-actuator</artifactId>
    45         </dependency>
    46     </dependencies>
    47 
    48     <!-- Spring Cloud -->
    49     <dependencyManagement>
    50         <dependencies>
    51             <dependency>
    52                 <groupId>org.springframework.cloud</groupId>
    53                 <artifactId>spring-cloud-dependencies</artifactId>
    54                 <version>Dalston.SR4</version>
    55                 <type>pom</type>
    56                 <scope>import</scope>
    57             </dependency>
    58         </dependencies>
    59     </dependencyManagement>

    application.properties文件:

    spring.application.name=producer
    server.port=8081
    eureka.client.service-url.defaultZone=http://127.0.0.1:8761/eureka/,http://127.0.0.1:8762/eureka/
    
    ### 配置健康检查的信息
    eureka.client.healthcheck.enabled=true
    ### 默认30s
    eureka.instance.lease-renewal-interval-in-seconds=5
    ### 默认90秒
    eureka.instance.lease-expiration-duration-in-seconds=5
    1 @RestController
    2 @RequestMapping("/home")
    3 public class HomeController {
    4 
    5     @GetMapping("/hello")
    6     public String hello(){
    7         return "hello";
    8     }
    9 }

     基本的control类和启动类

    1 @SpringBootApplication
    2 @EnableDiscoveryClient
    3 public class ProduceServiceApplication {
    4     public static void main(String[] args){
    5         SpringApplication.run(ProduceServiceApplication.class, args);
    6     }
    7 }

    启动之后再看Eureka注册中心:两个节点应该都有

    访问   http:127.0.0.1:8081/home/hello  发现正常返回字符串

    此时

    搭建customer请求服务

    pom.xml 和application.properties文件

    <properties>
            <java.version>11</java.version>
        </properties>
    
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>1.5.4.RELEASE</version>
            <relativePath/>
        </parent>
    
        <!-- 依赖 -->
        <dependencies>
            <!-- Eureka -->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-eureka</artifactId>
            </dependency>
            <!--java 11 缺少的模块 javax.xml.bind-->
            <dependency>
                <groupId>javax.xml.bind</groupId>
                <artifactId>jaxb-api</artifactId>
                <version>2.3.0</version>
            </dependency>
            <dependency>
                <groupId>com.sun.xml.bind</groupId>
                <artifactId>jaxb-impl</artifactId>
                <version>2.3.0</version>
            </dependency>
            <dependency>
                <groupId>org.glassfish.jaxb</groupId>
                <artifactId>jaxb-runtime</artifactId>
                <version>2.3.0</version>
            </dependency>
            <dependency>
                <groupId>javax.activation</groupId>
                <artifactId>activation</artifactId>
                <version>1.1.1</version>
            </dependency>
    
            <!-- Actuator 健康检查 -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-actuator</artifactId>
            </dependency>
        </dependencies>
    
        <!-- Spring Cloud -->
        <dependencyManagement>
            <dependencies>
                <dependency>
                    <groupId>org.springframework.cloud</groupId>
                    <artifactId>spring-cloud-dependencies</artifactId>
                    <version>Dalston.SR4</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
            </dependencies>
        </dependencyManagement>
    spring.application.name=customer
    server.port=8082
    eureka.client.service-url.defaultZone=http://127.0.0.1:8761/eureka/,http://127.0.0.1:8762/eureka/
    
    ### 配置健康检查的信息
    eureka.client.healthcheck.enabled=true
    ### 默认30s
    eureka.instance.lease-renewal-interval-in-seconds=5
    ### 默认90秒
    eureka.instance.lease-expiration-duration-in-seconds=5

    获取RestTemplate的类:

    1 @Configuration
    2 public class BeanConfiguration {
    3 
    4     @Bean
    5     @LoadBalanced
    6     public RestTemplate getRestTemplate(){
    7         return new RestTemplate();
    8     }
    9 }

    control类

     1 @RestController
     2 @RequestMapping("/customer/")
     3 public class CustomerServiceController {
     4 
     5     @Autowired
     6     private RestTemplate restTemplate;
     7 
     8     @GetMapping("/callHello")
     9     public String callHello(){
    10         return restTemplate.getForObject("http://producer/home/hello", String.class);
    11     }
    12 }

    启动类:

    1 @SpringBootApplication
    2 @EnableDiscoveryClient
    3 public class CustomerMainApplication {
    4     public static void main(String[] args){
    5         SpringApplication.run(CustomerMainApplication.class, args);
    6     }
    7 }

    首先,正常情况下访问   http://localhost:8082/customer/callHello

     

    然后停掉节点1,继续访问看是否成功:

    请求正常

     

    重新启动节点1,查看节点2的日志:

    节点2在丢失节点1之后会不断重试

    直到找到节点1,高可用重新生成

    如果有什么错误或者纰漏,恳请指正,谢谢

  • 相关阅读:
    [LeetCode] Longest Common Prefix
    [LeetCode] Path Sum II
    [LeetCode] Path Sum
    [LintCode] 寻找缺失的数
    [LintCode] 最多有多少个点在一条直线上
    [LeetCode] Max Points on a Line
    [LeetCode] Binary Tree Right Side View
    [LeetCode] Populating Next Right Pointers in Each Node II
    [LeetCode] Populating Next Right Pointers in Each Node
    apache php 60 503
  • 原文地址:https://www.cnblogs.com/ITyunbook/p/11236177.html
Copyright © 2020-2023  润新知