服务调用之Feign

tech2022-08-04  131

feign的作用

简化开发,内置robbin实现负载均衡,熔断(需要三步手动配,添加配置文件,在接口上用一个类去写fallback)等机制,使得远程调用其他服务就像调用自己的服务一样便利

第一步:依赖

<?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> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.3.3.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>consume-service</artifactId> <version>0.0.1-SNAPSHOT</version> <name>consume-service</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Hoxton.SR8</version> <type>pom</type> <scope>import</scope> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> <version>2.2.3.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>2.3.1.RELEASE</version> </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> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-ribbon</artifactId> <version>2.2.5.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-netflix-hystrix</artifactId> <version>2.2.5.RELEASE</version> </dependency> <dependency> <groupId>com.netflix.hystrix</groupId> <artifactId>hystrix-javanica</artifactId> <version>RELEASE</version> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> <version>2.2.3.RELEASE</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>

第二步 加注解

启动类上@EnableFeignClients 创建接口:

package com.example.consumeservice.client; import com.example.consumeservice.pojo.User; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; //底层实现负载均衡 @FeignClient("USER-SERVER")//服务名 public interface Userclient { @RequestMapping("/hello/{id}")//请求 User querybyid(@PathVariable("id") int id);//返回值 } // An highlighted block var foo = 'bar';

控制器调用接口:

package com.example.consumeservice.Controller; import com.example.consumeservice.client.Userclient; import com.example.consumeservice.pojo.User; import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties; import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand; import com.netflix.ribbon.proxy.annotation.Hystrix; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cloud.client.ServiceInstance; import org.springframework.cloud.netflix.ribbon.RibbonLoadBalancerClient; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.client.RestTemplate; @Controller @DefaultProperties(defaultFallback = "defaultmethod") public class ConsumerController { //注入接口 @Autowired Userclient userclient; // @Autowired // private RestTemplate restTemplate; // @Autowired // private RibbonLoadBalancerClient client; @RequestMapping("/hello/{id}") @ResponseBody public User query(@PathVariable("id") int id){ // ServiceInstance instanceInfo=client.choose("user-service"); // List<ServiceInstance> list=discoveryClient.getInstances("USER-SERVER"); // ServiceInstance instanceInfo=list.get(0) // String url="http://"+instanceInfo.getHost()+":"+instanceInfo.getPort()+"/hello"; User user= userclient.querybyid(id); return user; } private String queryfallback(){//方法的参数要和 return "不好意思,服务拥挤!"; } public String defaultmethod(){ return "不好意思,服务拥挤!"; } }

就可以实现调用了

最新回复(0)