[Spring Cloud Series] 3. Service Consumer (Feign) (Hoxton.M3 version)

[Spring Cloud Series] 3. Service Consumer (Feign) (Hoxton.M3 version)

This is the third day I participated in the more text challenge. For details of the event, please view: more text challenge

surroundings:

IDEA JDK1 .8 Spring Cloud Hoxton.M3 The Boot the Spring 2.2 .0 copy the code

1. Introduction to Feign

Feign is a declarative Web Service client, its purpose is to make Web Service calls easier. Feign provides a template for HTTP requests. By writing a simple interface and inserting annotations, you can define the parameters, format, address and other information of the HTTP request, while Feign will completely proxy the HTTP request. We only need to call it like a method. The service request and related processing can be completed. Feign integrates Ribbon and Hystrix.

In summary, Feign has the following characteristics:

  1. Pluggable annotation support, including Feign annotations and JAX-RS annotations;
  2. Support pluggable HTTP encoder and decoder;
  3. Support Hystrix and its Fallback;
  4. Support Ribbon load balancing;
  5. Supports compression of HTTP requests and responses.

Some attributes of FeignClient annotations

  • value : The default value is an empty string, which is mainly used to call the service name, which is the same as the name attribute
  • serviceId : The default value is an empty string, the main function is the service id, and the function is the same as the name attribute
  • name : The default value is an empty string, the main function is to call the service name, which is the same as the value attribute
  • url : The default value is an empty string, mainly used for the full path address or hostname, Http or Https is optional
  • decode404 : The default value is false, which is mainly used to configure whether FeignExceptions should be thrown when the response status code is 404
  • configuration : The default value is {}, which is mainly used to customize some configurations of the current feign client
  • fallback : The default value is void.class, which is mainly used as a fuse mechanism. When the call fails, the fallback method can be used to throw an exception or give the default return data.
  • path : The default value is an empty string, the main function is to automatically prefix the requestMapping of all methods, similar to the requestMapping on the controller class

2. create a project

1. File ----- New -----Project

2. Spring Initializr ----- Next

3. Enter Group and Artifact, click Next

4. Choose Spring cloud Discovery ----- Eureka Discovery Client---- Next

5. After the creation is complete, click Finish to open the new project in a new window

3. improve the project

1. Since the file just created is the default project for initialization, you need to add corresponding annotations and configuration files. The details are as follows:

a. Eureka's dependency spring-cloud-starter-eureka <!-- spring-cloud-starter-eureka --> < dependency > < groupId > org.springframework.cloud </groupId > < artifactId > spring-cloud-starter-eureka </artifactId > </dependency > b.Feign's dependency spring-cloud-starter-feign <!-- spring-cloud-starter-feign --> < dependency > < groupId > org.springframework.cloud </groupId > < artifactId > spring-cloud-starter-feign </artifactId > </dependency > c. Web dependency spring-boot-starter-web <!-- spring-boot-starter-web --> < Dependency > < the groupId > org.springframework.boot </the groupId > < the artifactId > Spring-Boot-Starter-Web </the artifactId > </dependency > copy the code

2. Add annotation @EnableEurekaClient to ClientserverApplication.java

3. To improve the configuration file application.properties, you need to formulate an application name, the application name is: Client_Server1

#Service port server.port = 8762 #Registered to the address of the service center eureka.client.serviceUrl.defaultZone=http: //localhost:8761/eureka/ # Whether to register to the eureka server, eureka.client.registerWithEureka = true #Whether to obtain registration information from eureka server eureka.client.fetchRegistry= true #Whether to open the self-protection mode, the default is true . eureka.server.enable-self-preservation= true #Renewal time, that is, the interval of scanning for invalid services (default 60 *1000ms) eureka.server.eviction-interval-timer- in -ms= 10000 spring.application.name=Client-Server1 Copy code

4. Create ClientController

import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; /** * Created by public account: Java full stack architect */ @RestController public class ClientController { @RequestMapping( "/test" ) //@ResponseBody public String test ( @RequestParam String name ) { return "Hello World!==port:8762===" +name; } } Copy code

4. run the project

1. Enter the FeignclientserverApplication.java startup class, right-click and select Run

2. Enter the service registration center:

http://localhost:8761/ shows that the service has been registered successfully (showing two services, because one of them was copied, and the port number was changed: 8763, two Feign projects are displayed, so two are displayed.)

3. Enter http://localhost:8762/test?name=123

The returned data is as follows:

Hello World!port:8762=123

4. The last picture to start three services:

5. the project code

1. pom.xml

<groupId>org.springframework.boot</groupId> < artifactId > spring-boot-starter-parent </artifactId > < version > 2.2.0.RELEASE </version > < relativePath/> <!-- lookup parent from repository --> </parent> <groupId>cn.mcus</groupId> <artifactId>feignclientserver1</artifactId> <version>0.0.1-SNAPSHOT</version> <name>feignclientserver</name> <description>Demo project for Spring Boot</description> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> <spring-cloud.version>Hoxton.M3</spring-cloud.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-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> <!-- spring-cloud-starter-eureka --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <!-- spring-cloud-starter-feign --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-feign</artifactId> </dependency> <!-- spring-boot-starter-web --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> <repositories> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/milestone</url> </repository> </repositories> </project>

2 application.properties

server.port=8762 eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/ # eureka eureka.client.registerWithEureka=true # eureka eureka.client.fetchRegistry=true # true eureka.server.enable-self-preservation=true # 60*1000ms eureka.server.eviction-interval-timer-in-ms=10000 spring.application.name=Client-Server1

3 ClientserverApplication.java

package cn.java; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; /** * Created by Java */ @SpringBootApplication @EnableEurekaClient public class ClientserverApplication { public static void main(String[] args) { SpringApplication.run(ClientserverApplication.class, args); } }

4 Controller

package cn.scpro.Controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController; /** * Created by Java */ @RestController public class ClientController { @RequestMapping("/test") // @ResponseBody public String test(@RequestParam String name){ return "Hello World!==port:8762==="+name; } }

Spring Cloud Feign