随着云计算的发展和企业业务的不断扩张,微服务架构已经成为了一个非常流行的系统架构。其中,spring boot和spring cloud是目前最为常用的微服务框架。spring cloud提供了丰富的组件来支持微服务的开发和管理,包括服务注册与发现、路由、负载均衡、配置管理、断路器等。
在本篇文章中,我们将构建一个分布式、安全的Spring Cloud微服务飞行系统作为案例,以此来展示Spring Cloud的强大功能。
首先,我们需要进行服务的注册与发现。Spring Cloud提供了Eureka来帮助我们实现服务的注册和发现。我们将通过Eureka Server来完成服务的注册与发现。
创建Eureka Server应用程序:
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}在application.properties中配置:
server.port=8761 eureka.client.register-with-eureka=false eureka.client.fetch-registry=false
在服务提供者和服务消费者应用程序中,我们需要将其注册到Eureka Server中。
在服务提供者的application.properties中配置:
spring.application.name=flight-service-provider server.port=8080 eureka.client.service-url.defaultZone=http://localhost:8761/eureka/
在服务消费者的application.properties中配置:
spring.application.name=flight-service-consumer server.port=8081 eureka.client.service-url.defaultZone=http://localhost:8761/eureka/
服务提供者通过Spring MVC创建RESTful接口:
@RestController
@RequestMapping("/flights")
public class FlightController {
@GetMapping("/{flightId}")
public ResponseEntity<Flight> getFlight(@PathVariable Integer flightId) {
Flight flight = new Flight(flightId, "Shanghai", "Beijing", new Date());
return new ResponseEntity<>(flight, HttpStatus.OK);
}
}服务消费者通过Spring RestTemplate来调用服务:
@Service
public class FlightService {
@Autowired
private RestTemplate restTemplate;
@Value("${service.provider.url}")
private String serviceProviderUrl;
public Flight getFlight(Integer flightId) {
return restTemplate.getForObject(serviceProviderUrl + "/flights/{flightId}", Flight.class, flightId);
}
}其中,service.provider.url在应用程序的application.properties中进行配置。
在实际的应用中,服务提供者很可能会部署在多个实例上,这时我们需要进行负载均衡以提高系统的性能和可用性。Spring Cloud提供了Ribbon来支持负载均衡。
在服务消费者的application.properties中进行配置:
service.provider.url=http://flight-service-provider/ spring.cloud.loadbalancer.ribbon.enabled=true
在FlightService中使用负载均衡的RestTemplate:
@Service
public class FlightService {
@Autowired
@LoadBalanced
private RestTemplate restTemplate;
@Value("${service.provider.name}")
private String serviceProviderName;
public Flight getFlight(Integer flightId) {
return restTemplate.getForObject("http://" + serviceProviderName + "/flights/{flightId}", Flight.class, flightId);
}
}其中,service.provider.name在应用程序的application.properties中进行配置。
Spring Cloud提供了Config来方便地管理应用程序的配置。我们可以将应用程序的配置存储在Git仓库中,并通过Config Server来进行分发。
创建Config Server应用程序:
@SpringBootApplication
@EnableConfigServer
public class ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
}
}在application.properties中配置:
server.port=8888 spring.cloud.config.server.git.uri=https://github.com/xxx/xxx.git spring.cloud.config.server.git.search-paths=config-repo
在服务提供者和服务消费者中,我们可以通过Config Server来获取应用程序的配置。
在服务提供者的application.yml中进行配置:
spring:
application:
name: flight-service-provider
cloud:
config:
uri: http://localhost:8888
label: master
profile: dev在服务消费者的application.yml中进行配置:
spring:
application:
name: flight-service-consumer
cloud:
config:
uri: http://localhost:8888
label: master
profile: dev在微服务架构中,由于服务之间的依赖关系非常复杂,一些服务宕机或者出现问题可能会导致整个系统的崩溃。为了应对这种情况,我们可以使用断路器来进行服务降级处理。
Spring Cloud提供了Hystrix来支持断路器功能。
在服务消费者的application.yml中进行配置:
spring:
application:
name: flight-service-consumer
cloud:
config:
uri: http://localhost:8888
label: master
profile: dev
loadbalancer:
ribbon:
enabled: true
circuitbreaker:
enabled: true
resilience4j:
enabled: false
circuitBreaker:
backend: flight-service-provider
failureRateThreshold: 50在FlightController中添加@HystrixCommand注解:
@RestController
@RequestMapping("/flights")
public class FlightController {
@Autowired
private FlightService flightService;
@GetMapping("/{flightId}")
@HystrixCommand(fallbackMethod = "defaultGetFlight")
public ResponseEntity<Flight> getFlight(@PathVariable Integer flightId) {
Flight flight = flightService.getFlight(flightId);
if (flight != null) {
return new ResponseEntity<>(flight, HttpStatus.OK);
} else {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
}
public ResponseEntity<Flight> defaultGetFlight(Integer flightId) {
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
}
}其中,defaultGetFlight为降级函数。
在分布式系统中,安全性问题非常重要。Spring Cloud提供了Security来支持安全性管理。
在服务提供者和服务消费者应用程序的pom.xml中添加:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-security</artifactId>
</dependency>在服务提供者和服务消费者应用程序的application.yml中进行配置:
security:
basic:
enabled: true
spring:
security:
user:
name: user
password: password其中,name和password分别为用户的名称和密码。需要注意的是,在实际的应用中要使用更加安全的方式进行用户认证和授权管理。
在FlightController的类级别上添加@PreAuthorize注解:
@RestController
@RequestMapping("/flights")
@PreAuthorize("hasRole('ROLE_ADMIN')")
public class FlightController {
@Autowired
private FlightService flightService;
@GetMapping("/{flightId}")
public ResponseEntity<Flight> getFlight(@PathVariable Integer flightId) {
Flight flight = flightService.getFlight(flightId);
if (flight != null) {
return new ResponseEntity<>(flight, HttpStatus.OK);
} else {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
}
}其中,@PreAuthorize注解用于对FlightController进行安全性验证。在实际的应用中,可以对每个方法进行不同的安全性验证。
这样,我们就完成了一个分布式、安全的Spring Cloud微服务飞行系统的构建。通过本文的案例,我们可以看到Spring Cloud提供了丰富的组件帮助我们构建微服务。同时,我们也需要注意到微服务架构带来的一些挑战,例如服务注册与发现、服务间通信、负载均衡、配置管理、断路器、安全性等问题。在实际的应用中,我们需要结合具体的场景进行技术选型和配置。
以上就是构建分布式、安全的Spring Cloud微服务飞行系统的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号