Eureka
单例Eureka
首先创建一个 Maven 项目,在 pom.xml 中配置 Eureka 的依赖信息,代码如下所示。
<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">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.shareprog</groupId>
<artifactId>shareprog-pom</artifactId>
<version>1.0.0</version>
</parent>
<artifactId>shareprog-eureka</artifactId>
<name>eureka</name>
<description>服务发现和治理</description>
<dependencies>
<!-- eureka-server的依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<!--健康监控-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!--spring-security-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
接下来在src/main/resource下面创建一个application.yml文件。
server:
port: 8001
spring:
application:
name: eureka
security:
user:
name: eureka
password: eureka
eureka:
instance:
hostname: 127.0.0.1 #UAT应用实例主机名
client:
register-with-eureka: false #是否向注册中心注册自己
fetch-registry: false #是否从Eureka获取注册信息
service-url:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
server:
wait-time-in-ms-when-sync-empty: 5 #在服务器接收请求之前等待的初始时间(分钟),用于生产配置
# enable-self-preservation: false #自我保护是否启用
# eviction-interval-timer-in-ms: 5000 #清理间隔
创建一个启动类代码如下:
package com.shareprog.eureka;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
/**
* @ClassName: EurekaApplication
* @Description: Eureka启动类
* @author cl
* @date 2021年1月13日
*/
@SpringBootApplication
@EnableEurekaServer
public class EurekaApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaApplication.class, args);
}
}
这样一个微服务注册中心就完成了。
如果上面配置文件中设置用户认证,那么还需要加配置,代码如下:
package com.shareprog.eureka.configuration;
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;
import org.springframework.security.config.http.SessionCreationPolicy;
/**
* @ClassName: WebSecurityConfig
* @Description: 启用登录权限配置
* @author cl
* @date 2021年1月13日
*/
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
/**
* 注意:为了可以使用 http://${user}:${password}@${host}:${port}/eureka/
* 这种方式登录,所以必须是httpBasic,
* 如果是form方式,不能使用url格式登录
*/
http.csrf().disable()
.authorizeRequests().anyRequest().authenticated()
.and().httpBasic()
.and().sessionManagement().sessionCreationPolicy(SessionCreationPolicy.NEVER);
}
}
这样项目就不会报错了。
如果有需求说,需要在服务注册或者服务宕机的时候进行处理,比如打印日志,发送邮件之类的,代码如下:
package com.shareprog.eureka.configuration;
import org.springframework.cloud.netflix.eureka.server.event.EurekaInstanceCanceledEvent;
import org.springframework.cloud.netflix.eureka.server.event.EurekaInstanceRegisteredEvent;
import org.springframework.cloud.netflix.eureka.server.event.EurekaInstanceRenewedEvent;
import org.springframework.cloud.netflix.eureka.server.event.EurekaRegistryAvailableEvent;
import org.springframework.cloud.netflix.eureka.server.event.EurekaServerStartedEvent;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;
import com.netflix.appinfo.InstanceInfo;
import lombok.extern.slf4j.Slf4j;
@Component
@Slf4j
public class EurekaStateChangeListener {
@EventListener
public void listen(EurekaInstanceCanceledEvent event) {
log.error(event.getServerId() + "\t" + event.getAppName() + " 服务下线 ");
}
@EventListener
public void listen(EurekaInstanceRegisteredEvent event) {
InstanceInfo instanceInfo = event.getInstanceInfo();
log.error(instanceInfo.getAppName() + " 进行注册 ");
}
@EventListener
public void listen(EurekaInstanceRenewedEvent event) {
log.error(event.getServerId() + "\t" + event.getAppName() + " 服务进行续约 ");
}
@EventListener
public void listen(EurekaRegistryAvailableEvent event) {
log.error(" 注册中心启动 ");
}
@EventListener
public void listen(EurekaServerStartedEvent event) {
log.error("Eureka Server启动 ");
}
}
Eureka集群
Eureka集群也很简单,只需要将搭建好的Eureka复制三份分别到不同的三台服务器上,修改不同的配置文件即可,三台服务配置如下:
Eureka1
server:
port: 8001
spring:
application:
name: eureka
security:
user:
name: eureka
password: eureka
eureka:
instance:
hostname: 192.168.0.1 #UAT应用实例主机名
client:
register-with-eureka: false #是否向注册中心注册自己
fetch-registry: false #是否从Eureka获取注册信息
service-url:
defaultZone: http://eureka:eureka@192.168.0.2:8001/eureka/,http://eureka:eureka@192.168.0.3:8001/eureka/
Eureka2
server:
port: 8001
spring:
application:
name: eureka
security:
user:
name: eureka
password: eureka
eureka:
instance:
hostname: 192.168.0.2 #UAT应用实例主机名
client:
register-with-eureka: false #是否向注册中心注册自己
lease-renewal-interval-in-seconds: 30 #服务续约任务的调用间隔时间,默认为30秒。
lease-expiration-duration-in-seconds: 90 #服务失效的时间,默认为90秒。
fetch-registry: false #是否从Eureka获取注册信息
service-url:
defaultZone: http://eureka:eureka@192.168.0.1:8001/eureka/,http://eureka:eureka@192.168.0.3:8001/eureka/
Eureka3
server:
port: 8001
spring:
application:
name: eureka
security:
user:
name: eureka
password: eureka
eureka:
instance:
hostname: 192.168.0.3 #UAT应用实例主机名
client:
register-with-eureka: false #是否向注册中心注册自己
fetch-registry: false #是否从Eureka获取注册信息
service-url:
defaultZone: http://eureka:eureka@192.168.0.1:8001/eureka/,http://eureka:eureka@192.168.0.2:8001/eureka/
获取其他服务的注册信息
@Autowired
private DiscoveryClient discoveryClient;
public void getServiceInstances() {
List<String> services = discoveryClient.getServices();
for (String service : services) {
List<ServiceInstance> instances = discoveryClient.getInstances(service);
for (ServiceInstance instance : instances) {
EurekaDiscoveryClient.EurekaServiceInstance eurekaServiceInstance = (EurekaDiscoveryClient.EurekaServiceInstance)instance;
InstanceInfo instanceInfo = eurekaServiceInstance.getInstanceInfo();
System.out.println(JSON.toJSONString(instanceInfo));
}
}
}
自定义元数据
eureka:
instance:
metadata-map:
test: "test"
在上面获取注册信息的时候使用
System.out.println(instanceInfo.getMetadata().get("test"));