SpringCloud学习第四篇:Feign学习(Hoxton.SR4)

一、Feign是什么?

Feign是一个声明式WebService客户端。使用Feign能让编写Web Service客户端更加简单,它的使用方法就是定义一个接口,然后在上面添加注解,同时也支持JAX-RS标准的注解。Feign也支持可插拔式的编码器和解码器。SpringCloud对Feign进行了封装,使其支持了Spring MVC标准注解和HttpMessageConverters,并整合了Ribbon和Eureka,从而让Feign的使用更加方便。在Spring Cloud feign的实现下,只需要创建一个接口并用注解方式配置它,即可完成服务提供方的接口绑定,简化了在使用Spring Cloud Ribbon时自行封装服务调用客户端的开发量。

二、工程的搭建

2.1、公共接口(spring-cloud-interface-demo)

  • Maven
   <dependencies>
        <!-- 服务调用 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
    </dependencies>
  • dto
@Data
public class UserDto {

    /**
     * ID
     */
    private Long id;

    /**
     * 用户名称
     */
    private String name;

}
  • 接口
@FeignClient(name="service-demo")
public interface UserService {
    /**
     * 保存用户
     * @param user
     */
    @PostMapping("/user/save")
    boolean saveUser( UserDto user);
    /**
     * 查询所有的用户列表
     */
    @GetMapping("/user/find/all")
    List<UserDto> findAll();
}

2.3、服务提供者(spring-cloud-service-demo)

  • Maven
    <dependencies>
    <!-- 依赖 API -->
    <dependency>
        <groupId>com.yk</groupId>
        <artifactId>spring-cloud-interface-demo</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </dependency>
    <!-- 依赖 Spring Cloud eureka client -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
   </dependencies>
  • application.properties
# Tomcat
server:
  port: 1201
spring:
  application:
    name: service-demo
eureka:
  client:
    service-url:
      #注册地址
      defaultZone: http://localhost:1101/eureka/
  #显示服务器IP加端口
  instance:
    hostname: localhost
    prefer-ip-address: true
    instance-id: ${spring.cloud.client.ip-address}:${server.port}
  • 启动入口类
@SpringBootApplication
@EnableDiscoveryClient //eureka
public class ServiceDemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(ServiceDemoApplication.class, args);
    }
}
  • service
public interface UserService {
    /**
     * 保存用户
     * @param user
     */

    boolean saveUser( UserDto user);
    /**
     * 查询所有的用户列表
     */

    List<UserDto> findAll();
}
@Service
public class  UserServiceImpl implements UserService {

    private Map<Long, UserDto> repository = new ConcurrentHashMap<>();

    @Override
    public boolean saveUser(UserDto user) {
        return repository.put(user.getId(), user) == null;
    }

    @Override
    public List<UserDto> findAll() {
        return new ArrayList(repository.values());
    }
}
  • Controller
@RestController
@RequestMapping("user")
public class DemoController {
    @Autowired
    private UserService userService;

    @PostMapping("save")
    public boolean saveUser(@RequestBody UserDto user) {
        return userService.saveUser(user);
    }

    @GetMapping("find/all")
    public List<UserDto> findAll() {
        return userService.findAll();
    }
}

2.4、服务调用者(spring-cloud-web-demo)

  • Maven
    <dependency>
        <groupId>com.yk</groupId>
        <artifactId>spring-cloud-interface-demo</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
  • application.properties
spring.application.name = spring-cloud-api-client
## 服务端口
# Tomcat
server:
  port: 1301
spring:
  application:
    name: web-demo
eureka:
  client:
    service-url:
      #注册地址
      #      defaultZone: http://localhost:1101/eureka/,http://localhost:11010/eureka/
      defaultZone: http://localhost:1101/eureka/
  #显示服务器IP加端口
  instance:
    hostname: localhost
    prefer-ip-address: true
    instance-id: ${spring.cloud.client.ip-address}:${server.port}

  • 启动入口类
@SpringBootApplication
//声明 UserService接口作为Feign Client 调用
@EnableFeignClients(clients = UserService.class)
@EnableDiscoveryClient
public class WebDemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(WebDemoApplication.class, args);
    }
}
  • Service
public class DemoService {
    /**
     * 保存用户
     * @param user
     */

    boolean saveUser( UserDto user);
    /**
     * 查询所有的用户列表
     */

    List<UserDto> findAll();
}
  • ServiceImpl
@RestController
public class DemoServiceImpl implements DemoService {
    @Autowired
    private UserService userService;
    @Override
    public boolean saveUser(UserDto user) {
        return userService.saveUser(user);
    }
    @Override
    public List<UserDto> findAll() {
        return userService.findAll();
    }
}
  • Controller
@RestController
@RequestMapping("damo")
public class DemoController {
    @Autowired
    private DemoService demoService;
    @PostMapping("save")
    public boolean saveUser(@RequestBody UserDto user) {
        return demoService.saveUser(user);
    }
    @GetMapping("queryAll")
    public List<UserDto> findAll() {
        return demoService.findAll();
    }
}

你可能感兴趣的:(Spring全家桶)