您的位置:首页 > 编程语言 > Java开发

Spring cloud Eureka服务注册及发现(一)创建Eurake服务器

2017-02-14 15:18 2066 查看
文章参考来源: lzhou666的 http://www.cnblogs.com/skyblog/p/5129603.html

所有的服务端及访问服务的客户端都需要连接到eureka服务器。服务在启动时会自动注册自己到eureka服务器,每一个服务都有一个名字,这个名字会被注册到eureka服务器。使用服务的一方只需要使用该名字加上方法名就可以调用到服务。

创建spring cloud eureka服务器和创建之前那个配置文件服务器类似,你只需要创建一个空的maven工程。



继承cloud付项目,引入eureka的jar包

<!--  1.引入springCloud parent包的继承-->
<parent>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-parent</artifactId>
<version>Brixton.RELEASE</version>
<relativePath />
</parent>

<groupId>com.vx.springCloud</groupId>
<artifactId>eurekaServer</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>eurekaServer</name>

<dependencies>
<!-- 2. 引入configserver服务包-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
<!-- spring配置客户端jar包  -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-client</artifactId>
</dependency>
</dependencies>


在启动类中使用EnableEurekaServer注解

@SpringBootApplication
@EnableEurekaServer
public class EurekaServer {
public static void main(String[] args) {
SpringApplication.run(EurekaServer.class,args);
}
}


可以看到只需要使用@EnableEurekaServer注解就可以让应用变为Eureka服务器,这是因为spring boot封装了Eureka Server,让你可以嵌入到应用中直接使用。

在application.yml中配置eureka服务信息

spring:
profiles:
#默认使用下面哪个配置项。启动项目的时候 可使用 -Dspring.profiles.active=dev 更换使用的配置项
active: test
#服务配置信息
application:
name: ${server.hostname}
cloud:
#配置服务器的设置信息
config:
#采用的文件类型 即:生产环境,测试环境。。
profile: ${spring.profiles.active}
label: master
#配置服务器的访问地址
uri: http://localhost:8888 
#将不同的配置以---分割
---
spring:
profiles: test
server:
hostname: eurekaServer
---
spring:
profiles: dev
server:
hostname: eurekaServer
---
#一下信息 尽量配置到git文件上,然后通过配置服务器获取
server:
port: 8761

eureka:
instance:
#eureka服务器的标识,如果是集群就可以写成 eurekaSer1,eurekaSer2,eurekaSer3..
hostname: ${server.hostname}
client:
registerWithEureka: false
fetchRegistry: false
#开启客户端存活状态监测
healthcheck:
enabled: true
serviceUrl:
defaultZone: http://${server.hostname}:${server.port}/eureka/[/code] 
其中server.port配置eureka服务器端口号。

registerWithEureka:表示是否注册自身到eureka服务器,因为当前这个应用就是eureka服务器,没必要注册自身,所以这里是false。

fetchRegistry:表示是否从eureka服务器获取注册信息,同上,这里不需要。

defaultZone:是设置eureka服务器所在的地址,查询服务和注册服务程序都注册到这个地址。

启动Eureka服务端

通过main方法运行后,输入http://127.0.0.1:8761/ 就可打开eurake管理界面

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Eureka springboot
相关文章推荐