Write in front
The source code is here . This example is explained on the basis of this blog . Paste the picture below to review the current architecture:
there is currently a 8671
server instance of eureka with port number 8672
, 8673
two instances of the same application of eureka client with port number , and eureka client instance 8674
of ribbon with port number . Here we add another service my-service-feign
called myeureka-client
by the module based on feign .
1: Createmy-service-feign
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-openfeign</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-feign</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>my-service-feign</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-openfeign</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: 8765
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
spring:
application:
name: dongshidaddy-first-feign-client
1.6: Enable related solutions
@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
@EnableFeignClients
Here @EnableFeignClients
is to enable the feign client.
The complete code is as follows:
@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
@EnableFeignClients
public class MyServiceFeignApplication {
public static void main(String[] args) {
SpringApplication.run(MyServiceFeignApplication.class, args);
}
}
1.7: Configure feign-based service
@FeignClient(value = "DONGSHIDADDY-FIRST-EUREKA-CLIENT")
public interface SchedualServiceHi {
@RequestMapping(value = "/hi",method = RequestMethod.GET)
String sayHiFromClientOne(@RequestParam(value = "name") String name);
}
Among them @FeignClient(value = "DONGSHIDADDY-FIRST-EUREKA-CLIENT")
is to specify the name of the service to be called DONGSHIDADDY-FIRST-EUREKA-CLIENT
. The method sayHiFromClientOne
configures the Mapping information of the called interface through the rest style interface .
1.8: Configure the controller
@RestController
public class HiController {
@Autowired
SchedualServiceHi schedualServiceHi;
@GetMapping(value = "/hi")
public String sayHi(@RequestParam String name) {
return schedualServiceHi.sayHiFromClientOne( 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-feign
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:
3: Project structure
At this point the system has 1
a eureka the server, port number 8761
, 3
a eureka clients, namely myeureka-client
, my-service-ribbon
, my-service-feign
where myeureka-client starts 2个实例
, port numbers are 8762
and 8763
, my-service-ribbon and my-service-feign call myeureka -Service provided by the client. The final structure is as follows:
the 蓝色边框
framed part is the part added in this article.