好的响应式网站,建筑设计网站国外,wordpress数据库命名,万网域名解析面板之前写了篇《Feign在实际项目中的应用实践总结》Feign在实际项目中的应用实践总结 - 沐风之境 - 博客园www.cnblogs.com总结了在一般项目中如何使用Feign这个提升开发效率的利器。最近在看Feign的文档的时候发现了之前遗漏的一些点#xff0c;所以写了这篇文章进行补充。pom…之前写了篇《Feign在实际项目中的应用实践总结》Feign在实际项目中的应用实践总结 - 沐风之境 - 博客园www.cnblogs.com总结了在一般项目中如何使用Feign这个提升开发效率的利器。最近在看Feign的文档的时候发现了之前遗漏的一些点所以写了这篇文章进行补充。pom.xmldependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-starter-openfeign/artifactId
/dependency
dependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-starter-loadbalancer/artifactId
/dependencyJavaConfigSlf4j
Configuration
EnableFeignClients
public class FeignConfig {Resourceprivate SimpleDiscoveryProperties simpleDiscoveryProperties;// 在K8s环境中不需要服务注册中心可以自定义一个DiscoveClient的Bean作为简单的一个服务列表Beanpublic DiscoveryClient discoveryClient() {log.info(JSON.toJSONString(simpleDiscoveryProperties)); // 这里打印了加载的配置参数return new SimpleDiscoveryClient(simpleDiscoveryProperties);}
}配置在里面写服务的路由配置spring:cloud:loadbalancer:ribbon:enabled: falsediscovery:client:simple:instances:feign-attempt-client:- uri: http://localhost:8080为什么要使用本地的服务列表服务部署在K8s环境中可以使用k8s提供的DNS解析和负载均衡功能。所以不需要再引入一个服务注册中心组建导致复杂性的增加。使用import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;FeignClient(name feign-attempt-client)
public interface FeignServerClient {GetMapping(/feign/server/hello-world/hello)String hello();
}运行单元测试import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import javax.annotation.Resource;
import static org.junit.jupiter.api.Assertions.*;SpringBootTest
class FeignServiceClientTest {Resourceprivate FeignServiceClient feignServiceClient;Testvoid hello() {final String res feignServiceClient.hello(); // HTTP请求返回字符串“Hi!”assertEquals(res, Hi!);}
}运行细节加载的服务路由配置可以看到instances的对象加载了我们定义的feign-attempt-client服务它包含了一个服务实例。为什么只要定义个服务实例呢因为我们服务是部署在K8s上的。k8s会帮我们做多实例的负载均衡的工作{instances: {feign-attempt-client: [{host: localhost,metadata: {},port: 8080,secure: false,serviceId: feign-attempt-client,uri: http://localhost:8080}]},local: {host: 172.16.8.55,metadata: {},port: 8080,secure: false,serviceId: application,uri: http://172.16.8.55:8080},order: 0
}