Eureka注册中心集群搭建
1.微服务RPC远程服务调用的核心
- 高可用,注册中心只有一个, 它出故障了会导致整个为服务环境不可用,所以搭建Eureka注册中心集群 ,实现负载均衡+故障容错
2.创建模块cloud-eureka-server7001和cloud-eureka-server7002
<!--两个服务都添加上eureka依赖-->
<!--eureka-server-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
3.host修改
127.0.0.1 eureka7001.com
127.0.0.1 eureka7002.com
4.添加application.yml
eureka:
instance:
hostname: eureka7002.com
client:
register-with-eureka: false
fetch-registry: false
service-url:
defaultZone: http://eureka7001.com:7002/eureka/
eureka:
instance:
hostname: eureka7002.com
client:
register-with-eureka: false
fetch-registry: false
service-url:
defaultZone: http://eureka7001.com:7001/eureka/
5.服务注册
<!--需要注册的也添加eureka依赖-->
<!--eureka-client-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
6.application.yml
eureka:
client:
register-with-eureka: true
fetchRegistry: true
service-url:
defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka
7.添加复制均衡注解
@Configuration
public class ApplicationContextConfig {
@Bean
@LoadBalanced
public RestTemplate restTemplate()
{
return new RestTemplate();
}
}