ribbon small chestnuts in springcloud

Write in front

This article continues the analysis on the basis of this blog post . Here we need to add a new maven module my-service-ribbonto my-enreka-serverpull the service registration information, and then call myeureka-clientthe interface in a load balancing manner . The source code is here .

1: Createmy-service-ribbon

1.1: Create a project

new-> module,Select Spring Inilializrand java8, then next, enter gav and other information, after the creation is completed, the following figure:
Insert picture description here

1.2: Set parent

<parent>
  <groupId>dongshi.daddy</groupId>
  <artifactId>my-eureka</artifactId>
  <version>1.0-SNAPSHOT</version>
  <relativePath/> <!-- lookup parent from repository -->
</parent>

1.3: Introduce dependencies

<dependencies>
  <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  </dependency>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
  </dependency>
  <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
  </dependency>
</dependencies>

1.4: Complete pom

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>dongshi.daddy</groupId>
  <artifactId>my-service-ribbon</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>my-service-ribbon</name>
  <description>Demo project for Spring Boot</description>

  <parent>
    <groupId>dongshi.daddy</groupId>
    <artifactId>my-eureka</artifactId>
    <version>1.0-SNAPSHOT</version>
    <relativePath/> <!-- lookup parent from repository -->
  </parent>

  <properties>
    <java.version>1.8</java.version>
  </properties>

  <dependencies>
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
    </plugins>
  </build>

</project>

1.5: Configuration file

server:
  port: 8764

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/

spring:
  application:
    name: dongshidaddy-first-ribbon

In addition to the port number, use eureka.client.serviceUrl.defaultZonethe address of the eureka server to register yourself, and spring.application.nameset your own name. The name will be displayed in the eureka registration information.

1.6: Enable related solutions

@EnableEurekaClient
@EnableDiscoveryClient

Annotate to @EnableEurekaClientenable yourself as a client of eureka and register with the server of eureka. Note @EnableDiscoveryClientEnable the finder client to read all eureka client registration information from the eureka server. Then register RestTemplatethe bean to the IOC container, and @LoadBanlancedgive it the ability to use ribbon for load balancing through annotations. The source code is as follows:

@Bean
@LoadBalanced
RestTemplate restTemplate() {
    
    
    return new RestTemplate();
}

The complete code is as follows:

@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
public class MyServiceRibbonApplication {
    
    

    public static void main(String[] args) {
    
    
        SpringApplication.run(MyServiceRibbonApplication.class, args);
    }

    @Bean
    @LoadBalanced
    RestTemplate restTemplate() {
    
    
        return new RestTemplate();
    }

}

1.7: Configure service

@Service
public class HelloService {
    
    

    @Autowired
    RestTemplate restTemplate;

    public String hiService(String name) {
    
    
        return restTemplate.getForObject("http://DONGSHIDADDY-FIRST-EUREKA-CLIENT/hi?name="+name,String.class);
    }
}

Note DONGSHIDADDY-FIRST-EUREKA-CLIENTthat myeureka-clientthe service name of my module should be replaced with your own! ! ! .

1.8: Configure the controller

@RestController
public class HelloControler {
    
    

    @Autowired
    HelloService helloService;

    @GetMapping(value = "/hi")
    public String hi(@RequestParam String name) {
    
    
        return helloService.hiService( name );
    }
}

2: Test

2.1: Startmy-enreka-server

This service is the server of eureka, used as the registration center of the service, and the port number is 8761.

2.2: Start the myeureka-clientfirst instance

Modify the configuration file to set the port number to 8762, and then start.

2.3: Start myeureka-clientthe second instance

Modify the port number to 8763. Careful not to directly start, you need to Edit Configurationsadd a program under springboot, and then set the name and the main function, as shown below:
Insert picture description here
After a successful start to view http://localhost:8761/you can see myeureka-clientan example of two registered, port number, respectively 8762, 8763.
Insert picture description here

2.4: Startmy-service-ribbon

After startup, you can see the registered information on the eureka server, as shown below:
Insert picture description here

2.5: Access interface

Repeated visits as follows, myeureka-clientload calls can be made in two instances of:

C:\Users\cj>curl http://localhost:8764/hi?name=dongshidaddy
hi dongshidaddy ,i am from port:8763
C:\Users\cj>curl http://localhost:8764/hi?name=dongshidaddy
hi dongshidaddy ,i am from port:8762
C:\Users\cj>curl http://localhost:8764/hi?name=dongshidaddy
hi dongshidaddy ,i am from port:8763
C:\Users\cj>curl http://localhost:8764/hi?name=dongshidaddy
hi dongshidaddy ,i am from port:8762

3: Project structure

At this point the system has 1a eureka the server, port number 8761, 2a eureka client, respectively myeureka-client, my-service-ribbonwhich myeureka-client starts 2个实例, port numbers are 8762and 8763, my-service-ribbon myeureka-client calls the service provided, final The structure is as follows:
Insert picture description here
pay attention to the ribbon of the diamond-shaped component in the figure, because the internal load actually uses the ribbon, so here is replaced by the ribbon to indicate it, and it should be my-service-riiboninternal, here is a summary of the explanation.

Finally: get out of the way, I want to drink Luckin

Insert picture description here

Guess you like

Origin blog.csdn.net/wang0907/article/details/109241299