扫码关注公众号,领取更多资源
文章目录
Spring Cloud Eureka
简介
Spring Cloud Eureka
是在Netflix Eureka
进行二次封装的一个组件,主要负责微服务框架中的服务治理。
服务治理是微服务框架中必不可少的一部分,前面讲过,如果不进行微服务治理,服务的访问者需要知道所有微服务的信息,导致编写逻辑复杂,容错低,性能差等一系列问题。所以在微服务治理的过程中需要一个注册中心,用来注册所有的服务提供方信息。
注册中心带来的好处就是服务的访问者不需要之后后方提供了多少服务,不需要知道哪些服务可用,哪些服务不可用。就好比我们去12306买车票,我们不需要知道具体有多少车,有多少航线,我们只需要知道是否有我们需要的那班列车的车票即可。
注册中心常用的还有Dubbo
框架,Dubbo
框架使用的Zookeeper
作为注册中心的,而Spring Cloud Eureka
选择的是Eureka
,两者有什么区别呢?
在分布式系统中有一个CAP
定理。C
为数据一致性,A
为服务可用性,P
为服务对网络分区故障的容错性。但是这三个特性在任何分布式系统中都不能同时满足,最多只能满足其中的两个。
在Zookeeper
中类似一个主从的架构,其中有一个leader
,当这个leader
无法使用之后就会通过Paxos
算法选举新的leader
,保证数据写入只写入leader
,读取可以从非leader
节点中读取,从而保证了数据的一致性。
点击链接了解更多关于Paxos算法
总之,如果想要保证AP
就用Eureka
,想要保证CP
就用Zookeeper
。
搭建Eureka服务注册中心
首先创建一个Maven
项目,取名eureka-server
,在pom.xml
中添加如下配置依赖:
<!-- 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>
创建一个启动类,很简单,代码如下:
@EnableEurekaServer
@SpringBootApplication
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
如上面的代码,这个启动类跟SpringBoot
的一样,只是多了一个@EnableEurekaServer
,表示当前服务开启了Eureka Server
注册中心。
接下来就是添加相关配置信息,在resource
文件夹下创建一个application.yml
属性文件,新增如下配置:
# 服务名称
spring:
application:
name: eureka-server
# 端口号
server:
port: 8761
eureka:
client:
# 是否注册到注册中心,因为自身就是注册中心,所以设置为false
register-with-eureka: false
# 是否需要去检索服务,设置为false
fetch-registry: false
运行EurekaServerApplication.java
,就可以启动我们的注册中心服务了。启动成功后访问http://localhost:8761 就可以在浏览器中看到Eureka
提供的web
控制台信息。
搭建Eureka服务提供者
注册中心搭建好了,接下来需要搭建一个服务提供者来验证是否可用。
首先还是其创建一个Maven
项目,在pom.xml
中添加如下依赖:
<!-- Spring Boot -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.6.RELEASE</version>
<relativePath />
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- eureka -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</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>
创建一个启动类,代码如下:
@SpringBootApplication
@EnableDiscoveryClient
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
与注册中心类似,需要添加注解@EnableDiscoveryClient
,表示当前服务是一个Eureka
的客户端。
接下来,在application.yml
中添加相关配置:
spring:
application:
name: eureka-client-user-server
server:
port: 8081
eureka:
client:
serviceUrl:
# 注册到注册中心的地址
defaultZone: http://localhost:8761/eureka
instance:
# 是否采用IP注册
preferIpAddress: true
#定义实例ID格式
instance-id: ${spring.application.name}:${spring.cloud.client.ip-address}:${server.port}
启动main
方法,运行成功后访问Eureka
注册中心的Web
界面,可以看到服务提供者已经被注册上去,并且名字是按照配置文件中定义的格式显示的。
服务消费者
直接调用接口
创建服务消费者服务,消费者也需要注册到Eureka
上,配置与服务提供者一样,就不重复展示代码了,在服务中添加配置类,代码如下:
@Configuration
public class BeanConfiguration {
@Bean
public RestTemplate getRestTemplate() {
return new RestTemplate();
}
}
RestTemplate
是 Spring
提供的用于访问 Rest
服务的客户端,RestTemplate
提供了多种便捷访问远程 Http
服务的方法,可以很方便的调用远程服务接口。随后调用的方式如下:
@RestController
public class ArticleController {
@Autowired
private RestTemplate restTemplate;
@GetMapping("/article /callHello")
public String callHello() {
return restTemplate.getForObject("http://localhost:8081/user/hello", String.class);
}
}
通过Eureka方式调用
上面的调用方式需要通过IP+端口号
的方式实现,如果有多个服务节点的集群部署,这种方式就显得很不好,通过Eureka调用,可以直接根据服务名称自动找到可用节点,并远程调用,只需要在配置类上添加@LoadBalanced
即可,这个注解会自动构造 LoadBalancerClient
接口的实现类并注册到 Spring
容器中,代码如下所示:
@Configuration
public class BeanConfiguration {
@Bean
@LoadBalanced
public RestTemplate getRestTemplate() {
return new RestTemplate();
}
}
接下来就是调用接口中间原来的IP+端口号换为服务名称:
@GetMapping("/article/callHello2")
public String callHello2() {
return restTemplate.getForObject("http://eureka-client-user-service/user/hello", String.class);
}
Eureka注册中心开启密码认真
Eureka
启动后自带一个Web
管理页面,方便我们查询注册上去的服务,但是,如果在公网环境中,如果能直接访问,这样很不安全,所以需要对Eureka
加上权限验证来保证安全性。
改造eureka-server
,在pom.xml
中添加Spring-Security
依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
在application.yml
中添加认证配置信息:
spring:
security:
user:
name: username
password: password
增加Security
配置类:
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
// 关闭csrf
http.csrf().disable();
// 支持httpBasic
http.authorizeRequests().anyRequest().authenticated().and().httpBasic();
}
}
重启注册中心,访问http://localhost:8761/
,此时浏览器会提示输入用户名和密码,只有输入正确之后才能继续访问管理页面。
在Eureka
开启认证之后,需要注册上去的客户端同样需要加上认证的用户名和密码才能正常注册:
eureka:
client:
serviceUrl:
defaultZone: http://username:password@localhost:8761/eureka/
SpringCloud实现Eureka高可用
正常生产环境中,只部署一个Eureka
肯定是不够用的,需要搭建多个Eureka
的集群,保证注册中心在宕机之后有后续的服务能够接替,实现高可用。
在实现高可用的时候,我们只需要吧当前注册中心当成一个服务提供者,向其他的注册中心注册即可。
首先,根据前面所讲,创建一个新的Eureka
注册中心服务eureka-server-cluster
,配置个eureka-server
一样。
首先我们需要在eureka-server
的配置文件中新增:
# 指向你的从节点的Eureka
eureka:
client:
serviceUrl:
defaultZone: http://username:password@localhost:8762/eureka/
# 指定不同的环境
spring:
profiles:
active: master
在eureka-server-cluster
中新增配置:
# 指向你的主节点的Eureka
eureka:
client:
serviceUrl:
defaultZone: http://username:password@localhost:8761/eureka/
# 指定不同的环境
spring:
profiles:
active: cluster
如果有多个,只需要在eureka.client.serviceUrl.defaultZone
中配置多个注册地址,使用逗号隔开:
eureka:
client:
serviceUrl:
defaultZone: http://username:password@localhost:8761/eureka/,http://username:password@localhost:8762/eureka/
Eureka自我保护模式和InstanceID配置
保护模式主要在一组客户端和 Eureka Server
之间存在网络分区场景时使用。一旦进入保护模式,Eureka Server
将会尝试保护其服务的注册表中的信息,不再删除服务注册表中的数据。当网络故障恢复后,该Eureka Server
节点会自动退出保护模式。下图就表名EurekaServer
进入了保护模式。
可以通过下面的配置将自我保护模式关闭,这个配置是在 eureka-server
中:
eureka:
server:
enableSelfPreservation: false
客户端在注册时,服务的 Instance ID
的默认值的格式如下:
${spring.cloud.client.hostname}:${spring.application.name}:${spring.application. instance_id:${server.port}}
很多时候我们想把 IP
显示在上述格式中,此时,只要把主机名替换成 IP
就可以了,或者调整顺序也可以。可以改成下面的样子,用“服务名称:服务所在 IP:服务端口
”的格式来定义:
eureka:
instance:
instance-id: ${spring.application.name}:${spring.cloud.client.ip-address}:${server.port}
我们还可以点击服务的 Instance ID
进行跳转,这个时候显示的名称虽然变成了 IP
,但是跳转的链接却还是主机名。所以还需要加一个配置才能让跳转的链接变成我们想要的样子,使用 IP
进行注册:
eureka:
instance:
preferIpAddress: true
刚刚我们通过配置实现了用 IP
进行注册,当点击 Instance ID
进行跳转的时候,就可以用 IP
跳转了,跳转的地址默认是 IP+Port/info
。我们可以自定义这个跳转的地址:
eureka:
instance:
status-page-url: c.uTing.net
Eureka开发时快速移除失效服务
在实际开发过程中,我们可能会不停地重启服务,由于 Eureka
有自己的保护机制,故节点下线后,服务信息还会一直存在于 Eureka
中。我们可以通过增加一些配置让移除的速度更快一点,当然只在开发环境下使用,生产环境下不推荐使用。
首先在我们的 eureka-server
中增加两个配置,分别是关闭自我保护和清理间隔:
eureka:
server:
enable-self-preservation: false
# 默认 60000 毫秒
eviction-interval-timer-in-ms: 5000
然后在具体的客户端服务中配置下面的内容:
eureka:
client:
healthcheck:
enabled: true
# 默认 30 秒
instance:
lease-renewal-interval-in-seconds: 5
# 默认 90 秒
lease-expiration-duration-in-seconds: 5
eureka.client.healthcheck.enabled
用于开启健康检查,需要在 pom.xml
中引入 actuator
的依赖,代码如下所示:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
其中:
- eureka.instance.lease-renewal-interval-in-seconds 表示 Eureka Client 发送心跳给 server 端的频率。
- eureka.instance.lease-expiration-duration-in-seconds 表示 Eureka Server 至上一次收到 client 的心跳之后,等待下一次心跳的超时时间,在这个时间内若没收到下一次心跳,则移除该 Instance。