Feign搭建

xiaoxiao2021-03-01  32

Maven依赖

springboot版本:2.0.2.RELEASEspringcloud版本:Finchley.BUILD-SNAPSHOT <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>

项目架构(服务提供方)

eureka集群搭建:https://blog.csdn.net/qq_33594101/article/details/80422876

EurekaApplication.java

package zzq.eureka; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @EnableEurekaServer @SpringBootApplication @RestController public class EurekaApplication{ @Value("${server.port}") private int port; @RequestMapping("hello") public String hello(){ return "eureka-server:"+port; } public static void main(String[] args) { SpringApplication.run(EurekaApplication.class, args); } }

application.yml

spring: application: name: eureka-server profiles: active: server1,server2,server3

项目架构(远程服务调用方)

FeignBeanFactoryPostProcessor.java

package zzq.feign.config; import org.springframework.beans.BeansException; import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.beans.factory.config.BeanFactoryPostProcessor; import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; import org.springframework.stereotype.Component; import java.util.Arrays; @Component public class FeignBeanFactoryPostProcessor implements BeanFactoryPostProcessor { @Override public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { if (containsBeanDefinition(beanFactory, "feignContext", "eurekaAutoServiceRegistration")) { BeanDefinition bd = beanFactory.getBeanDefinition("feignContext"); bd.setDependsOn("eurekaAutoServiceRegistration"); } } private boolean containsBeanDefinition(ConfigurableListableBeanFactory beanFactory, String... beans) { return Arrays.stream(beans).allMatch(b -> beanFactory.containsBeanDefinition(b)); } }

HelloService.java

package zzq.feign.service; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.RequestMapping; /** *远程服务调用 **/ @FeignClient("eureka-server")//服务名称 public interface HelloService { @RequestMapping("hello")//映射路径 String hello(); }

FeignApplication.java

package zzq.feign; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.cloud.openfeign.EnableFeignClients; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import zzq.feign.service.HelloService; @SpringBootApplication @EnableEurekaClient @EnableFeignClients @RestController public class FeignApplication { @Autowired private HelloService hs; @Value("${server.port}") private int port; @RequestMapping("hello") public String hello(){ return hs.hello()+"/nWeb:"+port; } public static void main(String[] args) { SpringApplication.run(FeignApplication.class, args); } }

application.yml

server: port: 8801 spring: application: name: fegin eureka: client: service-url: defaultZone: http://server1:8764/eureka/,http://server2:8762/eureka/,http://server3:8763/eureka/

运行结果

源码地址:https://github.com/18770911080/Springboot-Architecture

转载请注明原文地址: https://www.6miu.com/read-3199942.html

最新回复(0)