Learning micro service to serve consumers - Feign

In addition to restTemplate+ribbon, there is another way to call between microservices: Feign

1. Create a feign service

build.gradle file

buildscript {
	ext {
		springBootVersion = '2.0.4.RELEASE'
	}
	repositories {
		mavenCentral()
	}
	dependencies {
		classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
	}
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'

group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8

repositories {
	mavenCentral()
}


ext {
	springCloudVersion = 'Finchley.SR1'
}

dependencies {
	compile('org.springframework.boot:spring-boot-starter-web')
	compile('org.springframework.cloud:spring-cloud-starter-netflix-eureka-client')
	compile('org.springframework.cloud:spring-cloud-starter-openfeign')
	testCompile('org.springframework.boot:spring-boot-starter-test')
}

dependencyManagement {
	imports {
		mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
	}
}

application.yml file

server:
  port: 8795


spring:
  application:
    name: service-feign


eureka:
  client:
    service-url:
      defaultZone: http://localhost:8791/eureka/

Start main method

package com.example.servicefeign;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;

@EnableFeignClients
@EnableEurekaClient
@SpringBootApplication
public class ServiceFeignApplication {

	public static void main(String[] args) {
		SpringApplication.run(ServiceFeignApplication.class, args);
	}
}

Define a feign interface and specify which service to call through @ FeignClient ("service name"). For example, the /hi interface of service-hi service is invoked in code.

SayHiService.java file

package com.example.servicefeign;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@FeignClient("eureka-client-say-hi")  //Service name to call
public interface SayHiService {

    @RequestMapping(value = "/hi", method = RequestMethod.GET) //The interface route of the service to call,
    String sayHiFromClient(); //In parentheses, you can fill in the parameters of the interface to be called


}

HelloController.java file

package com.example.servicefeign;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {




    @Autowired
    SayHiService sayHiService;

    @RequestMapping("/hi")
    public String sayHi(){
        return sayHiService.sayHiFromClient();
    }
}

Start the project and visit http://localhost:8795/hi

and

 

It is found that feign adopts interface based annotation and has load balancing function

Keywords: Spring Java Gradle Eclipse

Added by irishdreaming on Sun, 05 Jan 2020 06:27:40 +0200