1. Environmental preparation
We continue to use the Eureka cluster environment in "Eureka Use of SpringCloud".
1. First of all, I have spring-cloud-parent pom project here
2. For the spring-cloud-eureka-server Eureka Server sub-project, we start with 90 for this port
Our cluster here is composed of two service instances, port 9090 and port 9091
3.spring-cloud-user-service-consumer user service (that is, our service consumer). Here we use port 80 starting
4. spring-cloud-order-service-provider order provision service (service provider) Here we use the port number 70
Our order service provider also consists of two instances, 7070 and 7071
2. spring-cloud-eureka-server
Our spring-cloud-parent parent project and Eureka Server service spring-cloud-eureka-server do not need to be moved, and then start Eureka Server on port 9090 and port 9091 respectively.
3. Order service (service provider)
3.1 application.yml
Here we use the springboot profiles profile feature to divide the order provider service spring-cloud-order-service-provider into different ports
spring:
application:
name: spring-cloud-order-service-provider
---
spring:
profiles: p1
eureka:
client:
service-url:
defaultZone: http://EurekaServerA:9090/eureka,http://EurekaServerB:9091/eureka
fetch-registry: true
register-with-eureka: true
instance:
prefer-ip-address: true # 使用ip注册
#自定义实例显示格式,添加版本号
instance-id: ${spring.cloud.client.ip-address}:${spring.application.name}:${server.port}:@project.version@
server:
port: 7070
---
spring:
profiles: p2
eureka:
client:
service-url:
defaultZone: http://EurekaServerA:9090/eureka,http://EurekaServerB:9091/eureka
fetch-registry: true
register-with-eureka: true
instance:
prefer-ip-address: true # 使用ip注册
#自定义实例显示格式,添加版本号
instance-id: ${spring.cloud.client.ip-address}:${spring.application.name}:${server.port}:@project.version@
server:
port: 7071
And configure idea to start
3.2 controller
Modify the controller to return the port of the current service, which will help us observe
@RestController
@RequestMapping("/order/data")
public class OrderStatisticServiceController {
@Value("${server.port}")
private Integer port;
/**
* 根据用户id获取今日完单数
* @param id 用户ID
* @return 完单数
*/
@GetMapping("/getTodayFinishOrderNum/{id}")
public Integer getTodayFinishOrderNum(@PathVariable("id") Integer id){
return port;
}
}
3.3 Start
Start the order service provider 7070 and 7071 services respectively, we can see that the two service providers are registered on Eureka Server.
4. User service (service caller)
Order caller service: spring-cloud-user-service-consumer:8080
4.1 RestTemplateConfiguration
We need to add the annotation @LoadBalanced to the method of injecting RestTemplate in the RestTemplate configuration class
@Configuration
public class RestTemplateConfiguration {
@Bean
@LoadBalanced
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
}
That's ok, we don't need to add Ribbon dependencies here, because the Eureka Client package helps us introduce Ribbon-related dependencies.
4.2 controller
Here we only need to write the name of the service to be called in the call url, and then Ribbon will help us find the appropriate service call from the service list
@RestController
@RequestMapping("/user/data")
public class UserCenterController {
@Autowired
private RestTemplate restTemplate;
@GetMapping("/getTodayStatistic/{id}")
public Integer getTodayStatistic(@PathVariable("id") Integer id){
String url ="http://spring-cloud-order-service-provider/order/data/getTodayFinishOrderNum/"+id;
return restTemplate.getForObject(url, Integer.class);
}
}
4.3 Start test
We called twice and returned 7071 and 7070 respectively, indicating that our Ribbon is working.
4.4 Adjust load balancing strategy
Our default load balancing strategy is: ZoneAvoidanceRule: zone trade-off strategy. Then we can use other load balancing strategies through configuration, for example, we use random strategies:
Placed in application.yml
This is for the service spring-cloud-order-service-provider.
spring-cloud-order-service-provider:
ribbon:
NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule
Source: CSDN blogger "$ code out of the future"
Original link: https://blog.csdn.net/yuanshangshenghuo/java/article/details/106975354