面试题答案
一键面试使用Eureka实现服务发现与负载均衡
- Eureka Server配置:
- 创建一个Spring Boot项目作为Eureka Server。
- 在
pom.xml
中添加Eureka Server依赖:
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency>
- 在
application.yml
中配置Eureka Server相关属性,例如:
server: port: 8761 eureka: instance: hostname: localhost client: register - with - eureka: false fetch - registry: false
- 在主应用类上添加
@EnableEurekaServer
注解启用Eureka Server功能。
- 微服务客户端配置:
- 在每个微服务的
pom.xml
中添加Eureka Client依赖:
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency>
- 在
application.yml
中配置Eureka客户端属性,例如:
eureka: client: service - url: defaultZone: http://localhost:8761/eureka/
- 在主应用类上添加
@EnableEurekaClient
注解启用Eureka客户端功能。
- 在每个微服务的
- 负载均衡:
- Spring Cloud Ribbon会自动与Eureka集成,实现客户端负载均衡。在微服务调用其他服务时,使用
RestTemplate
并添加@LoadBalanced
注解,例如:
@Bean @LoadBalanced public RestTemplate restTemplate() { return new RestTemplate(); }
- 然后在代码中通过服务名调用其他服务,例如
restTemplate.getForObject("http://service - name/path", responseType);
- Spring Cloud Ribbon会自动与Eureka集成,实现客户端负载均衡。在微服务调用其他服务时,使用
使用Consul实现服务发现与负载均衡
- Consul Server配置:
- 下载并启动Consul Server。例如,在命令行中执行
consul agent -server -bootstrap -ui -bind=127.0.0.1 -data -dir=/tmp/consul
启动一个开发模式的Consul Server并启用Web UI。
- 下载并启动Consul Server。例如,在命令行中执行
- 微服务客户端配置:
- 在每个微服务的
pom.xml
中添加Spring Cloud Consul相关依赖:
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud - starter - consul - discovery</artifactId> </dependency>
- 在
application.yml
中配置Consul客户端属性,例如:
spring: cloud: consul: host: localhost port: 8500 discovery: service - name: your - service - name
- 在主应用类上添加
@EnableDiscoveryClient
注解启用服务发现功能。
- 在每个微服务的
- 负载均衡:
- Spring Cloud Ribbon同样可以与Consul集成实现客户端负载均衡,配置方式与Eureka类似,使用
RestTemplate
并添加@LoadBalanced
注解。
- Spring Cloud Ribbon同样可以与Consul集成实现客户端负载均衡,配置方式与Eureka类似,使用
使用Nginx作为负载均衡器
- Nginx配置:
- 安装Nginx。
- 配置Nginx的
nginx.conf
或在conf.d
目录下创建新的配置文件(例如microservices.conf
)。 - 假设微服务在Docker容器中运行,通过Docker网络可以访问,配置示例如下:
upstream microservice - group { server 172.17.0.2:8080; # 微服务1的Docker容器IP和端口 server 172.17.0.3:8080; # 微服务2的Docker容器IP和端口 # 可以继续添加更多微服务实例 } server { listen 80; server_name your - domain.com; location / { proxy_pass http://microservice - group; proxy_set_header Host $host; proxy_set_header X - Real - IP $remote_addr; proxy_set_header X - Forwarded - For $proxy_add_x_forwarded_for; proxy_set_header X - Forwarded - Proto $scheme; } }
- 重启Nginx使配置生效。
- 结合服务发现:
- 可以使用脚本或工具动态更新Nginx的上游服务器列表,结合Consul或Eureka的服务发现信息。例如,使用Consul - Template工具,它可以根据Consul中的服务实例动态生成Nginx配置文件并重新加载Nginx。具体步骤如下:
- 安装Consul - Template。
- 创建一个Consul - Template配置文件(例如
consul - template.hcl
):
template { source = "/path/to/nginx.template" destination = "/etc/nginx/conf.d/microservices.conf" command = "nginx -s reload" }
- 创建
nginx.template
模板文件,例如:
upstream microservice - group { {{range service "your - service - name"}} server {{.Address}}:{{.Port}}; {{end}} } server { listen 80; server_name your - domain.com; location / { proxy_pass http://microservice - group; proxy_set_header Host $host; proxy_set_header X - Real - IP $remote_addr; proxy_set_header X - Forwarded - For $proxy_add_x_forwarded_for; proxy_set_header X - Forwarded - Proto $scheme; } }
- 运行Consul - Template,它会根据Consul中的服务实例动态更新Nginx配置。
- 可以使用脚本或工具动态更新Nginx的上游服务器列表,结合Consul或Eureka的服务发现信息。例如,使用Consul - Template工具,它可以根据Consul中的服务实例动态生成Nginx配置文件并重新加载Nginx。具体步骤如下: