springcloud of feign small chestnuts

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:
Insert picture description here
there is currently a 8671server instance of eureka with port number 8672, 8673two instances of the same application of eureka client with port number , and eureka client instance 8674of ribbon with port number . Here we add another service my-service-feigncalled myeureka-clientby the module based on feign .

1: Createmy-service-feign

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-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 @EnableFeignClientsis 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 sayHiFromClientOneconfigures 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-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-feign

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:
Insert picture description here

3: Project structure

At this point the system has 1a eureka the server, port number 8761, 3a eureka clients, namely myeureka-client, my-service-ribbon, my-service-feignwhere myeureka-client starts 2个实例, port numbers are 8762and 8763, my-service-ribbon and my-service-feign call myeureka -Service provided by the client. The final structure is as follows:
Insert picture description here
the 蓝色边框framed part is the part added in this article.

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/109245666