赞
踩
1、创建一个Eureka Server工程,项目名称:eureka-server
2、创建一个Eureka Client 服务提供者 工程,项目名称:eureka-client1
工程eureka-server和eureka-client1 创建请参考
启动eureka-server、eureka-client1
PS:eureka-client1 在idea中通过多实例启动
查看 Eureka 控制台
项目名称:service-feign
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.yi</groupId> <artifactId>serice-feign</artifactId> <version>0.0.1-SNAPSHOT</version> <name>serice-feign</name> <description>serice-feign</description> <parent> <groupId>com.yi</groupId> <artifactId>finchley</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> </dependencies> </project>
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
server:
port: 8765
spring:
application:
name: service-feign
feign:
hystrix:
enabled: true
package com.yi.sericefeign; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.cloud.openfeign.EnableFeignClients; @SpringBootApplication @EnableEurekaClient @EnableDiscoveryClient @EnableFeignClients public class SericeFeignApplication { public static void main(String[] args) { SpringApplication.run(SericeFeignApplication.class, args); } }
@EnableFeignClients:开启Feign的功能
@FeignClient(value = "service-client",fallback = HelloWorldServiceImpl.class)
public interface HelloWorldService {
@RequestMapping(value = "/hello", method = RequestMethod.GET)
String helloWorld(@RequestParam(value = "name") String name);
}
@ FeignClient(“服务名”):指定调用哪个服务,通过服务名称
@RestController
public class HelloWorldController {
@Autowired
HelloWorldService helloWorldService;
@GetMapping(value = "/helloWorld")
public String helloWorld(@RequestParam String name) {
System.out.println("...............service-feign");
return helloWorldService.helloWorld(name);
}
}
访问:http://localhost:8765/helloWorld?name=yi
轮询出现,与前章节ribbon一致
hello yi ,port is 8762
hello yi ,port is 8763
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。