最近在学习spring-cloud微服务相关内容,首先对服务治理Eureka了解学习,记录下学习过程中的内容。
一,服务注册中心
1,Maven依赖,这里使用的是Camden.RELEASE版本
<parent>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-parent</artifactId>
<version>Camden.RELEASE</version>
<relativePath/>
</parent>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
2,创建启动类
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
/**
* EUREKA SERVER
* 使用SpringBoot注解快速构建
*/
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
public static void main( String[] args ) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
3,配置注册中心信息
eureka:
instance:
hostname: localhost
instance-id: ${spring.application.name}:${spring.cloud.client.ipAddress}:${spring.application.instance_id:${server.port}}
client:
#是否从注册中心获取注册信息,默认为true
fetch-registry: false
#是否将自身注册到注册中心,默认为true
register-with-eureka: false
#eureka注册中心地址(/eureka是固定值)
service-url:
defaultZone: http://localhost:${server.port}/eureka
spring:
application:
name: eureka-server
server:
port: 8000
这里需要注意的是配置文件使用的是YAML语言,需要注意一些使用规范:
1.大小写敏感
2.使用缩进表示层级关系
3.缩进时不允许使用Tab键,只允许使用空格
4.缩进的空格数目不重要,只要相同层级的元素左侧对齐即可
4,启动注册中心
可以直接运行main函数,也可以使用maven命令spring-boot:run
输出如下信息表示启动成功:
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v1.4.0.RELEASE)
省略输出信息......
2017-08-08 21:02:03.002 INFO 1932 --- [ Thread-9] e.s.EurekaServerInitializerConfiguration : Started Eureka Server
2017-08-08 21:02:03.391 INFO 1932 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8000 (http)
2017-08-08 21:02:03.394 INFO 1932 --- [ main] c.n.e.EurekaDiscoveryClientConfiguration : Updating port to 8000
2017-08-08 21:02:03.426 INFO 1932 --- [ main] c.s.c.eureka.EurekaServerApplication : Started EurekaServerApplication in 20.1 seconds (JVM running for 39.913)
5,管理页面


目前为止,一个Spring-Cloud-Eureka的服务注册中心就构建好了
网友评论