fegin整合了hystrix和ribbon。在ribbon中我们通过使用RestTemplate来实现对远程服务的调用,但是在代码的编写上并不美观,Spring Cloud给出了另一套方案,也即是本篇中提到的Feign,Spring Cloud Feign 是基于Netflix Feign 实现的。
下面通过搭建一个通过feign调用服务的例子:
服务提供方:
pom.xml:
<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>
<parent>
<groupId>com.dbs</groupId>
<artifactId>zsy-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>project-service-client</artifactId>
</project>
application.properties:
server.port=2222
spring.application.name=provide-clinet
#设置本微服务需要挂载的服务治理的地址
eureka.client.serviceUrl.defaultZone=http://10.10.1.73:1111/eureka/
#设置本服务将地址转换为服务机器的IP
eureka.instance.prefer-ip-address=true
eureka.instance.instanceId=${spring.application.name}:${spring.application.instance_id:${server.port}}
启动类:
@SpringBootApplication
@EnableDiscoveryClient
public class ProvideClientApplication {
public static void main(String[] args) {
SpringApplication.run(ProvideClientApplication.class, args);
}
}
服务提供Controller:
此类通过@RequestMapping注解提供的方法可供服务调用方调用.
@RestController
@RefreshScope
public class helloController {
@Value("${urlname}")
private String bar;
@RequestMapping("/app")
public String hello() {
System.out.println("--------------------------"+bar);
return "Hello " + bar + "!";
}
@RequestMapping(value="/test/{id}")
public String hello1(@PathVariable String id) {
System.out.println("---------------------++-----"+id);
return "Hello " +id+ bar + "!";
}
}
服务调用方:
pom.xml
<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>
<parent>
<groupId>com.zsy</groupId>
<artifactId>zsy-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>project-service-feign</artifactId>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-feign -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
</dependencies>
</project>
application.properties:
#设置本微服务的名称
spring.application.name=feign-client
#设置本微服务的端口
server.port=11102
#设置本微服务需要挂载的服务治理的地址
eureka.client.serviceUrl.defaultZone=http://10.10.1.73:1111/eureka/
#设置本服务将地址转换为服务机器的IP
eureka.instance.prefer-ip-address=true
eureka.instance.instanceId=${spring.application.name}:${spring.application.instance_id:${server.port}}
启动类:
@EnableDiscoveryClient
@SpringBootApplication
@EnableFeignClients
public class FeignClientApplication {
public static void main(String[] args) {
SpringApplication.run(FeignClientApplication.class, args);
}
}
服务调用Service:
@RequestMapping(value ="/test/{id}", method = RequestMethod.GET)对应服务提供方Controller的方法。
//name的值为微服务的名称
@FeignClient(name="provide-clinet")
public interface TestFeignRequestService {
@RequestMapping(value ="/test/{id}", method = RequestMethod.GET)
public String test(@PathVariable("id") String id) ;
}
服务调用Controller:
@RestController
public class FeignRequestController {
@Autowired
private TestFeignRequestService testFeignRequestService;
@RequestMapping("/feigntest/{id}")
public String test(@PathVariable String id) {
String str = testFeignRequestService.test(id);
return str +"--------"+id;
}
}
一个简单的服务提供和服务调用例子就介绍完毕了。
本文介绍了如何使用Spring Cloud Feign实现微服务间的调用,包括服务提供者和消费者的配置及代码实现。
1269

被折叠的 条评论
为什么被折叠?



