项目清单
Feign 需要注册到 eureka 服务中,下面分别建立四个项目
- eureka-server (spring-cloud-rureka-server-feign) name:eureka-server-feign port:9090
- parent -feign[父项目] -(spring-cloud-feign) -- 以下项目采用聚合工程
- feign-api[契约服务] name:feign-api port:8081
- feign-consumer[服务消费端] name:person-consumer port:8082
- feign-provider[服务提供方] name:person-provider port:8083
Eureka Server 项目
1.Maven 导入
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
2.Application.java 增加注解
@SpringBootApplication
@EnableEurekaServer //开启eureka server
public class SpringCloudRurekaServerFeignApplication {
public static void main(String[] args) {
SpringApplication.run(SpringCloudRurekaServerFeignApplication.class, args);
}
}
3.修改 application.properties
server.port = 9090
spring.application.name = eureka-server-feign
eureka.client.fetch-registry = false
eureka.client.register-with-eureka = false
# 开启全部actuator管理
management.endpoints.web.exposure.include=*
Parent : Feign 项目(spring-cloud-feign)
1.Maven 导入
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</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-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
2.修改pom
新建 Module : feign-api
1.修改 pom
<parent>
<groupId>com.blaaair</groupId>
<artifactId>spring-cloud-feign</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
2.Application.java 配置
@SpringBootApplication
@EnableEurekaClient
public class SpringCloudFeignApiApplication {
public static void main(String[] args) {
SpringApplication.run(SpringCloudFeignApiApplication.class, args);
}
}
3.application.properties 配置
server.port = 8081
spring.application.name = feign-api
eureka.client.service-url.defaultZone = http://localhost:9090/eureka
management.endpoints.web.exposure.include=*
4.增加接口
@FeignClient("person-provider") //person-provider : 接口提供方应用名称
public interface PersonService {
@PostMapping("/person/save")
boolean save(@RequestBody Person person);
}
新建 Module :feign-consumer
1.修改 pom
<parent>
<groupId>com.blaaair</groupId>
<artifactId>spring-cloud-feign</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
需在依赖中增加
feign-api
2.Application.java 配置
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients(clients = PersonService.class) //接受暴露出的
public class SpringCloudFeignApiApplication {
public static void main(String[] args) {
SpringApplication.run(SpringCloudFeignApiApplication.class, args);
}
}
3.application.properties 配置
server.port = 8082
spring.application.name = person-consumer
eureka.client.service-url.defaultZone = http://localhost:9090/eureka
management.endpoints.web.exposure.include=*
4.增加调用远程 service 的 controller
@RestController
public class PersonController implements PersonService {
private final PersonService personService;
@Autowired
public PersonController(PersonService personService) {
this.personService = personService;
}
@Override
public boolean save(Person person) {
return personService.save(person);
}
}
新建 Module : feign-provider
1.修改 pom
<parent>
<groupId>com.blaaair</groupId>
<artifactId>spring-cloud-feign</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
需在依赖中增加
feign-api
2.Application.java 配置
@SpringBootApplication
@EnableEurekaClient
public class SpringCloudFeignApiApplication {
public static void main(String[] args) {
SpringApplication.run(SpringCloudFeignApiApplication.class, args);
}
}
3.application.properties 配置
server.port = 8083
# 这里的name 与feign-api中@FeignClient("person-provider")对应
spring.application.name = person-provider
eureka.client.service-url.defaultZone = http://localhost:9090/eureka
management.endpoints.web.exposure.include=*
4.提供的调用 controller
@RestController
public class PersonController {
@PostMapping("/person/save")
boolean save(@RequestBody Person person){
return new HashMap<>().put(person.getId(),person)==null;
}
}
测试
分别启动项目:eureka-server - > feign-provider - > feign-api - > feign-consumer
POSTMAN : localhost:8082/person/save ,
- raw:
{ id:1, name:"ws" }
- 返回:true 则表示成功!
- 调用顺序 : postman --> consumer --> provider
欢迎来到这里!
我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。
注册 关于