赞
踩
目录
一、建立服务注册中心(eurekaserver)
1. 创建springboot项目,添加依赖(使用IDEA2021)
选择依赖
2. 引入eureka的依赖(服务注册中心)
1.5版本使用spring-cloud-starter-eureka-server还是没问题的。2.0以上建议使用 spring-cloud-starter-netflix-eureka-server。
- <!--引入eureka的依赖-->
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
- </dependency>
引入依赖后注意更新pom从maven创库下载依赖包,注意pom.xml文件的以下几点(如图):
3. 配置application.properties文件
- # 应用名称
- spring.application.name=eurekaserver
- # 应用服务 WEB 访问端口
- #server.port=8080
-
- #设置Eureka端口
- server.port=8761
-
- #eureka服务端的实例名称
- eureka.instance.hostname=localhost
- #false表示不向注册中心注册自己
- eureka.client.register-with-eureka=false
- #false表示自己端就是注册中心,我的职责就是维护实例,并不需要去检索服务
- eureka.client.fetch-registry=false
- #设置与Eureka Server交互的地址查询服务和注册服务都需要依赖这个地址
- eureka.client.service-url.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/

4. 添加@EnableEurekaServer注解到springboot项目启动类
如果@EnableEurekaServer 注解 红色警告: 需要添加<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>依赖,下载更新
- package com.eurekaserver;
-
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
- import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
-
- @SpringBootApplication
- @EnableEurekaServer
- public class EurekaserverApplication {
-
- public static void main(String[] args) {
- SpringApplication.run(EurekaserverApplication.class, args);
- }
-
- }
运行: http://localhost:8761/ 结果如下图界面:(此时无服务注册)
1. 创建springboot项目,添加依赖(使用IDEA2021)
略过:此处同建立服务注册中心步骤,工程名:eurekauser
2. 引入eureka的客户端依赖
- <!--引入eureka的客户端依赖-->
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
- </dependency>
引入依赖注意更新pom从maven创库下载依赖包,如图:(另注意创建注册中心时的两张截图)
3. 配置application.properties文件
- # 应用名称
- spring.application.name=eurekauser
- # 应用服务 WEB 访问端口
- server.port=8081
-
- #更改主机实例ID名
- eureka.instance.instance-id=${spring.application.name}:${server.port}
- #鼠标悬停时显示主机ip地址
- eureka.instance.prefer-ip-address=true
-
- #表示是否将自己注册进Eureka,默认为true
- eureka.client.register-with-eureka=true
- #是否从EurekaServer抓取已有的注册信息,默认为true
- eureka.client.fetch-registry=true
- #向服务注册中心注册自己(此处为注册中心地址)
- eureka.client.service-url.defaultZone=http://localhost:8761/eureka/

4. 添加@EnableEurekaClient注解到springboot项目启动类
@EnableEurekaClient只适用于Eureka作为注册中心,@EnableDiscoveryClient 可以是其他注册中心。
- package com.eurekauser;
-
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
- import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
- import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
-
- /**
- * @EnableEurekaClient只适用于Eureka作为注册中心,@EnableDiscoveryClient 可以是其他注册中心。
- */
- @SpringBootApplication
- //@EnableEurekaClient
- @EnableDiscoveryClient
- public class EurekauserApplication {
-
- public static void main(String[] args) {
- SpringApplication.run(EurekauserApplication.class, args);
- }
-
- }

先启动eurekaserver,再启动这个(eurekauser)实例,输入eurekaserver的实例地址(http://localhost:8761/),出现如下界面:(此时服务:eurekauser成功在Eureka注册)
总结
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。