准备工作:创建cloud工程
<dependencies>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-config-serverartifactId>
dependency>
dependencies>
启动类
package com.msr.batter.config;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
@SpringBootApplication
@EnableConfigServer
public class ConfigApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigApplication.class, args);
}
}
配置文件:resources/bootstrap.yml
spring:
profiles:
active: native
application:
name: config-server
server:
port: 8888
这里不是主要讲解Spring Cloud Config
内容。所以对于配置文件使用native的profile,也就是使用文件来存储配置,默认是放在resources/config
目录下。在Eureka Server和Eureka Client模块分别引入spring-cloud-starter-config
。分别都创建一个ServerController
和ClientController
。
Eureka Client的配置文件:resources/config/eureka-client.yml
server:
port: 8081
spring:
application:
name: cloud-eureka-client1
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/ # 一个Eureka Server
Eureka Server配置文件:resources/config/eureka-server-peer1.yml
server:
port: 8761
spring:
application:
name: cloud-eureka-server
eureka:
instance:
hostname: localhost
preferIpAddress: true
client:
registerWithEureka: true
fetchRegistry: true
serviceUrl:
defaultZone: http://localhost:8761/eureka/ # 一个Eureka Server
server:
waitTimeInMsWhenSyncEmpty: 0
enableSelfPreservation: false
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-configartifactId>
dependency>
ServerController
package com.msr.better.registry.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.netflix.eureka.EurekaClientConfigBean;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("query")
public class ServerController {
@Autowired
EurekaClientConfigBean eurekaClientConfigBean;
@GetMapping("eureka-server")
public Object getEurekaServerUrl() {
return eurekaClientConfigBean.getServiceUrl();
}
}
配置文件:resources/bootstrap.yml
spring:
application:
name: cloud-eureka-server
cloud:
config:
uri: http://localhost:8888
management:
endpoints:
web:
exposure:
include: '*'
配置文件:resources/application.yml
eureka:
server:
peer-eureka-nodes-update-interval-ms: 10000 #默认是10分钟即600000,这里为了验证改为10秒
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-configartifactId>
dependency>
ClientController
package com.msr.better.client.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.netflix.eureka.EurekaClientConfigBean;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @author MaiShuRen
* @date 2020-10-23
*/
@RestController
@RequestMapping("query")
public class ClientController {
@Autowired
private DiscoveryClient discoveryClient;
@Autowired
private EurekaClientConfigBean clientConfigBean;
/**
* 服务实例信息
*
* @return
*/
@GetMapping("info")
public Map<String, Object> info() {
Map<String, Object> map = new HashMap<>(16);
List<String> services = discoveryClient.getServices();
String description = discoveryClient.description();
int order = discoveryClient.getOrder();
map.put("services", services);
map.put("description", description);
map.put("order", order);
return map;
}
/**
* 根据服务名称获取服务实例列表
*
* @param name 服务名
* @return 返回结果
*/
@GetMapping("instance/{name}")
public List<ServiceInstance> getIns(@PathVariable String name) {
return discoveryClient.getInstances(name);
}
/**
* 获取Eureka Server的url
*
* @return
*/
@GetMapping("eureka-server")
public Object getServiceUrl() {
return clientConfigBean.getServiceUrl();
}
}
配置文件:resources/bootstrap.yml
spring:
application:
name: cloud-eureka-client
cloud:
config:
uri: http://localhost:8888
management:
endpoints:
web:
exposure:
include: '*'
费别启动cloud-config-server
、cloud-eureka-server
(使用peer1的profile)、cloud-eureka-client
。
mvn spring-boot:run 在cloud-config-server目录下执行,启动cloud-config-server服务
mvn spring-boot:run -Dspring-boot.run.profiles=peer1 在cloud-eureka-server目录下执行,启动cloud-eureka-server
mvn spring-boot:run 在cloud-eureka-client目录下执行,启动cloud-eureka-client
在cloud-config-server
下新增配置cloud-eureka-server-peer2.yml
和cloud-eureka-server-peer3.yml
cloud-eureka-server-peer2.yml
server:
port: 8762
eureka:
instance:
hostname: localhost
preferIpAddress: true
client:
registerWithEureka: true
fetchRegistry: true
serviceUrl:
defaultZone: http://localhost:8761/eureka/,http://localhost:8763/eureka/
server:
waitTimeInMsWhenSyncEmpty: 0
enableSelfPreservation: false
启动:mvn spring-boot:run -Dspring-boot.run.profiles=peer2
cloud-eureka-server-peer3.yml
server:
port: 8763
eureka:
instance:
hostname: localhost
preferIpAddress: true
client:
registerWithEureka: true
fetchRegistry: true
serviceUrl:
defaultZone: http://localhost:8761/eureka/,http://localhost:8762/eureka/
server:
waitTimeInMsWhenSyncEmpty: 0
enableSelfPreservation: false
启动:mvn spring-boot:run -Dspring.profiles.active=peer3
修改一下cloud-eureka-client.yml
、cloud-eureka-server-peer1.yml
的配置文件
cloud-eureka-server-peer1.yml的修改
server:
port: 8761
eureka:
instance:
hostname: localhost
preferIpAddress: true
client:
registerWithEureka: true
fetchRegistry: false
serviceUrl:
defaultZone: http://localhost:8762/eureka/,http://localhost:8763/eureka/
server:
waitTimeInMsWhenSyncEmpty: 0
enableSelfPreservation: false
cloud-eureka-client.yml的修改
server:
port: 8081
spring:
application:
name: cloud-eureka-client1
eureka:
client:
serviceUrl:
efaultZone: http://localhost:8761/eureka/,http://localhost:8762/eureka/,http://localhost:8763/eureka/
然后重新启动cloud-config-server
,使修改的配置生效。然后使用Postman这类型工具通过POST请求
去访问http://localhost:8081/actuator/refresh、http://localhost:8761/actuator/refresh,分别刷新cloud-eureka-client
和cloud-eureka-server-peer1
。或者使用下面的命令
curl -i -X POST localhost:8081/actuator/refresh
curl -i -X POST localhost:8761/actuator/refresh
然后在浏览器或者使用curl命令去请求,之前cloud-config-server
写好的一个接口:http://localhost:8081/query/eureka-server。最后返回结果:
{“defaultZone”: “http://localhost:8761/eureka/,http://localhost:8762/eureka/,http://localhost:8763/eureka/”}
结果显示扩容成功
对于配置文件:在扩容中使用了spring-cloud-config
这个项目统一来管理所有服务的配置文件。在真实的使用中,这些配置文件可以放在像gitlab
、github
这类型的git平台上,在修改了配置之后,不需要重启配置中心。只需要通过消息总线去通知各个服务去获取最新的配置。
对于扩容的实例:在配置中心中配好了,新增的实例的配置信息之后。通过命令去启动打包好的springbot项目的jar包,列如:java -jar cloud-eureka-server --spring.profiles.active=peer2
。最后刷新正在运行的实例。
这里启动四个Eureka Server实例。配置两个zone:zone1和zone2,每一个zone都有两个Eureka Server实例,这两个实例配置在同一个的region:region-beijing上。
启动类:
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerMultiApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerMultiApplication.class, args);
}
}
配置文件:
application-zone1a.yml
server:
port: 8761
spring:
application:
name: eureka-server
eureka:
instance:
hostname: localhost
preferIpAddress: true
metadataMap.zone: zone1
client:
register-with-eureka: true
fetch-registry: true
region: region-beijing
service-url:
zone1: http://localhost:8761/eureka/,http://localhost:8762/eureka/
zone2: http://localhost:8763/eureka/,http://localhost:8764/eureka/
availability-zones:
region-beijing: zone1,zone2
server:
waitTimeInMsWhenSyncEmpty: 0
enableSelfPreservation: false
application-zone1b.yml
server:
port: 8762
spring:
application:
name: eureka-server
eureka:
instance:
hostname: localhost
preferIpAddress: true
metadataMap.zone: zone1
client:
register-with-eureka: true
fetch-registry: true
region: region-beijing
service-url:
zone1: http://localhost:8761/eureka/,http://localhost:8762/eureka/
zone2: http://localhost:8763/eureka/,http://localhost:8764/eureka/
availability-zones:
region-beijing: zone1,zone2
server:
waitTimeInMsWhenSyncEmpty: 0
enableSelfPreservation: false
application-zone2a.yml
server:
port: 8763
spring:
application:
name: eureka-server
eureka:
instance:
hostname: localhost
preferIpAddress: true
metadataMap.zone: zone2
client:
register-with-eureka: true
fetch-registry: true
region: region-beijing
service-url:
zone1: http://localhost:8761/eureka/,http://localhost:8762/eureka/
zone2: http://localhost:8763/eureka/,http://localhost:8764/eureka/
availability-zones:
region-beijing: zone1,zone2
server:
waitTimeInMsWhenSyncEmpty: 0
enableSelfPreservation: false
application-zone2b.yml
server:
port: 8764
spring:
application:
name: eureka-server
eureka:
instance:
hostname: localhost
preferIpAddress: true
metadataMap.zone: zone2
client:
register-with-eureka: true
fetch-registry: true
region: region-beijing
service-url:
zone1: http://localhost:8761/eureka/,http://localhost:8762/eureka/
zone2: http://localhost:8763/eureka/,http://localhost:8764/eureka/
availability-zones:
region-beijing: zone1,zone2
server:
waitTimeInMsWhenSyncEmpty: 0
enableSelfPreservation: false
application.yml
eureka:
server:
use-read-only-response-cache: false
response-cache-auto-expiration-in-seconds: 10
management:
endpoints:
web:
exposure:
include: '*'
在上面的四个配置可以看得出,每一个Eureka instance都配置了自己的zone:ereuka.instance.metadataMap.zone
。那么接下来启动它们看一下:随便访问一个Eureka Server
mvn spring-boot:run -Dspring-boot.run.profiles=zone1a
mvn spring-boot:run -Dspring-boot.run.profiles=zone1b
mvn spring-boot:run -Dspring-boot.run.profiles=zone2a
mvn spring-boot:run -Dspring-boot.run.profiles=zone2b
创建次模块获取Eureka Server信息。
配置文件
application-znoe1.yml
server:
port: 8081
spring:
application:
name: eureka-client
eureka:
instance:
metadataMap.zone: zone1
client:
register-with-eureka: true
fetch-registry: true
region: region-beijing
service-url:
zone1: http://localhost:8761/eureka/,http://localhost:8762/eureka/
zone2: http://localhost:8763/eureka/,http://localhost:8764/eureka/
availability-zones:
region-beijing: zone1,zone2
application-znoe2.yml
server:
port: 8082
spring:
application:
name: eureka-client
eureka:
instance:
metadataMap.zone: zone2
client:
register-with-eureka: true
fetch-registry: true
region: region-beijing
service-url:
zone1: http://localhost:8761/eureka/,http://localhost:8762/eureka/
zone2: http://localhost:8763/eureka/,http://localhost:8764/eureka/
availability-zones:
region-beijing: zone1,zone2
application.yml
management:
endpoints:
web:
exposure:
include: '*'
启动类
@EnableDiscoveryClient
@SpringBootApplication
public class EurekaClientMultiApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaClientMultiApplication.class, args);
}
}
启动zone1和zone2这两个配置:
mvn spring-boot:run -Dspring-boot.run.profiles=zone1
mvn spring-boot:run -Dspring-boot.run.profiles=zone2
创建一个zuul实例来演示Eureka使用metadataMap的zone属性时ZoneAffinity特性
代码
pom.xml
<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>cloudartifactId>
<groupId>com.msr.bettergroupId>
<version>1.0-SNAPSHOTversion>
parent>
<modelVersion>4.0.0modelVersion>
<artifactId>cloud-zuul-gatewayartifactId>
<packaging>jarpackaging>
<properties>
<maven.compiler.source>8maven.compiler.source>
<maven.compiler.target>8maven.compiler.target>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-netflix-zuulartifactId>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
plugin>
plugins>
build>
project>
启动类
@EnableDiscoveryClient
@EnableZuulProxy
@SpringBootApplication
public class ZuulApplication {
public static void main(String[] args) {
SpringApplication.run(ZuulApplication.class, args);
}
}
配置文件
application-zone1.yml
server:
port: 10001
eureka:
instance:
metadataMap.zone: zone1
client:
register-with-eureka: true
fetch-registry: true
region: region-beijing
service-url:
zone1: http://localhost:8761/eureka/,http://localhost:8762/eureka/
zone2: http://localhost:8763/eureka/,http://localhost:8764/eureka/
availability-zones:
region-beijing: zone1,zone2
application-zone2.yml
server:
port: 10002
eureka:
instance:
metadataMap.zone: zone2
client:
register-with-eureka: true
fetch-registry: true
region: region-beijing
service-url:
zone1: http://localhost:8761/eureka/,http://localhost:8762/eureka/
zone2: http://localhost:8763/eureka/,http://localhost:8764/eureka/
availability-zones:
region-beijing: zone1,zone2
application.yml
spring:
application:
name: zuul-gateway
management:
endpoints:
web:
exposure:
include: '*'
启动这两个配置的zuul实例:
mvn spring-boot:run -Dspring-boot.run.profiles=zone1
mvn spring-boot:run -Dspring-boot.run.profiles=zone2
浏览器访问:http://localhost/eureka-client/actuator/env和http://localhost:10002/eureka-client/actuator/env。分别会返回。
{
"activeProfiles": [
"zone1"
],
"propertySources": [...]
}
{
"activeProfiles": [
"zone2"
],
"propertySources": [...]
}
从结果可以看得出来,通过请求gateway的eureka-clienti/actuator/env,访问的是eureka-client实例的/actuator/env接口,处于zone1的gateway返回的activeProfiles为zone1,处于zone2的gateway返回的activeProfiles为zone2。从这个表象看gateway路由时对client的实例是ZoneAffinity的。
一下模块的maven坐标和启动类和上面基本一致。代码就不贴出来了。
配置四个Eureka Server,分四个zone。zone1、zone2属于region-beijing;zone3、zone4属于region-guangzhou。region-beijing和region-guangzhou互为remote-region
配置文件
application-zone1.yml
server:
port: 8761
spring:
application:
name: eureka-server
eureka:
server:
waitTimeInMsWhenSyncEmpty: 0
enableSelfPreservation: false
remoteRegionUrlsWithName:
region-guangzhou: http://localhost:8763/eureka/
client:
register-with-eureka: true
fetch-registry: true
region: region-beijing
service-url:
zone1: http://localhost:8761/eureka/
zone2: http://localhost:8762/eureka/
availability-zones:
region-beijing: zone1,zone2
instance:
hostname: localhost
metadataMap.zone: zone1
application-zone2.yml
server:
port: 8762
spring:
application:
name: eureka-server
eureka:
server:
waitTimeInMsWhenSyncEmpty: 0
enableSelfPreservation: false
remoteRegionUrlsWithName:
region-guangzhou: http://localhost:8763/eureka/
client:
register-with-eureka: true
fetch-registry: true
region: region-beijing
service-url:
zone1: http://localhost:8761/eureka/
zone2: http://localhost:8762/eureka/
availability-zones:
region-beijing: zone1,zone2
instance:
hostname: localhost
metadataMap.zone: zone2
application-zone3-region-guangzhou.yml
server:
port: 8763
spring:
application:
name: eureka-server
eureka:
server:
waitTimeInMsWhenSyncEmpty: 0
enableSelfPreservation: false
remoteRegionUrlsWithName:
region-beijing: http://localhost:8761/eureka/
client:
register-with-eureka: true
fetch-registry: true
region: region-guangzhou
service-url:
zone3: http://localhost:8763/eureka/
zone4: http://localhost:8764/eureka/
availability-zones:
region-guangzhou: zone3,zone4
instance:
hostname: localhost
metadataMap.zone: zone3
application-zone4-region-guangzhou.yml
server:
port: 8764
spring:
application:
name: eureka-server
eureka:
server:
waitTimeInMsWhenSyncEmpty: 0
enableSelfPreservation: false
remoteRegionUrlsWithName:
region-beijing: http://localhost:8761/eureka/
client:
register-with-eureka: true
fetch-registry: true
region: region-guangzhou
service-url:
zone3: http://localhost:8763/eureka/
zone4: http://localhost:8764/eureka/
availability-zones:
region-guangzhou: zone3,zone4
instance:
hostname: localhost
metadataMap.zone: zone4
创建配置类:RegionConfig。因为zone3和zone4是属于region-guangzhou,remote-region配置为region-beijing。由于源码里EurekaServerConfigBean的remoteRegionAppWhitelist默认为null;而getRemoteRegionAppWhitelist(String regionName)方法会直接被调用,如果不设置会导致空指针异常。
@Configuration
@AutoConfigureBefore(EurekaServerAutoConfiguration.class)
public class RegionConfig {
@Bean
@ConditionalOnMissingBean
public EurekaServerConfig eurekaServerConfig(EurekaClientConfig clientConfig) {
EurekaServerConfigBean serverConfigBean = new EurekaServerConfigBean();
if (clientConfig.shouldRegisterWithEureka()) {
serverConfigBean.setRegistrySyncRetries(6);
}
serverConfigBean.setRemoteRegionAppWhitelist(new HashMap<>());
return serverConfigBean;
}
}
启动Eureka Server:
mvn spring-boot:run -Dspring-boot.run.profiles=zone1
mvn spring-boot:run -Dspring-boot.run.profiles=zone2
mvn spring-boot:run -Dspring-boot.run.profiles=zone3-region-guangzhou
mvn spring-boot:run -Dspring-boot.run.profiles=zone4-region-guangzhou
四个client,分为四个zone,属于region-beijng和region-guangzhou这两个region。
配置文件
application.yml
management:
endpoints:
web:
exposure:
include: '*'
application-zone1.yml
server:
port: 8071
spring:
application.name: eureka-client
eureka:
client:
prefer-same-zone-eureka: true
region: region-beijing
service-url:
zone1: http://localhost:8761/eureka/
zone2: http://localhost:8762/eureka/
availability-zones:
region-beijing: zone1,zone2
instance:
metadataMap.zone: zone1
application-zone2.yml
server:
port: 8072
spring:
application.name: eureka-client
eureka:
client:
prefer-same-zone-eureka: true
region: region-beijing
service-url:
zone1: http://localhost:8761/eureka/
zone2: http://localhost:8762/eureka/
availability-zones:
region-beijing: zone1,zone2
instance:
metadataMap.zone: zone2
application-zone3.yml
server:
port: 8073
spring:
application.name: eureka-client
eureka:
client:
prefer-same-zone-eureka: true
region: region-guangzhou
service-url:
zone3: http://localhost:8763/eureka/
zone4: http://localhost:8764/eureka/
availability-zones:
region-guangzhou: zone3,zone4
instance:
metadataMap.zone: zone3
application-zone4.yml
server:
port: 8074
spring:
application.name: eureka-client
eureka:
client:
prefer-same-zone-eureka: true
region: region-guangzhou
service-url:
zone3: http://localhost:8763/eureka/
zone4: http://localhost:8764/eureka/
availability-zones:
region-guangzhou: zone3,zone4
instance:
metadataMap.zone: zone4
启动
mvn spring-boot:run -Dspring-boot.run.profiles=zone1
mvn spring-boot:run -Dspring-boot.run.profiles=zone2
mvn spring-boot:run -Dspring-boot.run.profiles=zone3
mvn spring-boot:run -Dspring-boot.run.profiles=zone4
配置文件
application.yml
spring:
application:
name: zuul-gateway
management:
endpoints:
web:
exposure:
include: '*'
application-zone1.yml
server:
port: 10001
eureka:
instance:
metadataMap.zone: zone1
client:
register-with-eureka: true
fetch-registry: true
region: region-beijing
service-url:
zone1: http://localhost:8761/eureka/
zone2: http://localhost:8762/eureka/
availability-zones:
region-beijing: zone1,zone2
application-zone3-region-guangzhou.yml
server:
port: 10002
eureka:
instance:
metadataMap.zone: zone3
client:
register-with-eureka: true
fetch-registry: true
region: region-guangzhou
service-url:
zone3: http://localhost:8763/eureka/
zone4: http://localhost:8764/eureka/
availability-zones:
region-guangzhou: zone3,zone4
启动
mvn spring-boot:run -Dspring-boot.run.profiles=zone1
mvn spring-boot:run -Dspring-boot.run.profiles=zone3-region-guangzhou
在全部启动完成之后,分别访问
http://localhost:10001/eureka-client/actuator/env
http://localhost:10002/eureka-client/actuator/env
10001端口的网关访问的是zone1的eureka-client。10002端口的网关访问的是zone3的eureka-client。现在关掉zone1和zone2的eureka-client,继续访问http://localhost:10001/eureka-client/actuator/env
,在报错几次之后,就会自动fallback切换到remote-region里的zone。实现了类似于异地多活自动转义请求的效果。
Eureka Server是有暴露了自己的REST API,如果没有安全认证,别人就可以通过这些API修改信息,造成服务异常。那就看一下Euerka Server是怎么开启使用HTTP Basic校验,以及Eureka Client是怎么鉴权的。
pom.xml.。引入spring-boot-starter-security
<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>cloudartifactId>
<groupId>com.msr.bettergroupId>
<version>1.0-SNAPSHOTversion>
parent>
<modelVersion>4.0.0modelVersion>
<artifactId>cloud-eureka-server-http-basicartifactId>
<properties>
<maven.compiler.source>8maven.compiler.source>
<maven.compiler.target>8maven.compiler.target>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-netflix-eureka-serverartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-securityartifactId>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
plugin>
plugins>
build>
project>
application.yml。很明显我们在配置文件中配置了用户名和密码,这些配置config-server这些配置中心来使用更加适合。
server:
port: 8761
spring:
security:
basic:
enabled: true
user:
name: admin
password: Xk38CNHigBP5jK75
eureka:
instance:
hostname: localhost
client:
registerWithEureka: false
fetchRegistry: false
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
server:
waitTimeInMsWhenSyncEmpty: 0
enableSelfPreservation: false
禁用spring-boot-starter-security
默认开启的csrf功能
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
super.configure(http);
http.csrf().disable();
}
}
在浏览器访问http://localhost:8761或http://localhost:8761/eureka/apps,这些eureka的REST API时,是要求登录的。使用在配置文件中配置好的username和password登录,才可以成功访问。
因为Eureka Server开启了HTTP Basic认证,所以Eureka Client需要配置相对应的用户名和密码。同样这里在配置文件中配置,当然配置config-server使用更好。
配置文件
application.yml
server:
port: 8081
spring:
application:
name: client1
eureka:
client:
security:
basic:
user: admin
password: Xk38CNHigBP5jK75
serviceUrl:
defaultZone: http://${eureka.client.security.basic.user}:${eureka.client.security.basic.password}@localhost:8761/eureka/
#defaultZone: http://localhost:8761/eureka/
如果不使用用户名密码的话启动会报错
Request execution failure with status code 401; retrying on another server if available
浏览器登录之后,访问http://localhost:8761/eureka/apps。可以看到注册进去的eureka-client实例的信息,说明注册成功。
虽然开启了HTTP Basic但是基于base64编码很容易抓包被破解,如果直接暴露在公网上很危险。因此Eureka Server和Client开启https可以有效的解决这类问题。
1、生成证书
使用jdk的keytool工具
keytool -genkeypair -alias server -storetype PKCS12 -keyalg RSA -keysize 2048 -keystore eureka.server -validity 365
上面的命令生成server端的证书,密码是server
keytool -genkeypair -alias client -storetype PKCS12 -keyalg RSA -keysize 2048 -keystore eureka.client -validity 365
上面的命令生成client端的证书,密码是client
2、导出证书
#导出server
keytool -export -alias server -file server.crt --keystore eureka.server
#导出client
keytool -export -alias client -file client.crt --keystore eureka.client
3、使Client端信任Server
# 提示输入密码时,输入client的密码:Client
keytool -import -alias server -file server.crt --keystore eureka.client
4、使Server信任Client
# 同理
keytool -import -alias client -file client.crt --keystore eureka.server
把生成的server.crt、client.crt和eureka.server复制到resources文件夹下。
配置文件
application.yml
server:
port: 8761
ssl:
enabled: true
key-store: classpath:eureka.server
key-store-password: server
key-store-type: PKCS12
key-alias: server
eureka:
instance:
hostname: localhost
securePort: ${server.port}
securePortEnabled: true
nonSecurePortEnabled: false
homePageUrl: https://${eureka.instance.hostname}:${server.port}/
statusPageUrl: https://${eureka.instance.hostname}:${server.port}/
client:
registerWithEureka: false
fetchRegistry: false
serviceUrl:
defaultZone: https://${eureka.instance.hostname}:${server.port}/eureka/
server:
waitTimeInMsWhenSyncEmpty: 0
enableSelfPreservation: false
启动之后,分别在浏览器访问http://localhost:8761和https://localhost:8761。使用http访问的时候会出现
Bad Request
This combination of host and port requires TLS.
使用https访问时正确的访问到了eureka。说明https启用成功
把生成的server.crt、client.crt和eureka.client复制到resources文件夹下。
pom.xml,添加httpclient
<dependency>
<groupId>org.apache.httpcomponentsgroupId>
<artifactId>httpclientartifactId>
<version>4.5.5version>
dependency>
配置文件
application.yml
server:
port: 8081
spring:
application:
name: eureka-client1
eureka:
client:
securePortEnabled: true
ssl:
key-store: eureka.client
key-store-password: client
serviceUrl:
defaultZone: https://localhost:8761/eureka/
在上面的配置文件中,没有配置整个应用开启https,因为这里仅仅是Eureka Server开启了https。通过自定义eureka.client.key-store以及eureka.client.ssl.key.store-password,来执行Eureka Client访问Eureka Server的sslContext配置,所以需要配置一下DiscoveryClient。
最后,启动就可以在Eureka Server上看到注册的Eureka Client了。
在微服务架构中注册中心是必不可少的基础组件,Eureka Server是Spring Cloud技术栈中的基础组件。Spring Cloud中国社区开源了一个对Eureka Server节点进行监控、服务动态启停的管控平台:Eureka Admin。Github上的地址:https://github.com/SpringCloud/eureka-admin。在Github上看见这个仓库最新的更新:Updated on 15 Aug 2019,现在国内用的最多的是Spring Cloud Alibaba Nacos,毕竟Eureka2.x版本已经停止开源了,但是Eureka1.x版本还在持续的维护中。可能Alibaba就是看到了这一点强势开源了Spring Cloud Alibaba,其中的Nacos确实解决了一些“痛点”。
现在来看一下,Eureka Admin!
构建步骤:
1、正常启动您的Eureka以及服务体系
2、由于目前尚未提交中央仓库,需下载源码构建地址:https://github.com/SpringCloud/eureka-admin,打开eureka-admin-starter-server项目配置文件,设置您的eureka注册中心地址以及eureka-admin管控平台端口即可,如下
server:
port: 8080
eureka:
server:
eviction-interval-timer-in-ms: 30000
client:
register-with-eureka: false
fetch-registry: true
filterOnlyUpInstances: false
serviceUrl:
defaultZone: http://localhost:8761/eureka/,http://localhost:8762/eureka/,http://localhost:8763/eureka/,http://localhost:8764/eureka/
logging:
level:
com:
itopener: DEBUG
org:
springframework: INFO
3、启动EurekaAdminServer。浏览器访问:http://localhost:8080
4、结果