第一篇:【Spring Cloud】搭建注册中心
本地代码创建的工具是STS,spring boot出来的时候官方推荐的。
1.创建eureka

2.选择eureka注册中所需要的组件 Eureka Server
引入spring-cloud-starter-netflix-eureka-server的依赖,注册中心核心包

3.pom文件如下
<?xml version="1.0" encoding="UTF-8"?>"http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 com.cohesion spring-cloud-eureka 0.0.1-SNAPSHOT jar spring-cloud-eureka Demo project for Spring Boot org.springframework.boot spring-boot-starter-parent 2.0.3.RELEASE UTF-8 UTF-8 1.8 Finchley.RELEASE org.springframework.cloud spring-cloud-starter-netflix-eureka-server org.springframework.boot spring-boot-starter-test test org.springframework.cloud spring-cloud-dependencies ${spring-cloud.version} pom import org.springframework.boot spring-boot-maven-plugin
4.配置 配置文件
application.yml
registerWithEureka表示是否注册自身到eureka服务器,因为当前这个应用就是eureka服务器,没必要注册自身,所以这里是false
fetchRegistry表示是否从eureka服务器获取注册信息,同上,这里不需要。
defaultZone就比较重要了,是设置eureka服务器所在的地址,查询服务和注册服务都需要依赖这个地址
server: port: 8080 eureka: instance: hostname: localhost client: register-with-eureka: false fetch-registry: false service-url: defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ spring: application:

5.配置主类,生命这个项目是注册中心
package com.cohesion; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @SpringBootApplication @EnableEurekaServer//标记自己是注册中心 public class SpringCloudEurekaApplication { public static void main(String[] args) { SpringApplication.run(SpringCloudEurekaApplication.class, args); } }
6.启动项目 run-SpringCloudEurekaApplication
http://localhost:8080
