1. Environment preparation

We continue to use the Eureka cluster environment in SpringCloud’s Eureka usage guide.

1. Firstly, I have spring-Cloud-parent POM project

2. Spring-cloud-eureka-server Eureka Server sub-project

Our cluster consists of two service instances, namely ports 9090 and 9091

We use a port that starts with 80

Spring-cloud-order-service-provider (spring-cloud-order-service-provider) spring-cloud-order-service-provider (spring-cloud-order-service-provider

Our order service is also composed of two instances, respectively 7070 and 7071

 

2. spring-cloud-eureka-server

The spring-cloud-Eureka Server does not need to be moved, and then starts the Eureka Server on port 9090 and port 9091 respectively.

 

3. Order Service (Service provider)

3.1 application. Yml

Here we use the SpringBoot Profiles 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 ${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: Example: true instance: prefer-ip: true ${spring.cloud.client.ip-address}:${spring.application.name}:${server.port}:@project.version@ server: port: 7071Copy the code

And configure IDEA startup

 

 

3.2 the controller

Modify the controller so that it returns the port it is currently serving

@RestController @RequestMapping("/order/data") public class OrderStatisticServiceController { @Value("${server.port}") private Integer port; @param ID User ID * @return User ID */ @getMapping ("/getTodayFinishOrderNum/{ID}") public Integer getTodayFinishOrderNum(@PathVariable("id") Integer id){ return port; }}Copy the code

3.3 start

Starting the order service provider 7070 and 7071 services respectively, we can see that both service providers are registered with Eureka Server.

 

4. User service (service caller)

Order caller service: Spring-Cloud-user-service-consumer :8080

4.1 RestTemplateConfiguration

We need to add the @loadBalanced annotation to the RestTemplate configuration class above the methods that inject the RestTemplate

@Configuration public class RestTemplateConfiguration { @Bean @LoadBalanced public RestTemplate getRestTemplate(){ return new RestTemplate(); }}Copy the code

This is ok. We do not need to add Ribbon dependencies, because the Eureka Client package introduces Ribbon dependencies.

 

4.2 the controller

Here we simply write the name of the service to be invoked in the call URL. The Ribbon will then help us find the appropriate service to invoke 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); }}Copy the code

4.3 Starting a Test

 

We call it twice, returning 7071 and 7070, respectively, to show that our Ribbon works.

 

 

4.4 Adjusting load Balancing Policies

The default load balancing policy on our side is: ZoneAvoidanceRule: zone tradeoff policy. We can then configure to use other load balancing policies, such as we use random policies:

In the application. The yml configuration

This is for spring-cloud-order-service-provider.

spring-cloud-order-service-provider:
  ribbon:
    NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule
Copy the code

Source: CSDN blogger “$Code out the Future”

The original link: blog.csdn.net/yuanshangsh…