0
点赞
收藏
分享

微信扫一扫

第十篇: 高可用的服务注册中心(Finchley版本)V2.0_dev


一、准备工作

Eureka通过运行多个实例,使其更具有高可用性。事实上,这是它默认的熟性,你需要做的就是给对等的实例一个合法的关联serviceurl。

二、创建eureka-server

引入依赖

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>

添加配置信息

eureka:
client:
registerWithEureka: false
fetchRegistry: false
serviceUrl:
defaultZone: http://localhost:8762/eureka/,http://localhost:8763/eureka/
spring:
application:
name: eurka-server

运行主类

@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {

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

}

分别启动3个eureka-server实例,
第一步:启动以第1个eureka-server实例:
-Dserver.port=8761
配置信息修改如下:

eureka:
client:
registerWithEureka: false
fetchRegistry: false
serviceUrl:
defaultZone: http://localhost:8762/eureka/,http://localhost:8763/eureka/
spring:
application:
name: eurka-server

第一步:启动以第1个eureka-server实例:
-Dserver.port=8762
配置信息修改如下:

eureka:
client:
registerWithEureka: false
fetchRegistry: false
serviceUrl:
defaultZone: http://localhost:8761/eureka/,http://localhost:8763/eureka/
spring:
application:
name: eurka-server

第三步:启动以第1个eureka-server实例:
-Dserver.port=8763
配置信息修改如下:

eureka:
client:
registerWithEureka: false
fetchRegistry: false
serviceUrl:
defaultZone: http://localhost:8762/eureka/,http://localhost:8761/eureka/
spring:
application:
name: eurka-server

依次访问:localhost:8761、localhost:8761、localhost:8761

可以看到运行了3个eureka-serser实例

创建eureka-client工程

 <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

启动主类

@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
public class ServiceHiApplication {

public static void main(String[] args) {
SpringApplication.run(ServiceHiApplication.class, args);
}
}

配置文件

server:
port: 8765

spring:
application:
name: service-hi

eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/,http://localhost:8762/eureka/,http://localhost:8763/eureka/

web访问

@RestController
public class HiController {

@Value("${server.port}")
private String port;

@GetMapping("/hi")
public String home(@RequestParam(value = "name", defaultValue = "gblfy") String name) {
return "hi," + name + ",i am from port" + port;
}
}

启动eureka-client,

再依次访问:localhost:8761、localhost:8761、localhost:8761

发现eureka-client已经成功注册到3个eureka-server服务端了,本次策略采取的是,eureka-server两两注册,就算其中一个宕机了,也不会影响服务的发现和注册。

当然eureka-client建议采用集群策略,以达到高可用


举报

相关推荐

0 条评论