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,server3FeignBeanFactoryPostProcessor.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