前言
本文依托于SpringCloud最新版环境集成-2021年11月
只介绍eureka环境搭建过程。
在SpringCloud的使用过程中我总结为三步曲。
- 引入spring-cloud-starter相应jar包
- properties或yml加入相关配置
- 启动类加上@Enable相关注解
eureka服务端
1.引入jar
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
<exclusions>
<exclusion>
<artifactId>gson</artifactId>
<groupId>com.google.code.gson</groupId>
</exclusion>
</exclusions>
</dependency>
2.properties加入配置
spring.application.name=springcloud-eureka-server
server.port=10001
# 不向eureka注册
eureka.client.register-with-eureka=false
# 不从eureka获取注册信息
eureka.client.fetch-registry=false
# 向eureka注册的地址,后面服务名称必须为eureka
eureka.client.serviceUrl.defaultZone=http://localhost:10001/eureka/
3.启动类加入注解
@EnableEurekaServer
eureka集群
只需要稍微修改properties的eureka的注册地址。
1.eureka1的application.properties
# 向eureka注册的地址,后面服务名称必须为eureka
eureka.client.serviceUrl.defaultZone=http://localhost:10002/eureka/
2.eureka2的application.properties
# 向eureka注册的地址,后面服务名称必须为eureka
eureka.client.serviceUrl.defaultZone=http://localhost:10001/eureka/
问题
1.eureka无法访问
- eureka的defaultZone服务名称必须是eureka,而不能是你定义的其他名称。
- eureak的网页是只需要访问http://localhost:10001/的地址,而不需要eureka服务名
2.服务启动报错
Description:
An attempt was made to call a method that does not exist. The attempt was made from the following location:
java.lang.invoke.MethodHandleNatives.resolve(Native Method)
The following method did not exist:
com.google.gson.GsonBuilder.setLenient()Lcom/google/gson/GsonBuilder;
The method's class, com.google.gson.GsonBuilder, is available from the following locations:
jar:file:/D:/maven/maven-depostitory/com/google/code/gson/gson/2.1/gson-2.1.jar!/com/google/gson/GsonBuilder.class
The class hierarchy was loaded from the following locations:
com.google.gson.GsonBuilder: file:/D:/maven/maven-depostitory/com/google/code/gson/gson/2.1/gson-2.1.jar
Action:
Correct the classpath of your application so that it contains a single, compatible version of com.google.gson.GsonBuilder
由于我使用的是最新版的jar包,网络上的教程直接引入 spring-cloud-starter-eureka-server.jar,而我的是spring-cloud-starter-netflix-eureka-server.jar,结果却启动报错。
根据报错发现和gson包有关系,我不需要使用gson包,因此exclusion排除掉即可。