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();
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章