SpringCloud学习心得——Eureka注册中心

时间:2019-10-22
本文章向大家介绍SpringCloud学习心得——Eureka注册中心,主要包括SpringCloud学习心得——Eureka注册中心使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

SpringCloud学习心得——Eureka注册中心

定义

Spring Cloud Eureka 是 Spring Cloud Netflix 微服务套件的一部分,基于 REST 的服务,并且提供了基于 Java 的客户端组件,主要负责实现微服务架构中的服务治理功能。

与Zookeeper区别与联系

CAP 定理:C 为数据一致性;A 为服务可用性;P 为服务对网络分区故障的容错性。这三个特性在任何分布式系统中都不能同时满足,最多同时满足两个。
Eureka 是基于 AP 原则构建的,而 ZooKeeper 是基于 CP 原则构建的。
  • Zookeeper 有一个 Leader,而且在这个 Leader 无法使用的时候通过 Paxos(ZAB)算法选举出一个新的 Leader。这个 Leader 的任务就是保证写数据的时候只向这个 Leader 写入,Leader 会同步信息到其他节点。通过这个操作就可以保证数据的一致性。
  • 去 Eureka 中去拉取服务列表,查看你调用的服务在不在其中,在的话就拿到服务地址、端口等信息,然后调用。

搭建Eureka中心

  1. 创建maven项目,添加如下依赖

    1. <!-- Spring Boot -->
      <parent>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-parent</artifactId>
          <version>2.0.6.RELEASE</version>
          <relativePath />
      </parent>
      <dependencies>
          <!-- eureka -->
          <dependency>
              <groupId>org.springframework.cloud</groupId>
              <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
          </dependency>
      </dependencies>
      <!-- Spring Cloud -->
      <dependencyManagement>
          <dependencies>
              <dependency>
                  <groupId>org.springframework.cloud</groupId>
                  <artifactId>spring-cloud-dependencies</artifactId>
                  <version>Finchley.SR2</version>
                  <type>pom</type>
                  <scope>import</scope>
              </dependency>
          </dependencies>
      </dependencyManagement>
      26
       
      1
      <!-- Spring Boot -->
      2
      <parent>
      3
          <groupId>org.springframework.boot</groupId>
      4
          <artifactId>spring-boot-starter-parent</artifactId>
      5
          <version>2.0.6.RELEASE</version>
      6
          <relativePath />
      7
      </parent>
      8
      <dependencies>
      9
          <!-- eureka -->
      10
          <dependency>
      11
              <groupId>org.springframework.cloud</groupId>
      12
              <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
      13
          </dependency>
      14
      </dependencies>
      15
      <!-- Spring Cloud -->
      16
      <dependencyManagement>
      17
          <dependencies>
      18
              <dependency>
      19
                  <groupId>org.springframework.cloud</groupId>
      20
                  <artifactId>spring-cloud-dependencies</artifactId>
      21
                  <version>Finchley.SR2</version>
      22
                  <type>pom</type>
      23
                  <scope>import</scope>
      24
              </dependency>
      25
          </dependencies>
      26
      </dependencyManagement>
  2. 添加启动类

    1. @EnableEurekaServer
      @SpringBootApplication
          public static void main(String[] args) {
              SpringApplication.run(EurekaServer Application.class, args);
          }
      }
       
      1
      @EnableEurekaServer
      2
      @SpringBootApplication
      3
          public static void main(String[] args) {
      4
              SpringApplication.run(EurekaServer Application.class, args);
      5
          }
      6
      }
  3. 添加配置文件

    1. spring:
        application:
          name: myEureka-Server
      
      server:
        port: 8761
        
      eureka:
        client:
          # 因为自己是主机,所以不向自己注册
          register-with-eureka: false
          # 自己就是注册中心,所以不用去检索服务
          fetch-registry: false
       
      1
      spring:
      2
        application:
      3
          name: myEureka-Server
      4
      5
      server:
      6
        port: 8761
      7
        
      8
      eureka:
      9
        client:
      10
          # 因为自己是主机,所以不向自己注册
      11
          register-with-eureka: false
      12
          # 自己就是注册中心,所以不用去检索服务
      13
          fetch-registry: false
  4. 运行,并访问,http://localhost:8761



创建服务提供者

创建项目注册到Eureka

  1. 新建maven项目,引入依赖
    1. 31
       
      1
      <parent>
      2
          <groupId>org.springframework.boot</groupId>
      3
          <artifactId>spring-boot-starter-parent</artifactId>
      4
          <version>2.0.6.RELEASE</version>
      5
      </parent>
      6
      <dependencies>
      7
      8
          <dependency>
      9
              <groupId>org.springframework.boot</groupId>
      10
              <artifactId>spring-boot-starter-web</artifactId>
      11
          </dependency>
      12
      13
          <dependency>
      14
              <groupId>org.springframework.cloud</groupId>
      15
              <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
      16
          </dependency>
      17
      18
      </dependencies>
      19
      20
      <!-- Spring Cloud -->
      21
      <dependencyManagement>
      22
          <dependencies>
      23
              <dependency>
      24
                  <groupId>org.springframework.cloud</groupId>
      25
                  <artifactId>spring-cloud-dependencies</artifactId>
      26
                  <version>Finchley.SR2</version>
      27
                  <type>pom</type>
      28
                  <scope>import</scope>
      29
              </dependency>
      30
          </dependencies>
      31
      </dependencyManagement>


    2. 创建启动类,注意,此处在启动类上的注解是@EnableDiscoveryClient
      @EnableDiscoveryClient
      @SpringBootApplication
      public class Application {
          public static void main(String[] args) {
              SpringApplication.run(Application.class, args);
          }
      }
      7
       
      1
      @EnableDiscoveryClient
      2
      @SpringBootApplication
      3
      public class Application {
      4
          public static void main(String[] args) {
      5
              SpringApplication.run(Application.class, args);
      6
          }
      7
      }

    3. 添加测试接口
      @RestController
      @RequestMapping("/user")
      public class UserController {
      
          @GetMapping("/hello")
          public String sayHello(){
              return "hello";
          }
      }
      9
       
      1
      @RestController
      2
      @RequestMapping("/user")
      3
      public class UserController {
      4
      5
          @GetMapping("/hello")
      6
          public String sayHello(){
      7
              return "hello";
      8
          }
      9
      }
    4. 增加配置文件
      server:
        port: 8081
      
      spring:
        application:
          name: myEureka-Service
      
      eureka:
        client:
          #这次要注册到eureka上
          register-with-eureka: true
          serviceUrl:
            defaulZone: "http://localhost:8761/eureka/"
        instance:
          #采用ip注册的方式
          prefer-ip-address: true
          #id的格式
          instance-id: ${spring.application.name}:${spring.cloud.client.ip-address}:${server.port}
      18
       
      1
      server:
      2
        port: 8081
      3
      4
      spring:
      5
        application:
      6
          name: myEureka-Service
      7
      8
      eureka:
      9
        client:
      10
          #这次要注册到eureka上
      11
          register-with-eureka: true
      12
          serviceUrl:
      13
            defaulZone: "http://localhost:8761/eureka/"
      14
        instance:
      15
          #采用ip注册的方式
      16
          prefer-ip-address: true
      17
          #id的格式
      18
          instance-id: ${spring.application.name}:${spring.cloud.client.ip-address}:${server.port}

    5. 启动成功,去eureka看一下,发现了8762的服务

创建消费者

  1. 创建maven项目,eureka-consumer,添加相同依赖与启动类,但要更改配置文件
    spring.application.name=eureka-client-article-service
    server.port=8082
    2
     
    1
    spring.application.name=eureka-client-article-service
    2
    server.port=8082
  2. 使用RestTemplate获取Rest服务端调用接口
    @Configuration
    public class BeanConfiguration {
        @Bean
        public RestTemplate getRestTemplate() {
            return new RestTemplate();
        }
    }
    7
     
    1
    @Configuration
    2
    public class BeanConfiguration {
    3
        @Bean
    4
        public RestTemplate getRestTemplate() {
    5
            return new RestTemplate();
    6
        }
    7
    }

  3. 创建接口
    @RestController
    public class ArticleController {
        @Autowired
        private RestTemplate restTemplate;
        @GetMapping("/article /callHello")
        public String callHello() {
            return restTemplate.getForObject("http://localhost:8081/user/hello", String.class);
        }
    }
    9
     
    1
    @RestController
    2
    public class ArticleController {
    3
        @Autowired
    4
        private RestTemplate restTemplate;
    5
        @GetMapping("/article /callHello")
    6
        public String callHello() {
    7
            return restTemplate.getForObject("http://localhost:8081/user/hello", String.class);
    8
        }
    9
    }

  4. 调用

  5. 改造
    1. 我们的目的是通过服务调用,而不要关心端口
    2. 改造 RestTemplate 的配置,添加一个 @LoadBalanced 注解,这个注解会自动构造 LoadBalancerClient 接口的实现类并注册到 Spring 容器中
      @Configuration
      public class RestTemplateConfig {
      
          @Bean
          @LoadBalanced
          public RestTemplate getRestTemplate(){
              return new RestTemplate();
          }
      }
      9
       
      1
      @Configuration
      2
      public class RestTemplateConfig {
      3
      4
          @Bean
      5
          @LoadBalanced
      6
          public RestTemplate getRestTemplate(){
      7
              return new RestTemplate();
      8
          }
      9
      }

    3. 接下来就是改造调用代码,我们不再直接写固定地址,而是写成服务的名称,这个名称就是我们注册到 Eureka 中的名称,是属性文件中的 spring.application.name
      @GetMapping("/callHello")
      public String callHello() {
      	return restTemplate.getForObject("http://myEureka-Service/user/hello", String.class);
      }
      4
       
      1
      @GetMapping("/callHello")
      2
      public String callHello() {
      3
          return restTemplate.getForObject("http://myEureka-Service/user/hello", String.class);
      4
      }
      调用成功,舒服了





原文地址:https://www.cnblogs.com/pipicai96/p/11719136.html