栈长这里是生成了一个 Maven 示例项目。

时间:2023-03-09 19:29:45
栈长这里是生成了一个 Maven 示例项目。

  Spring Cloud 的注册中心可以由 Eureka、Consul、Zookeeper、ETCD 等来实现,这里推荐使用 Spring Cloud Eureka 来实现注册中心,它基于 Netflix 的 Eureka 做了二次封装,完成分布式服务中服务治理的功能,微服务系统中的服务注册与发现都通过这个注册中心来进行管理。
  
  jin就来分享一个 Eureka 注册中心玩法,从 0 到分布式集群一步到位,单机版的咱就不玩了,没意义。
  
  本文基于最新的 Spring Cloud Greenwich.SR1 以及 Spring Boot 2.1.3 版本进行分享。
  
  快速构建一个 Eureka Server 项目
  
  打开 Spring 的快速构建网址,如下图所示,选择对应的参数,最后选择 Eureka Server 依赖,生成项目示例代码即可。
  
  https://start.spring.io/
  
  栈长这里是生成了一个 Maven 示例项目。
  
  <?xml version="1.0" encoding="UTF-8"?>

  <modelVersion>4.0.0</modelVersion>
  
  <parent>
  
  <groupId>org.springframework.boot</groupId>
  
  <artifactId>spring-boot-starter-parent</artifactId>
  
  <version>2.1.3.RELEASE</version>
  
  <relativePath/> <!-- lookup parent from repository -->
  
  </parent>
  
  <groupId>cn.javastack</groupId>
  
  <artifactId>spring-cloud--eureka-server</artifactId>
  
  <version>0.0.1-SNAPSHOT</version>
  
  <name>spring-cloud--eureka-server</name>
  
  <description>Demo project for Spring Cloud Eureka Server</description>
  
  <properties>
  
  <java.version>1.8</java.version>
  
  <spring-cloud.version>Greenwich.SR1</spring-cloud.version>
  
  </properties>
  
  <dependencies>
  
  <dependency>
  
  <groupId>org.springframework.cloud</groupId>
  
  <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
  
  </dependency>
  
  <dependency>
  
  <groupId>org.springframework.boot</groupId>
  
  <artifactId>spring-boot-starter-test</artifactId>
  
  <scope>test</scope>
  
  </dependency>
  
  </dependencies>
  
  <dependencyManagement>
  
  <dependencies>
  
  <dependency>
  
  <groupId>org.springframework.cloud</groupId>
  
  <artifactId>spring-cloud-dependencies</artifactId>
  
  <version>${spring-cloud.version}<www.tiaotiaoylzc.com /version>
  
  <type>pom</type>
  
  <scope>import</scope>
  
  </dependency>
  
  </dependencies>
  
  </dependencyManagement>
  
  <build>
  
  <plugins>
  
  <plugin>
  
  <groupId>org.springframework.boot</groupId>
  
  <artifactId>spring-boot-maven-plugin</artifactId>
  
  </plugin>
  
  </plugins>
  
  </build>
  
  </project>
  
  主要是加入了 Eureka Server 和 Spring Test 依赖包,还有 Spring Boot 和 Spring Cloud 的基础依赖。
  
  Maven就不多介绍了,不熟悉的,请关注Java技术栈微信公众号,在后台回复:Maven,即可获取栈长整理的一系列 Maven 系列教程文章。
  
  开启 Eureka Server 功能
  
  @EnableEurekaServer
  
  @SpringBootApplication
  
  public class EurekaServerApplication {
  
  public static void main(String[www.huarenyl.cn] args) {
  
  SpringApplication.run(EurekaServerApplication.class, args);
  
  }
  
  }
  
  在启动类上加入 @EnableEurekaServer 注解,@EnableEurekaServer注解即开启注册中心服务器的功能。
  
  Spring Boot就不多介绍了,不熟悉的,请关注Java技术栈微信公众号,在后台回复:Boot,即可获取栈长整理的一系列 Spring Boot 系列教程文章。
  
  添加 Eureka Server 配置
  
  在 application.yml 中加入如下配置:
  
  spring:
  
  application:
  
  name: register-center
  
  eureka:
  
  instance:
  
  prefer-ip-address: false
  
  instance-id: ${spring.cloud.client.www.honglanggjpt.cn ip-address}:${server.port}
  
  lease-expiration-duration-in-www.taoyyunsheng.com seconds: 30
  
  lease-renewal-interval-in-seconds: 5
  
  server:
  
  enable-self-preservation: true
  
  eviction-interval-timer-in-ms: 5000
  
  client:
  
  register-with-eureka: true
  
  fetch-registry: true
  
  serviceUrl:
  
  defaultZone: http://www.fengshen157.com eureka1:8761/eureka/, http://eureka2:8762/eureka/
  
  logging.level.com.netflix:
  
  eureka: OFF
  
  discovery: OFF
  
  ---
  
  spring:
  
  profiles: rc1
  
  server:
  
  port: 8761
  
  eureka.instance.hostname: eureka1
  
  ---
  
  spring:
  
  profiles: rc2
  
  server:
  
  port: 8762
  
  eureka.instance.hostname: eureka2
  
  这里做了两台注册中心的高可用配置rc1,rc2,也可以做多台,既然是高可用,每个注册中心都向别的注册中心注册自己。
  
  注意不要用Localhost
  
  如上图所示,如果大家在实战中遇到集群不可用,出现在 unavailable-replicas 里面时,说明是你配置的问题。
  
  如果 defaultZone 用了 localhost,prefer-ip-address 设置的是 false,则集群不行,不能用 localhost,要配置 hosts,并代替 localhost。
  
  127.0.0.1 localhost eureka1 eureka2
  
  启动 Eureka 注册中心
  
  这样两个注册心的 Eureka Server 就搭好了,启动的时候使用不同的 Profile 来指定不同的端口。
  
  spring-boot:run -Dspring-boot.run.profiles=rc1
  
  spring-boot:run -Dspring-www.sanxinyulevip.com boot.run.profiles=rc2
  
  按上方面命令启动两个 Eureka Server,然后再来验证一下注册情况,分别打开两个 Eureka Server 控制台页面。
  
  http://localhost:8761/http://localhost:8762/
  
  我们可以看到两个注册的注册中心实例了。
  
  欢迎工作一到五年的Java工程师朋友们加入Java程序员开发: 721575865
  
  群内提供免费的Java架构学习资料(里面有高可用、高并发、高性能及分布式、Jvm性能调优、Spring源码,MyBatis,Netty,Redis,Kafka,Mysql,Zookeeper,Tomcat,Docker,Dubbo,Nginx等多个知识点的架构资料)合理利用自己每一分每一秒的时间来学习提升自己,不要再用"没有时间“来掩饰自己思想上的懒惰!趁年轻,使劲拼,给未来的自己一个交代!