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-ribbon
to my-enreka-server
pull the service registration information, and then call myeureka-client
the interface in a load balancing manner . The source code is here .
1: Createmy-service-ribbon
1.1: Create a project
new
-> module
,Select Spring Inilializr
and java8, then next, enter gav and other information, after the creation is completed, the following figure:
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.defaultZone
the address of the eureka server to register yourself, and spring.application.name
set your own name. The name will be displayed in the eureka registration information.
1.6: Enable related solutions
@EnableEurekaClient
@EnableDiscoveryClient
Annotate to @EnableEurekaClient
enable yourself as a client of eureka and register with the server of eureka. Note @EnableDiscoveryClient
Enable the finder client to read all eureka client registration information from the eureka server. Then register RestTemplate
the bean to the IOC container, and @LoadBanlanced
give 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-CLIENT
that myeureka-client
the 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-client
first instance
Modify the configuration file to set the port number to 8762
, and then start.
2.3: Start myeureka-client
the second instance
Modify the port number to 8763
. Careful not to directly start, you need to Edit Configurations
add a program under springboot, and then set the name and the main function, as shown below:
After a successful start to view http://localhost:8761/
you can see myeureka-client
an example of two registered, port number, respectively 8762
, 8763
.
2.4: Startmy-service-ribbon
After startup, you can see the registered information on the eureka server, as shown below:
2.5: Access interface
Repeated visits as follows, myeureka-client
load 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 1
a eureka the server, port number 8761
, 2
a eureka client, respectively myeureka-client
, my-service-ribbon
which myeureka-client starts 2个实例
, port numbers are 8762
and 8763
, my-service-ribbon myeureka-client calls the service provided, final The structure is as follows:
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-riibon
internal, here is a summary of the explanation.