搭建eureka服务端
新建一个springboot服务
- 引入依赖坐标
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
- 添加配置文件
server:
port: 9091
spring:
application:
name: eureka-server1
#eureka server 配置
eureka:
instance:
hostname: localhost
client:
register-with-eureka: false #是否将自己注册到注册中心
fetch-registry: false #是否从eureka获取注册信息
service-url: #配置暴露给eureka client的请求地址defaultZone
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
#defaultZone: http://127.0.0.1:9090/eureka/
server:
enable-self-preservation: false #关闭自我保护机制
eviction-interval-timer-in-ms: 4000 #剔除服务间隔
- 激活eureka服务
springboot启动类上添加注解
@EnableEurekaServer
搭建eureka客户端
新建一个springboot服务
- 引入坐标
<!--euaeka客户端-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
- 添加配置文件
# eureka 客户端
eureka:
client:
service-url:
defaultZone: http://localhost:9091/eureka/
instance:
instance-id: ${spring.application.name}:${server.port}:@project.version@
hostname: node1
prefer-ip-address: true #优先使用IP地址方式进行注册服务
- 激活eureka服务
可以不用激活,默认激活状态
@EnableDiscoveryClient
服务调用
/**
* eureka客户端
*/
@Autowired
private DiscoveryClient discoveryClient;
/**
* 基于eureka客户端调用远程服务
*
* @return
*/
@Override
public List<OrderEntity> getOrderList() {
List<OrderEntity> orderList = orderMapper.getOrderList();
List<Integer> idList = orderList.stream().map(o ->
o.getProductId()).distinct().collect(Collectors.toList());
//通过服务名称获取实例
List<ServiceInstance> instances = discoveryClient.getInstances("service-product");
ServiceInstance serviceInstance = instances.stream().findFirst().orElse(null);
log.info("host:{}-----------post:{}",serviceInstance.getHost(),serviceInstance.getPort());
String url = StrUtil.format("http://{}:{}/product/list?ids={}",
serviceInstance.getHost(),serviceInstance.getPort(),StrUtil.join(",",idList));
//调用远程服务
ResponseEntity<List> entity = restTemplate.getForEntity
(url, List.class);
if (Objects.nonNull(entity) && Objects.equals(entity.getStatusCode().value(), 200)) {
List<ProductEntity> body = entity.getBody();
log.info("实体对象:{}", StrUtil.toString(body));
}
return orderMapper.getOrderList();
}
eureka集群
新建两个eureka服务
端口9090,9091互相注册
1号server
server:
port: 9091
spring:
application:
name: eureka-server1
#eureka server 配置
eureka:
instance:
hostname: localhost
client:
#register-with-eureka: false #是否将自己注册到注册中心
#fetch-registry: false #是否从eureka获取注册信息
service-url: #配置暴露给eureka client的请求地址defaultZone
#defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
defaultZone: http://127.0.0.1:9090/eureka/
2号server
server:
port: 9090
spring:
application:
name: eureka-server2
#eureka server 配置
eureka:
instance:
hostname: localhost
client:
#register-with-eureka: false #是否将自己注册到注册中心
#fetch-registry: false #是否从eureka获取注册信息
service-url: #配置暴露给eureka client的请求地址defaultZone
#defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
defaultZone: http://127.0.0.1:9091/eureka/
将服务注册到eureka集群
# eureka 客户端
eureka:
client:
service-url:
defaultZone: http://localhost:9091/eureka/,http://localhost:9090/eureka/
instance:
instance-id: ${spring.cloud.client.ip-address}:${server.port}:@project.version@
hostname: node1
prefer-ip-address: true #优先使用IP地址方式进行注册服务
lease-renewal-interval-in-seconds: 5 #向eureka发送心跳的间隔
lease-expiration-duration-in-seconds: 10 #10秒后,如果没有回应,代表已经宕机