在使用Eureka时,分为服务端配置和用户端配置
1.服务端配置:
1.1yml文件配置
server:
port: 7001
eureka:
client:
#表示是否将自己注册到Eureka-Server中,默认true
register-with-eureka: false
#false表示自己端就是注册中心,职责就是维护服务实例,并不需要去检索服务
fetch-registry: false
#设置注册中心的地址,注意加空格,访问地址localhost:7001
service-url:
defaultZone: http://localhost:${server.port}/eureka/
server:
# 是否启用自我保护机制
enable-self-preservation: false
1.2 maven依赖
<properties>
<spring-cloud.version>Finchley.SR4</spring-cloud.version>
</properties>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
1.3启动类添加注解
@SpringBootApplication
@EnableEurekaServer //启用Eureka服务器
public class CloudEureka7001Application {
public static void main(String[] args) {
SpringApplication.run(CloudEureka7001Application.class, args);
}
}
2.用户端配置
2.1yml文件配置(注册端)
server:
port: 8001
spring:
application:
# 应用名,在Erueka注册中心显示的服务名
name: user-provider
eureka:
client:
#表示是否将自己注册到Eureka-Server中,默认true
register-with-eureka: true
#false表示自己端就是注册中心,职责就是维护服务实例,并不需要去检索服务
fetch-registry: false
#设置注册中心的地址,注意加空格,访问地址localhost:7001
service-url:
defaultZone: http://127.0.0.1:7001/eureka/
# 在Eureka中注册的实例名称
instance:
instance-id: user-provider:8001
2.2yml文件配置(调用端)
server:
port: 8080
eureka:
client:
# 配置是否将自己注册到Eureka-Server中
register-with-eureka: false
# 获取服务提供者的地址信息
fetch-registry: true
# 配置eureka地址,获取服务用
service-url:
defaultZone: http://127.0.0.1:7001/eureka/
spring:
application:
name: user-consumer
# feign调用超时时间配置
#feign:
# client:
# config:
# default:
# connectTimeout: 10000
# readTimeout: 600000
2.3maven依赖
<properties>
<spring-cloud.version>Finchley.SR4</spring-cloud.version>
</properties>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
2.4注册服务添加注解
@SpringBootApplication
@EnableEurekaClient //启用Eureka 客户端,注册服务端
public class CloudProvider8001Application {
public static void main(String[] args) {
SpringApplication.run(CloudProvider8001Application.class, args);
}
}
2.5调用服务添加注解
@SpringBootApplication
// 启用Eureka客户端,调用服务端
@EnableEurekaClient
// 启用Feign客户端,扫描指定包下所有Feign注解
@EnableFeignClients(basePackages = "com.itany.service")
public class CloudConsumer8080Application {
public static void main(String[] args) {
SpringApplication.run(CloudConsumer8080Application.class, args);
}
}
// 调用的是服务名,到Eureka中查找对应的服务名,找到的是微服务的ip:端口
// 即调用配置中的http://localhost:7001/eureka/获取到的配置的user-provider对应的服务地址http://localhost:8001
@FeignClient(value = "user-provider")
// 注入Service
@Service
public interface UserService {
@GetMapping("/users")
public ResponseResult getUserList();
}
3.问题记录
1.用户端访问Eureka服务时超时或502错误时,检查网络是否有限制(我用的公司内网访问受限,切换热点后连接正常)
优先排查端口、网络等本地配置