I. Introduction of FEIGN
Feign is a declarative, templated HTTP client. This makes the writing of the Web service client more convenient to use feign to create an interface and annotate it. It has pluggable annotation support, including feign annotations and jax-rs annotations. Feign also supports pluggable encoders and decoders. Spring cloud adds support for spring MVC annotations and uses the httpmessageconverters that are used by default in the Spring Web. Spring Cloud integrates the Ribbon and Eureka to provide load-balanced HTTP clients when using feign. This phrase comes from an official document, which is a simple way to invoke the rest interface via feign, without the need for other HTTP access Components, and also provides load balancing, codec, and other functions that are convenient to use.
Ii. Introduction of the environment
Start the Eureka service on a server first, and then start the Ms-demo-provider service on the B and C two servers separately, which can also be deployed on a single server with different ports. Access the Eureka interface to view the status of the service registration, after which the new client on-premises calls the project for testing. At this point A is a registry, B and C are service providers (providing the same interface), and local projects serve consumers.
Third, the project code
To create a MAVEN project in idea, the Ms-eurekaclient-demo Engineering code is structured as follows:
The dependencies in 1:pom.xml are as follows:
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd" > <modelversion>4.0.0 </modelVersion> <groupId>com.cloud.microservice</groupId> <artifactId> Ms-eurekaclient-demo</artifactid> <version>0.0.1-SNAPSHOT</version> <packaging>jar</ Packaging> <name>ms-eurekaclient-demo</name> <description>demo Project for Spring boot</description> <parent> <groupId>org.springframework.boot</groupId> & Lt;artifactid>spring-boot-starter-parent</artifactid> <version>1.5.9.RELEASE</version> <relativePath/> <!--lookup parent from repository to </parent> <properties> <pro Ject.build.sourceencoding>utf-8</project.build.sourceencoding> <project.reporting.outputencoding >UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> <spring-c loud.version>edgware.sr1</spring-cloud.version> </properties> <dependencies> <depende Ncy> <groupId>org.springframework.cloud</groupId> <artifactid>spring-cloud-star ter-feign</artifactid> </dependency> <dependency> <groupid>org.springfram Ework.cloud</groupid> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency& Gt <groupId>org.springframework.cloud</groupId> <artifactid>spring-cloud-starter-ribbon</arti Factid> </dependency> <!--add-on circuit breakers--<dependency> <groupid>or G.springframework.cloud</groupid> <artifactId>spring-cloud-starter-hystrix</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactid>spring-boot-starter-test</a rtifactid> <scope>test</scope> </dependency> </dependencies> <depend Encymanagement> <dependencies> <dependency> <groupid>org.springframework.clou D</groupid> <artifactId>spring-cloud-dependencies</artifactId> <version >${spring-cloud.version}</version> <type>pom</type> <scope>Import</scope> </dependency> </dependencies> </dependencyManagement> <BUILD&G T <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build></project>
The configuration information in 2:application.properties is as follows:
spring.application.name=ms-eurekaclient-demoserver.port=9800# Registration Center Address Eureka.client.serviceUrl.defaultZone=http://xx.xx.xx.xx:9000/eureka/ This client should fetch Eureka Registry information from Eureka server# whether clients want to get registration information from Eureka Server, default is Trueeurek A.client.fetchregistry=true# indicates how often (in seconds) to fetch the registry information from the Eureka server# the frequency at which registration information is obtained from Eureka Server, which defaults to 30 seconds, shortening the configuration time can alleviate the problem of long service uptime Eureka.client.registryFetchIntervalSeconds =10
3: Add annotations to @enableeurekaclient and @enablefeignclients in the Ingress class application
- @EnableEurekaClient: Annotations are used to identify the Open service Discovery feature and are said to use @enablediscoveryclient Online Some people say that @enableeurekaclient itself is to use @enablediscoveryclient to achieve, this has not been carefully studied
- @EnableFeignClients: Annotations are used to turn on the feign function
PackageCom.cloud.microservice.eurekaclientdemo;Importorg.springframework.boot.SpringApplication;Importorg.springframework.boot.autoconfigure.SpringBootApplication;Importorg.springframework.cloud.netflix.eureka.EnableEurekaClient;Importorg.springframework.cloud.netflix.feign.enablefeignclients;@ Springbootapplication@enableeurekaclient@enablefeignclients Public classfeigndemoapplication { Public Static voidMain (string[] args) {Springapplication.run (feigndemoapplication.class, args); }}
4: Create the Iuserfeignserviceclient interface class with the following code:
Packagecom.cloud.microservice.eurekaclientdemo.FeignClient;Importorg.springframework.cloud.netflix.feign.FeignClient;Importorg.springframework.web.bind.annotation.RequestMapping;ImportOrg.springframework.web.bind.annotation.RequestMethod; @FeignClient ("Ms-demo-provider") Public Interfaceiuserfeignserviceclient {//feign defining the service Provider Interface@RequestMapping (value = "/demo/user/1.0/findall", method = Requestmethod.get, produces = {"application/json;charset= UTF-8 "}) String findAll ();}
5: Create the Iuserservice interface class and the Userserviceimp implementation class with the following code:
The Iuserservice interface classes are as follows:
Package com.cloud.microservice.eurekaclientdemo.FeignClient; Public Interface iuserservice { String findAll ();}
The USERSERVICEIMP implementation classes are as follows:
package com.cloud.microservice.eurekaclientdemo.FeignClient; import org.springframework.beans.factory.annotation.Autowired; import Org.springframework.stereotype.Service; @Service public class Userserviceimp implements iuserservice{@ autowired private Iuserfeignserviceclient userfeignserviceclient; public String FindAll () { return "feign:" + Userfeignserviceclient.findall (); }}
6:rest interface Definition, the code is as follows:
Packagecom.cloud.microservice.eurekaclientdemo.FeignClient;Importorg.springframework.beans.factory.annotation.Autowired;Importorg.springframework.web.bind.annotation.RequestMapping;ImportOrg.springframework.web.bind.annotation.RequestMethod;ImportOrg.springframework.web.bind.annotation.RestController; @RestController Public classFeigndemocontroller {@AutowiredPrivateIuserservice UserService; @RequestMapping (Value= "/feigndemo/findall", method = Requestmethod.get, produces = {"Application/json;charset=utf-8"}) PublicString Feigndemo () {returnUserservice.findall (); }}
Start the project and refresh the Eureka interface to see that feign is already registered with the service center
Iv. running the test
Open the browser, access the interface in Ms-eurekaclient-demo, Address: Http://localhost:9800/feigndemo/findAll, return the results as follows:
The above results indicate that the interface call was successful. We can also log on to the service provider's server.
With multiple accesses to the HTTP://LOCALHOST:9800/FEIGNDEMO/FINDALL interface, you can see that the provider on the B and C two servers have an interface called record.
Springcloud Series Research---Eureka service consumption feign