搭建基于Spring Cloud的微服务注册中心

版权声明:本文为原创文章,转载请注明转自Clement-Xu的csdn博客。 https://blog.csdn.net/ClementAD/article/details/53667478
简单几步,搭建基于Spring Cloud的微服务注册中心,包括几个特性:
- 提供注册中心由服务提供者进行注册
- 服务消费者在注册中心发现服务所在的位置
- 高可用(集群)配置

1、引入依赖:
需要引入两个依赖:
	<dependencies>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-eureka-server</artifactId>
		</dependency>
      .....
	</dependencies>

	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-dependencies</artifactId>
				<version>Camden.SR2</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>

2、配置:
为了配置高可用的注册中心集群,我们配置了3个注册中心互相备用。假设所在的主机分别为:
- 192.168.0.9
- 192.168.0.10
- 192.168.0.18

端口为58000。
spring:
  profiles:
    active: dev
  application:
    name: eureka-service
    
server:
  port: 58000
management:
  port: 59000

eureka:
  client:
    #register-with-eureka: false
    fetch-registry: false
  #instance:
  #  prefer-ip-address: true
        
---
# 开发环境配置
spring:
  profiles: dev

---
# 开发服0配置
spring:
  profiles: dev0

eureka:
  instance:
    hostname: 192.168.0.9
  client:
    service-url:
      defaultZone: http://192.168.0.10:58000/eureka/,http://192.168.0.18:58000/eureka/

---
# 开发服1配置
spring:
  profiles: dev1

eureka:
  instance:
    hostname: 192.168.0.10
  client:
    service-url:
      defaultZone: http://192.168.0.9:58000/eureka/,http://192.168.0.18:58000/eureka/

---
# 开发服2配置
spring:
  profiles: dev2

eureka:
  instance:
    hostname: 192.168.0.18
  client:
    service-url:
      defaultZone: http://192.168.0.10:58000/eureka/,http://192.168.0.9:58000/eureka/

3、代码:
只需要一个主类就行了:
@EnableEurekaServer
@SpringBootApplication
public class RegistryApplication {

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

4、部署和检查结果:
把三个服务中心分别部署到三个服务器中,然后打开管理页面:
http://192.168.0.9:58000/

可以看到是否已经正常启动,各个备用(replicas)注册中心是否available。


猜你喜欢

转载自blog.csdn.net/ClementAD/article/details/53667478