主要配置
- Eureka客户端工程
- user_service 服务提供
- 服务地址使用ip方式
- 续约
- consumer_demo 服务消费
- 获取服务地址的频率
- user_service 服务提供
- Eureka服务端工程 eureka_server
- 失效剔除
- 自我保护
在各服务的配置文件application.yml中配置,其他文件无需修改
Eureka服务
Eureka是服务注册中心,只做服务注册;自身并不提供服务也不消费服务。可以搭建web工程使用Eureka,可以使用Spring Boot方式搭建。
导入坐标
- pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>springcloud_parent</artifactId>
<groupId>li.chen.com</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>eureka_service</artifactId>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-netflix-eureka-server -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
</project>
配置文件
- application.yml
server:
port: 10086
spring:
application:
name: eureka_server
eureka:
client:
service-url:
# eureka 服务地址,如果是集群的话;需要指定其它集群eureka地址
defaultZone: http://127.0.0.1:10086/eureka
# 不注册自己 集群才需要
register-with-eureka: false
# 不拉取服务
fetch-registry: false
server:
# 服务失效剔除时间间隔,默认60秒(单位毫秒)
eviction-interval-timer-in-ms: 60000
# 关闭自我保护模式(默认是打开的;即爆红提示,开发时可关闭)
enable-self-preservation: false
创建启动类
package li.chen.com;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
/**
* @ClassName: EurekaServerApplication
* @Description 启动引导类
* @EnableEurekaServer 声明当前类为Eureka服务
**/
@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class,args);
}
}
测试
启动 EurekaServerApplication
访问路径 http://127.0.0.1:10086/
服务注册(user_service)
导入坐标
- pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/200