1. Overview Spring provides a basic framework for a distributed environment that can be used to implement a service-based architecture. Using these services basically requires adding pom dependencies, adding configuration, and adding annotations to the code.
2. Specific services
1. Service registration and discovery
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
@EnableEurekaServer
eureka:
instance:
hostname: peer1
client:
serviceUrl:
defaultZone: http://peer2:8762/eureka/ # Backup each other
2. Registration of specific services
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
@EnableDiscoveryClient
@Autowired
private DiscoveryClient discoveryClient;
eureka:
client:
serviceUrl:
defaultZone: http://10.88.102.203:8761/eureka/
instance:
preferIpAddress: true
3. Consumption service mode—ribbon
Ribbon can realize the logic of finding the same Zone and less load eureka server, obtaining the service list through the uereka server, client load balancing, timeout retry and other logic.
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
}
// find service by url
restTemplate.getForObject("http://xxx/" + id, User.class);
eureka:
client:
serviceUrl:
defaultZone: http://10.88.102.203:8761/eureka/
instance:
preferIpAddress: true
4. Consumption service method 2 Feign
Integrate ribbon, simplify code, suitable for calling many services at one time
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
@EnableFeignClients
@EnableDiscoveryClient
@FeignClient(name = "microservice-provider-user")
public interface UserFeignClient {
@RequestMapping("/{id}")
// find service by function name
public User findByIdFeign(@RequestParam("id") Long id);
}
ribbon:
eureka:
enabled: true
5. Scope of Hystrix fuse isolation faults
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
@EnableCircuitBreaker
1》ribbon:
// Note that the function signature of fallback should be consistent with the calling function itself
@HystrixCommand(fallbackMethod = "fallback")
2"forein: Just pass the second parameter (overloading of the function) directly
@FeignClient(name = "microservice-provider-user", fallback = HystrixClientFallback.class)
6. Hystrix Dashboard for monitoring fuses
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
@EnableHystrixDashboard
7. Collect monitoring data of multiple services at the same time Turbine
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-turbine</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-netflix-turbine</artifactId>
</dependency>
@EnableTurbine
security.basic.enabled: false
turbine:
aggregator:
clusterConfig: default
appConfig: serviceid1, serviceid2
clusterNameExpression: new String("default")
8. Unified configuration
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
@EnableConfigServer
cloud:
config:
server:
git:
uri: https://github.com/xxx
search-paths: dir
username:
password:
User:
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
@RefreshScope
@Value("${profile}")
private String profile;
bootstrap.yml (data loading order: bootstrap config application, if it is in application, placeholder replacement cannot be achieved)
cloud:
config:
uri: http://config-server:8040/
profile: dev
label: master
User refresh: curl -X POST http://localhost:8041/refresh
9. Gateway Zuul provides dynamic routing, monitoring, fallback, security and other functions.
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zuul</artifactId>
@EnableZuulProxy
zuul:
ignored-services: xxxx
routes:
movie:
path: /movie/**
service-id: yyyy
3. Common commands
java -jar target/**.jar --spring.profiles.active=peer1 > peer1.log 2>&1 &