Skip to content

develop rest service

chengyouling edited this page Jun 13, 2022 · 9 revisions

开发REST服务和调用REST服务

provider服务端开发

  • Application服务启动

注意使用注解@EnableDiscoveryClient,以实现框架的微服务发现功能,具体编码如下:

@SpringBootApplication
@EnableDiscoveryClient
public class ProviderApplication {
  public static void main(String[] args) {
    SpringApplication.run(ProviderApplication.class, args);
  }
}
  • 对外提供接口层

@RestController相当于@ResponseBody和@controller注解,使用该注解返回的是内容,而不是jsp、html页面。 @GetMapping注解,处理get请求,传统的RequestMapping来编写应该是 @RequestMapping(value = “/get/{id}”, method = RequestMethod.GET) 新方法可以简写为:@GetMapping("/get/{id}"); @PostMapping:处理post请求; @PutMapping:它的使用方法与PostMapping几乎是一样的,主要区别就是幂等性,@PostMapping注解是标示接口为 非幂等性接口,一般处理插入数据,@PutMapping注解是标示接口为幂等性接口,一般处理更新数据; @DeleteMapping 处理删除数据请求。

@RestController
public class ProviderController {
  // a very simple service to echo the request parameter
  @GetMapping("/sayHello")
  public String sayHello(@RequestParam("name") String name) {
    return "Hello " + name;
  }
}
  • 服务端开启跨应用comsumer调用

若要开启跨应用调用,首先需在provider端的bootstrap.yaml文件开启跨应用调用配置。 注意:需要升级微服务版本号, 以便在服务中心重新注册微服务信息,即使是development开发环境,也需要升级微服务版本号,因为development环境下, 也只有契约发生变化,才会重新注册契约。

配置项如下:

spring:
  cloud:
    servicecomb:
      discovery:
        version: 0.0.1
		allowCrossApp: true

comsumer客户端开发

1、RestTemplate方式调用服务

  • Application服务启动

同样引用注解@EnableDiscoveryClient,以实现框架的微服务发现功能,注入RestTemplate类增加@LoadBalanced表示通过 负载均衡实现服务调用。

@SpringBootApplication
@EnableDiscoveryClient
public class ConsumerApplication {
  public static void main(String[] args) {
    SpringApplication.run(ConsumerApplication.class, args);
  }

  //注入RestTemplate类
  @LoadBalanced
  @Bean
  public RestTemplate restTemplate() {
    return new RestTemplate();
  }
}
  • 调用服务端控制层

comsumer客户端相对前段也是一个服务提供者,需要通过@GetMapping、@PostMapping、@PutMapping和@DeleteMapping来提供url路径; 调用服务端URL格式为:http://basic-provider/sayHello。以用SpringMVC开发微服务中定义的服务提供者为例,其微服务名称是 basic-provider,请求Path是/sayHello,具体代码示例如下:

@RestController
public class ConsumerController {
  @Autowired
  private RestTemplate restTemplate;

  // consumer service which delegate the implementation to provider service.
  @GetMapping("/sayHello")
  public String sayHello(@RequestParam("name") String name) {
    return restTemplate.getForObject("http://basic-provider/sayHello?name={1}", String.class, name);
  }
}
  • 调用跨应用服务端

跨应用调用时,需要加上provider所属的应用ID,格式为[appID]:[microserviceName]。 示例假设provider所属应用为basic-application-cross,微服务名称为basic-provider; consumer所属应用为basic-application,微服务名称为basic-consumer,具体代码如下:

@GetMapping("/sayHello")
public String sayHello(@RequestParam("name") String name) {
	return restTemplate.getForObject("http://basic-application-cross.basic-provider/sayHello?name={1}", String.class, name);
}

2、Feign方式调用服务

  • Application服务启动

同样引用注解@EnableDiscoveryClient,以实现框架的微服务发现功能,并且需要增加@EnableFeignClients注解,保证项目启动阶段加 载feign相关配置信息,表示当前服务作为feign调用的客户端,定义feign接口即可调用服务端。

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class ConsumerApplication {
  public static void main(String[] args) {
    SpringApplication.run(ConsumerApplication.class, args);
  }
}
  • 定义feignClient服务层

增加@feignClient注解,显示指定value值为要调用的端服务名称,注意接口的参数个数、类型及返回结果要与服务端保持一致。

@FeignClient(value = "basic-provider")
public interface FeignConsumerService {

    @GetMapping("/sayHelloFeign")
    String sayHelloFeign(@RequestParam("name") String name);
}
  • 调用服务端控制层

相对REST方式调用服务层,直接注入定义的feignClient服务层,通过服务层接口直接调用服务接口,具体编码如下:

@RestController
public class ConsumerController {
  
  @Autowired
  private FeignConsumerService feignConsumerService;

  @GetMapping("/sayHelloFeign")
  public String sayHelloFeign(@RequestParam("name") String name) {
    return feignConsumerService.sayHelloFeign(name);
  }
}
Clone this wiki locally