spring cloud学习——17. Declarative REST Client: Feign

17. Declarative REST Client: Feign

Feign is a declarative web service client

17.1. How to Include Feign

eureka server port number is 8761

The service provider microservice-provider-user port number is 7900

Service Consumer 8011


pom.xml

<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactI d>spring-cloud-starter-openfeign</artifactId>
</dependency>

Service provider access address http://localhost:7900/demo/dd

Startup class annotation

@EnableEurekaClient
@SpringBootApplication
@EnableFeignClients
Feign interface

package com.ldgx.eshop.feign;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@FeignClient("microservice-provider-user")//Service provider spring.application.name=microservice-provider-user
public interface UserFeignClient {	
	@RequestMapping(value="/demo/dd", method = RequestMethod.GET)//The access path of the service provider is /demo/dd
	public String dd();
}

use in controller

@Autowired
private UserFeignClient client;//调用Feign

17.2. Overriding Feign Defaults

Note: The configuration file Configuration1 cannot be under the scope that @ComponentScan can scan. As in the following configuration file, the startup class is

com/ldgx/eshop/MicroserviceCustomerMovie1Application.java

The configuration file is not under the same package and subpackage of the startup class

package com.ldgx.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import feign.Contract;
@Configuration
public class Configuration1 {	
	/**
	 * feignContract:Defines what annotations and values are valid on interfaces.	 *
	 * The annotation method used by @return
	 */
	@Bean
    public Contract feignContract() {
        return new feign.Contract.Default();//An example of the annotation method of feign.Contract.Default(): @RequestLine("GET /repos/{owner}/{repo}/contributors")
    }
}
feign

package com.ldgx.eshop.feign;
import org.springframework.cloud.netflix.feign.FeignClient;
import com.ldgx.config.Configuration1;
import feign.RequestLine;
@FeignClient(name = "microservice-provider-user",configuration = Configuration1.class)//服务提供者spring.application.name=microservice-provider-user
public interface UserFeignClient {	
	//@RequestMapping(value="/demo/dd",method = RequestMethod.GET)//The access path of the service provider is /demo/dd
	 @RequestLine("GET /demo/dd")
	public String dd();
}

If you also use @RequestMapping(value="/demo/dd", method = RequestMethod.GET), an error will be reported:

Caused by: java.lang.IllegalStateException: Method dd not annotated with HTTP method type (ex. GET, POST)

View specific annotations ( new feign.Contract.Default() ), so open https://github.com/OpenFeign/feign to view


Example 2, still based on the original code

If the address of the eureka server is http://localhost:8761.

Then you can visit http://localhost:8761/eureka/apps/microservice-provider-user

microservice-provider-user is the service provider registered to eureka

Feign, visit to change the address

package com.ldgx.eshop.feign;

import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

@FeignClient(name="xxxx",url = "http://localhost:8761")//eureka server address is http://localhost:8761
public interface FeignClient2 {

	@RequestMapping(value="/eureka/apps/{serviceName}")//The access path of the url is http://localhost:8761/eureka/apps/microservice-provider-user
	public String findServiceInfoFromEurekaByServiceName(@PathVariable("serviceName") String serviceName);
}
controller

@RestController
public class UseDemoController {	
	@Autowired
	private FeignClient2 client2;
		
	@GetMapping("/{serviceName}")
	public String findServiceInfoFromEurekaByServiceName(@PathVariable String serviceName) {
		return client2.findServiceInfoFromEurekaByServiceName(serviceName);
	}
}



17.3. Creating Feign Clients Manually

Add configuration in application.yml

feign.hystrix.enabled: true

17.4. Feign Hystrix Support

Add annotation to startup class

@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
@EnableCircuitBreaker
package com.ldgx.eshop.feign;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@FeignClient(name="microservice-provider-user", fallback = HystrixClientFallback.class)//服务提供者spring.application.name=microservice-provider-user
public interface UserFeignClient {	
	@RequestMapping(value="/demo", method = RequestMethod.GET)//The access path of the service provider is /demo
	public String dd();
}
@Component
class HystrixClientFallback implements UserFeignClient {//circuit breaker
	@Override
	public String dd() {
		// TODO Auto-generated method stub
		return "===feign fall back===";
		//return null;
	}

}


17.5. Feign Hystrix Fallbacks
17.6. Feign and @Primary
17.7. Feign Inheritance Support
17.8. Feign request/response compression
17.9. Feign logging

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325928892&siteId=291194637