在启动类添加注解
@SpringBootApplication @EnableEurekaServer // 启用Eureka服务 public class RegistryApplication { private static final Logger log = LoggerFactory.getLogger(RegistryApplication.class); private final Environment env; public RegistryApplication(Environment env) { this.env = env; } /** * Main method, used to run the application. * * @param args the command line arguments */ public static void main(String[] args) { SpringApplication app = new SpringApplication(RegistryApplication.class); Environment env = app.run(args).getEnvironment(); logApplicationStartup(env); } private static void logApplicationStartup(Environment env) { String protocol = "http"; if (env.getProperty("server.ssl.key-store") != null) { protocol = "https"; } String serverPort = env.getProperty("server.port"); String contextPath = env.getProperty("server.servlet.context-path"); if (StringUtils.isBlank(contextPath)) { contextPath = "/"; } String hostAddress = "localhost"; try { hostAddress = InetAddress.getLocalHost().getHostAddress(); } catch (UnknownHostException e) { log.warn("The host name could not be determined, using `localhost` as fallback"); } log.info("\n----------------------------------------------------------\n\t" + "Application '{}' is running! Access URLs:\n\t" + "Local: \t\t{}://localhost:{}{}\n\t" + "External: \t{}://{}:{}{}\n\t" + "Profile(s): \t{}\n----------------------------------------------------------", env.getProperty("spring.application.name"), protocol, serverPort, contextPath, protocol, hostAddress, serverPort, contextPath, env.getActiveProfiles()); String configServerStatus = env.getProperty("configserver.status"); if (configServerStatus == null) { configServerStatus = "Not found or not setup for this application"; } log.info("\n----------------------------------------------------------\n\t" + "Config Server: \t{}\n----------------------------------------------------------", configServerStatus); } }@EnableEurekaServer // 启用Eureka服务 里面的代码不用在意,只是在启动时展示的信息,方便观看启动完成与一些属性展示 然后删掉application.properties, 新建application.yml ,我个人喜欢yml格式配置清晰一些 添加配置
#服务器配置 server: #端口 port: 8761 # 1.服务注册中心集群相互注册一定要开启 # register-with-eureka: true # fetch-registry: true # 2.服务注册中心集群的spring.application.name一定要一样 # 3.服务注册中心集群eureka.client.service-url.defaultZone:不能出现 localhost,一定要使用host指定主机名 # eureka: instance: #主机名称 hostname: registry_1 appname: registry client: service-url: #服务注册中心地址,查询服务和注册服务都需要依赖这个地址,默认地址:[http://localhost:8761/eureka](http://localhost:8761/eureka) ,可以配置多个地址用逗号分隔 defaultZone: http://${user.name}:${user.password}@192.168.0.112:${server.port}/eureka #禁用将自己注册到自己Eureka Server register-with-eureka: false #禁用从Eureka Server 获取注册信息 fetch-registry: false user: name: admin password: admin_1里面注解部分是集群说明,此次我们先不做集群,这里面我对连接做了账户密码的简单效验这个user配置是自定义配置,名称自己定义,user.name与user.password的校验,服务注册时需要与这里的admin,admin_1一直才可以注册进来,然后运行,根据端口号打开页面
添加依赖
<!--rest接口依赖--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--微服务依赖--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency>与上同,在application.yml中添加配置
#spring配置 spring: application: #应用名称(服务提供者) name: app-producer-1 #服务器配置 server: #端口 port: 8001 #服务中心发现注册配置 eureka: client: #服务注册中心地址 service-url: defaultZone: http://${user.name}:${user.password}@192.168.0.112:8761/eureka user: name: admin password: admin_1注意,user账户带上,不然不能注册,运行,在服务中心页面即可看见这个服务