首页 技术 正文
技术 2022年11月9日
0 收藏 436 点赞 3,825 浏览 7259 个字

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

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

例如:

两节点

  – 节点1注册到节点2

  –节点2注册到节点1

三节点

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

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

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

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

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

节点1:

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

pom.xml主要代码

 <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.4.RELEASE</version>
<relativePath/>
</parent> <properties>
<java.version>11</java.version>
</properties> <!-- 依赖 -->
<dependencies>
<!-- Eureka -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</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> <!-- 添加 Spring-Security 不需要则不添加-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</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>

application.yml

---
server:
###启动端口
port:
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:
security:
basic:
###暂时关闭安全认证
enabled: false---
server:
###启动端口
port:
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:
security:
basic:
###暂时关闭安全认证
enabled: false

启动类:EurekaServerApplication.class

 @EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication { public static void main(String [] args){
SpringApplication.run(EurekaServerApplication.class, args);
}
}

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

点击这个:

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

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

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

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

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

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

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

producer服务注册:

主要的pom.xml文件

     <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>

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
 @RestController
@RequestMapping("/home")
public class HomeController { @GetMapping("/hello")
public String hello(){
return "hello";
}
}

基本的control类和启动类

 @SpringBootApplication
@EnableDiscoveryClient
public class ProduceServiceApplication {
public static void main(String[] args){
SpringApplication.run(ProduceServiceApplication.class, args);
}
}

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

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

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

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

此时

搭建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=
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=
### 默认90秒
eureka.instance.lease-expiration-duration-in-seconds=

获取RestTemplate的类:

 @Configuration
public class BeanConfiguration { @Bean
@LoadBalanced
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
}

control类

 @RestController
@RequestMapping("/customer/")
public class CustomerServiceController { @Autowired
private RestTemplate restTemplate; @GetMapping("/callHello")
public String callHello(){
return restTemplate.getForObject("http://producer/home/hello", String.class);
}
}

启动类:

 @SpringBootApplication
@EnableDiscoveryClient
public class CustomerMainApplication {
public static void main(String[] args){
SpringApplication.run(CustomerMainApplication.class, args);
}
}

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

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

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

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

请求正常

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

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

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

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

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

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,488
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,903
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,737
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,489
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,128
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,290