Eureka:Netflex旗下基于REST的一个中间件服务,用于解决微服务业务中的服务负载均衡及故障转移问题,具体表现为服务注册和服务发现二大核心功能点。Eureka包含两个组件:
1,服务注册中心:Eureka server。Eureka server负责服务注册,接受Eureka client的注册信息(服务名,端口,版本,协议等)并作保存,各节点的状态支持图形界面展示。由于Eureka是采用的HTTP协议,所以Eureka server的健康检查依赖于Eureka client的定时心跳上报,节点心跳频率默认为30s,注册中心默认在90秒内没有收该节点的心跳则剔除该节点。
2,节点:Eureka client。节点通过http向注册中心提交自己的注册信息并定时向注册中心上报自己的状态(默认30s),也会定时向注册中心拉取其保存的服务注册列表。
Eureka可配置内容如下可参考这篇博客:EUREKA配置详细说明
一,搭建服务注册中心
1,新建springboot项目,pom.xml中加入artifactId为:"spring-cloud-starter-netflix-eureka-server"和"spring-boot-starter-web"的依赖
<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>
2,在springboot的启动类中启用Eureka注册中心注解
@EnableEurekaServer @SpringBootApplication public class KsApplication { public static void main(String[] args) { SpringApplication.run(KsApplication.class, args); } }
3,在src/main/resources中新建application.yml文件,配置如下:
server: port: 8081 eureka: instance: hostname: 127.0.0.1 client: registerWithEureka: false fetchRegistry: false serviceUrl: defalutZone: http://${eureka.instance.hostname}:${server.port}/eureka
4,启动项目并打开网页:http://localhost:8081即可进入eureka管理界面
5,集成spring security对注册中心进行密码保护
1),pom.xml加入artifactId为"spring-boot-starter-security"的依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency>
2),新建一个WebSecurityConfig类
import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; @EnableWebSecurity public class WebSecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { super.configure(http); http.csrf().disable(); } }
3),配置文件application.yml进对密码配置
server:
port: 8081
spring:
security:
user:
name: liujb
password: 123456
eureka:
instance:
hostname: 127.0.0.1
client:
register-with-eureka: false
fetch-registry: false
service-url:
defaultZone: http://localhost:8081/eureka
4) 重启程序
二,建立Eureka服务提供者(节点)
1, 与建议服务注册中心一样,pom.xml中加入artifactId为:"spring-cloud-starter-netflix-eureka-server"和"spring-boot-starter-web"的依赖
2,启动类启用@EnableEurekaClient注解
@EnableEurekaClient
@SpringBootApplication
public class EurekNodeApplication {
public static void main(String[] args) {
SpringApplication.run(EurekNodeApplication.class, args);
}
}
3,新建application.yml,配置如下内容
server:
port: 8082
ek:
username: liujb
password: 123456
eureka-url: 127.0.0.1
eureka-port: 8081
eureka:
client:
service-url:
defaultZone:
http://${ek.username}:${ek.password}@${ek.eureka-url}:${ek.eureka-port}/eureka
spring:
application:
name: eureka-provide-1
4,打开注册中心,可以看到eureka-provide-1 已经注册成功
三,建立asp.net core 服务提供者
1,新建一个web api项目并添加nuget程序包:Steeltoe.Discovery.ClientCore(2.1.1)
2,配置文件appsetting.json
{ "Logging": { "LogLevel": { "Default": "Warning" } }, "AllowedHosts": "*", "spring": { "application": { "name": "eureka-provide-2" } }, "eureka": { "client": { "serviceUrl": "http://eureka:123456@localhost:8765/eureka/", "shouldFetchRegistry": true, "validateCertificates": false }, "instance": { "port": 8013, "preferIpAddress": true, "instanceId": "eureka-provide-2:8013" } } }
3,Startup.Configservice方法中添加服务发现中间件:services.AddDiscoveryClient(Configuration);
4,Startup.Config方法中启用服务发现中间件:app.UseDiscoveryClient();
5,启动项目后服务注册成功