搭建eureka-server工程
需求
添加eureka对应依赖和编写引导类搭建eureka服务并可访问eureka服务界面分析:
Eureka是服务注册中心,只做服务注册;自身并不提供服务也不消费服务。可以搭建web工程使用Eureka使用Spring Boot方式搭建。
搭建步骤
1 创建工程
2 添加启动器依赖
<artifactId>eureka-server</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
3 编写启动引导类(添加Eureka的服务注解)
启动引导类:
package com.yy;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
//声明当前应用时Eureka服务
@EnableEurekaServer
@SpringBootApplication
@EnableDiscoveryClient //开启Eureka客户端发现功能
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
4 修改配置文件(端口,应用名称…)
application.yml配置文件:
server:
port: ${port:10086}
spring:
application:
name: eureka-server
eureka:
client:
service-url:
# eureka 服务地址,如果是集群的话;需要指定其它集群eureka地址
defaultZone: http://127.0.0.1:10086/eureka
# false代表不注册自己,如果以后是集群还是需要去注册,默认是true
register-with-eureka: false
# 不拉取服务,默认是true
fetch-registry: false
5 测试
配置成功的话,启动引导类,可以通过浏览器访问http://127.0.0.1:10086/,出现下面的内容