上一篇文章介绍了order-service 调用 user-service
@Autowired
private RestTemplate restTemplate; @Bean //负载均衡 @LoadBalanced
public RestTemplate getRestTemplate(){
return new RestTemplate();
} @GetMapping("/order")
public String test1() { //此处服务名称 http://user-service return restTemplate.getForObject("http://user-service/hello",String.class);
}
RestTemplate还需要写上服务及IP这些信息,本章介绍openFeign
openFeign是基于REST的服务调用上提供更高级别的抽象。Spring Cloud openFeign在声明性原则上工作。使用openFeign时,我们在客户端编写声明式REST服务接口,并使用这些接口来编写客户端程序。开发人员不用担心这个接口的实现。这将在运行时由Spring动态配置。通过这种声明性的方法,开发人员不需要深入了解由HTTP提供的HTTP级别API的细节的RestTemplate。
一,编写接口,user-service-api 提供一组基于rest风格的api,新建子模块user-service-api
此时项目结构为
1,加入依赖pom
<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> <parent> <groupId>com.liu.dianmall</groupId> <artifactId>mall-parent</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <artifactId>user-service-api</artifactId> <properties> <java.version>1.8</java.version> </properties>
<dependencies> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> </dependencies>
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
2,添加配置文件application.yml
server: port: 9002
spring: application: name: user-service-api cloud: nacos: discovery: server-addr: 127.0.0.1:8848 3,添加接口userClient
package com.liu.nacos;
import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.GetMapping;
import com.liu.nacos.impl.UserClientHystrix;
//user-service 服务名 fallback 阻断后调用的服务
@FeignClient(name = "user-service",fallback = UserClientHystrix.class) public interface UserClient {
@GetMapping("/hello") String helloNacos(); }
添加实现类
package com.liu.nacos.impl;
import com.liu.nacos.UserClient;
public class UserClientHystrix implements UserClient{
public String helloNacos() { return "请求超时!"; } }
添加启动类,测试
package com.liu.nacos;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.openfeign.EnableFeignClients; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication @RestController @EnableDiscoveryClient @EnableFeignClients public class UserServiceApiApplication {
public static void main(String[] args) { SpringApplication.run(UserServiceApiApplication.class, args); }
@Autowired private UserClient userClient;
@GetMapping("/api/user") public String test() { return userClient.helloNacos(); } }
四,修改 order-service项目,修改为feign调用
1,加入api依赖
<dependency> <groupId>com.liu.dianmall</groupId> <artifactId>user-service-api</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency>
2,修改,
package com.liu.nacos;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.context.annotation.Bean; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate;
@SpringBootApplication @EnableDiscoveryClient @RestController public class OrderServiceApplication {
public static void main(String[] args) { SpringApplication.run(OrderServiceApplication.class, args); } @Autowired UserClient userClient; /* * @Autowired private RestTemplate restTemplate; * * @Bean * * @LoadBalanced public RestTemplate getRestTemplate(){ return new * RestTemplate(); } * * @GetMapping("/order") public String test1() { * * return restTemplate.getForObject("http://user-service/hello",String.class); } */ @GetMapping("/order") public String test1() { return userClient.helloNacos(); } }