修订日期 | 内容 |
---|---|
2021-2-17 | 初稿 |
2-Spring Cloud微服务快速搭建-eureka注册中心
注册中心简述
Eureka是Netflix开发的服务发现框架,本身是一个基于REST的服务。目前大多使用Eureka作为Spring Cloud的注册中心,另外有一个部分可能使用alibaba nacos,本章先介绍Eureka。
注册中心作用:
- 负载均衡
- 中间层服务故障转移
搭建Eureka服务(高可用)
- 在第一章创建好的项目中创建模块:eureka_server
- 添加maven依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
- 创建集群配置文件
创建两个配置文件,使用eureka集群
分别创建两个配置文件
application-8761.yml
server:
port: 8761
spring:
application:
name: eureka-server # 服务名,注意千万不能使用下划线,可以使用中划线
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8762/eureka/ # 客户端注册服务端的地址8762与8761相互注册
#register-with-eureka: false # 是否将自己注册到eureka注册中心
#fetch-registry: false # 是否从ureka中获取注册信息
instance:
ip-address: true # 使用ip地址注册
instance-id: ${
spring.cloud.client.ip-address}:${
server.port} # 向注册中心注册服务id
application-8762.yml
server:
port: 8762
spring:
application:
name: eureka-server # 服务名,注意千万不能使用下划线,可以使用中划线
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/ # 客户端注册服务端的地址
#register-with-eureka: false # 是否将自己注册到eureka注册中心
#fetch-registry: false # 是否从ureka中获取注册信息
instance:
ip-address: true # 使用ip地址注册
instance-id: ${
spring.cloud.client.ip-address}:${
server.port} # 向注册中心注册服务id
4.创建启动类
@SpringBootApplication
// 激活eureka服务
@EnableEurekaServer
public class EurekaServerApplication {
public static void main(String[] args) {
//ip地址
new SpringApplicationBuilder(EurekaServerApplication.class).run(args);
}
}
5.启动服务,分别启动两次
分别创建两个启动类,配置profiles以不同的端口命名即可,然后依次运行两个服务
验证:
启动成功后使用 localhost:8761,localhost:8762查看是否能成功访问,查看注册的服务中是否包含启动的服务
将服务注册到eureka
- 创建一个
order_service
模块 - 引入eureka客户端maven依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
- 配置文件
spring:
application:
name: order-service
server:
port: 8081
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/,http://localhost:8762/eureka/
instance:
ip-address: true # 使用ip地址注册
instance-id: ${
spring.cloud.client.ip-address}:${
server.port} # 向注册中心注册服务ip
- 创建启动类
@SpringBootApplication
// 激活客户端 ,新版本可以省略
//@EnableEurekaClient,@EnableDiscoveryClient
public class OrderServiceApplication {
public static void main(String[] args) {
new SpringApplicationBuilder(OrderServiceApplication.class).run(args);
}
}
- 启动服务,查看是否注册成功
关闭Eureka自我保护机制
eureka:
server:
enable-self-preservation: false # 关闭自我保护机制(开发测试阶段设置)
eviction-interval-timer-in-ms: 5000 # 剔除服务间隔毫秒数(开发测试阶段设置)
使用代码测试eureka服务
import org.junit.Test;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.test.context.junit4.SpringRunner;
import javax.annotation.Resource;
import java.util.List;
@RunWith(SpringRunner.class)
@SpringBootTest
public class ExpressControllerTest {
@Resource
private DiscoveryClient discoveryClient;
@Test
public void testDiscovery() {
List<ServiceInstance> instances = discoveryClient.getInstances("order-service");
for (ServiceInstance instance : instances) {
System.out.println(instance.getHost()+":"+instance.getPort());
}
}
}