标签:instance 方便 dea json autowired 客户 上线 wired 中心
Feign是一种声明式、模板化的HTTP客户端。这使得Web服务客户端的写入更加方便 要使用Feign创建一个界面并对其进行注释。它具有可插入注释支持,包括Feign注释和JAX-RS注释。Feign还支持可插拔编码器和解码器。Spring Cloud增加了对Spring MVC注释的支持,并使用Spring Web中默认使用的HttpMessageConverters。Spring Cloud集成Ribbon和Eureka以在使用Feign时提供负载均衡的http客户端。这段话来源于官方文档,说白了就是通过Feign来调用Rest接口,而无需使用其他HTTP访问组件,并且同时还提供了负载均衡、编解码等功能,使用起来很方便。
首先在A服务器上启动Eureka服务,然后在B、C两台服务器上分别启动ms-demo-provider服务,这里也可以部署在一台服务器上采用不同的端口。访问Eureka界面查看服务注册状态,之后在本地新建客户端调用工程进行测试。此时A为注册中心,B、C分别为服务提供者(提供相同的接口),本地工程为服务消费者。
在Idea中创建maven工程,ms-eurekaclient-demo工程代码结构如下:
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.cloud.microservice</groupId> <artifactId>ms-eurekaclient-demo</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>ms-eurekaclient-demo</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.9.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> <spring-cloud.version>Edgware.SR1</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-feign</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-ribbon</artifactId> </dependency> <!-- 加入断路器依赖 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-hystrix</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
spring.application.name=ms-eurekaclient-demo server.port=9800 # 注册中心地址 eureka.client.serviceUrl.defaultZone=http://xx.xx.xx.xx:9000/eureka/ # Indicates whether this client should fetch eureka registry information from eureka server # 客户端是否要从eureka server获取注册信息,默认为true eureka.client.fetchRegistry=true # Indicates how often(in seconds) to fetch the registry information from the eureka server # 从eureka server获取注册信息的频率,默认为30秒,缩短配置时间可以缓解服务上线时间过长的问题 eureka.client.registryFetchIntervalSeconds=10
package com.cloud.microservice.eurekaclientdemo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.cloud.netflix.feign.EnableFeignClients; @SpringBootApplication @EnableEurekaClient @EnableFeignClients public class FeignDemoApplication { public static void main(String[] args) { SpringApplication.run(FeignDemoApplication.class, args); } }
package com.cloud.microservice.eurekaclientdemo.FeignClient; import org.springframework.cloud.netflix.feign.FeignClient; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; @FeignClient("ms-demo-provider") public interface IUserFeignServiceClient { //Feign定义服务提供者接口 @RequestMapping(value = "/demo/user/1.0/findAll", method = RequestMethod.GET, produces = {"application/json;charset=UTF-8"}) String findAll(); }
IUserService接口类如下:
package com.cloud.microservice.eurekaclientdemo.FeignClient; public interface IUserService { String findAll(); }
UserServiceImp实现类如下:
package com.cloud.microservice.eurekaclientdemo.FeignClient; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class UserServiceImp implements IUserService{ @Autowired private IUserFeignServiceClient userFeignServiceClient; public String findAll() { return "Feign: " + userFeignServiceClient.findAll(); } }
package com.cloud.microservice.eurekaclientdemo.FeignClient; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; @RestController public class FeignDemoController { @Autowired private IUserService userService; @RequestMapping(value = "/feigndemo/findAll", method = RequestMethod.GET, produces = {"application/json;charset=UTF-8"}) public String feignDemo() { return userService.findAll(); } }
启动工程,然后刷新Eureka界面,可以看到Feign已经注册到服务中心
Honghu代码结构图:
意了解框架技术或者源码的朋友直接求求交流分享技术:2147775633
SpringCloud微服务云架构构建B2B2C电子商务平台之-Eureka服务消费Feign
标签:instance 方便 dea json autowired 客户 上线 wired 中心
原文地址:https://www.cnblogs.com/leafitit/p/10069279.html