Eureka实现动态扩容
文章目录1.概述2.Springcloud Config实现动态扩容Eureka2.1 原理图2.2 代码实现2.2.1 创建工程2.2.2 eureka-expansion2.2.2.1 添加依赖2.2.3 配置中心(config-server)2.2.3.1 添加依赖2.2.3.2 启动类2.2.3.3 配置文件2.2.4 eureka服务端(eureka-server)2.2.4.1 添加依
·
文章目录
1.概述
业务微服务化以后,我们要求服务高可用,于是我们可以部署多个相同的服务实例,并引入负载均衡机制。而微服务注册中心作为微服务化系统的重要单元,其高可用也是非常必要的,因此在生产中我们可能需要多个微服务注册中心实例来保证服务注册中心的稳定性。而在实际使用中可能会实现动态添加Eureka服务端。
2.Springcloud Config实现动态扩容Eureka
2.1 原理图
2.2 代码实现
2.2.1 创建工程
2.2.2 eureka-expansion
2.2.2.1 添加依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.9.RELEASE</version>
</parent>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<!-- 版本信息统一管理-->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.SR3</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
2.2.3 配置中心(config-server)
2.2.3.1 添加依赖
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
</dependencies>
2.2.3.2 启动类
@SpringBootApplication
//开启分布式配置中心服务器端
@EnableConfigServer
public class ConfigApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigApplication.class,args);
}
}
2.2.3.3 配置文件
spring:
application:
####注册中心应用名称
name: config-server
cloud:
config:
server:
git:
###git环境地址
uri: https://gitee.com/itmyx/testconfig.git
####搜索目录
search-paths:
- eureka
####读取分支
label: master
####端口号
server:
port: 8200
2.2.4 eureka服务端(eureka-server)
2.2.4.1 添加依赖
<dependencies>
<!--SpringCloud eureka-server里面包含web相关依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
2.2.4.2 启动类
@SpringBootApplication
@EnableEurekaServer
@RestController
public class EurekaSimpleApplication {
public static void main(String[] args) {
Scanner scanner =new Scanner(System.in);
new SpringApplicationBuilder(EurekaSimpleApplication.class).profiles(scanner.nextLine()).run(args);
}
@Autowired
EurekaClientConfigBean eurekaClientConfigBean;
@GetMapping("/eureka-service-info")
public Object getEurekaServerUrl(){
return eurekaClientConfigBean.getServiceUrl();
}
}
2.2.4.3 配置文件(bootstrap.yml)
spring:
application:
name: eureka-server
cloud:
config:
uri: http://localhost:8200
2.2.5 eureka客户端(eureka-client)
2.2.5.1 添加依赖
<dependencies>
<!-- SpringBoot整合Web组件 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- SpringBoot整合eureka客户端 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-client</artifactId>
</dependency>
</dependencies>
2.2.5.2 启动类
@SpringBootApplication
@EnableDiscoveryClient
@RestController
public class EurekaClientApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaClientApplication.class,args);
}
@Autowired
EurekaClientConfigBean eurekaClientConfigBean;
@GetMapping("/eureka-service-info")
public Object getEurekaServerUrl(){
return eurekaClientConfigBean.getServiceUrl();
}
}
2.2.5.3 配置文件(bootstrap.yml)
spring:
cloud:
config:
uri: http://localhost:8200
application:
name: eureka-client
profiles:
active: peer1
server:
port: 8300
management:
endpoints:
web:
exposure:
include: "*"
2.2.6 git仓库
- eureka-client-peer1.yml
eureka:
client:
service-url:
defaultZone: http://localhost:8100/eureka
- eureka-server-peer1.yml
###服务端口号
server:
port: 8100
###eureka 基本信息配置
eureka:
instance:
###注册到eurekaip地址
hostname: 127.0.0.1
client:
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
###因为自己是为注册中心,不需要自己注册自己
register-with-eureka: false
###因为自己是为注册中心,不需要检索服务
fetch-registry: false
management:
endpoints:
web:
exposure:
include: "*"
- eureka-server-peer2.yml
###服务端口号
server:
port: 8400
###eureka 基本信息配置
eureka:
instance:
###注册到eurekaip地址
hostname: 127.0.0.1
client:
serviceUrl:
defaultZone: http://localhost:8100/eureka/
###因为自己是为注册中心,不需要自己注册自己
register-with-eureka: false
###因为自己是为注册中心,不需要检索服务
fetch-registry: false
2.2.7 动态扩容演示
2.2.7.1 启动服务
分别启动config-server、eureka-server(输入peer1)、eureka-client
2.2.7.2 在启动eureka-server(输入peer2)
2.2.7.3 修改配置文件
eureka:
client:
service-url:
defaultZone: http://localhost:8100/eureka,http://localhost:8400/eureka
2.2.7.4 调用接口
post 127.0.0.1:8300/actuator/refresh
post 127.0.0.1:8100/actuator/refresh
2.2.7.5 模拟故障
停止8100注册中心
3.apollo实现eureka动态扩容
3.1 原理图
3.2 代码实现
3.2.1 Apollo配置中心搭建
- 略
3.2.2 注册中心服务(eureka-server)
3.2.2.1 添加依赖
<dependencies>
<!--SpringCloud eureka-server里面包含web相关依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>com.ctrip.framework.apollo</groupId>
<artifactId>apollo-client</artifactId>
<version>1.1.0</version>
</dependency>
<!--通用工具包-->
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>${hutool.version}</version>
</dependency>
</dependencies>
3.2.2.2 启动类
@SpringBootApplication
@EnableEurekaServer
@RestController
public class EurekaSimpleApplication {
public static void main(String[] args) {
Scanner scanner =new Scanner(System.in);
new SpringApplicationBuilder(EurekaSimpleApplication.class).properties("server.port="+scanner.nextLine()).run(args);
}
}
3.2.2.3 配置文件(bootstrap.yml)
app:
id: eureka-server
apollo:
meta: http://11.11.113.83:8080
bootstrap:
enabled: true
eagerLoad:
enabled: true
3.2.3 注册中心客户端(eureka-client)
3.2.3.1 添加依赖
<dependencies>
<!--SpringCloud eureka-server里面包含web相关依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>com.ctrip.framework.apollo</groupId>
<artifactId>apollo-client</artifactId>
<version>1.1.0</version>
</dependency>
<!--通用工具包-->
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>${hutool.version}</version>
</dependency>
</dependencies>
3.2.3.2 启动类
@SpringBootApplication
@EnableDiscoveryClient
@RestController
public class EurekaClientApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaClientApplication.class,args);
}
@Autowired
EurekaClientConfigBean eurekaClientConfigBean;
@GetMapping("/eureka-service-info")
public Object getEurekaServerUrl(){
return eurekaClientConfigBean.getServiceUrl();
}
}
3.2.3.3 监听类
@Component
public class EurekaRefresher {
@Autowired
EurekaClientConfigBean eurekaClientConfigBean;
@ApolloConfigChangeListener
public void onChange(ConfigChangeEvent changeEvent) {
if(changeEvent.isChanged("eureka.client.service-url.defaultZone")){
//自行修改配置文件方式
HttpUtil.createPost("http://localhost:8300/actuator/refresh").body("{}").execute();
System.out.println("eureka重新加载成功");
}
}
}
3.2.3.4 配置文件(bootstrap.yml)
app:
id: eureka-client
apollo:
meta: http://11.11.113.83:8080
bootstrap:
enabled: true
eagerLoad:
enabled: true
3.2.4 配置文件
- eureka-client
- eureka-server
3.2.5 测试
- 启动eureka-server输入8100
- 启动eureka-client
- 修改eureka-client配置文件发布
- 在启动eureka-server输入8400
- 停止8100服务查看8400效果
更多推荐
已为社区贡献3条内容
所有评论(0)