SpringCloud中Rest微服务构建(下)

本文介绍如何创建Spring Cloud项目中的Consumer模块,包括配置pom.xml引入所需依赖、设置application.yml、注入RESTTEMPLATE并创建Controller来实现远程服务调用。

1.Consumer模块的创建

pom.xml文件编写:


  1. <?xml version=”1.0″ encoding=”UTF-8″?>

  2. <project xmlns=”http://maven.apache.org/POM/4.0.0″

  3. xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”

  4. xsi:schemaLocation=”http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd”>

  5. <parent>

  6. <artifactId>springcloud</artifactId>

  7. <groupId>com.csh</groupId>

  8. <version>1.0-SNAPSHOT</version>

  9. </parent>

  10. <modelVersion>4.0.0</modelVersion>

  11. <artifactId>springcloud-consumer-dept-80</artifactId>

  12. <dependencies>

  13. <dependency>

  14. <groupId>com.csh</groupId>

  15. <artifactId>springcloudapi</artifactId>

  16. <version>1.0-SNAPSHOT</version>

  17. </dependency>

  18. <dependency>

  19. <groupId>org.springframework.boot</groupId>

  20. <artifactId>spring-boot-starter-web</artifactId>

  21. </dependency>

  22. <dependency>

  23. <groupId>org.springframework.cloud</groupId>

  24. <artifactId>spring-cloud-starter-ribbon</artifactId>

  25. <version>1.4.7.RELEASE</version>

  26. </dependency>

  27. <dependency>

  28. <groupId>org.springframework.cloud</groupId>

  29. <artifactId>spring-cloud-starter-config</artifactId>

  30. </dependency>

  31. <dependency>

  32. <groupId>org.springframework.cloud</groupId>

  33. <artifactId>spring-cloud-starter-eureka</artifactId>

  34. <version>1.4.7.RELEASE</version>

  35. </dependency>

  36. <dependency>

  37. <groupId>org.springframework.boot</groupId>

  38. <artifactId>spring-boot-devtools</artifactId>

  39. </dependency>

  40. </dependencies>

  41. <properties>

  42. <maven.compiler.source>8</maven.compiler.source>

  43. <maven.compiler.target>8</maven.compiler.target>

  44. </properties>

  45. </project>

application.yml文件编写


  1. server:

  2. port: 80

新建一个CONFIGBEAN包注入 RESTTEMPLATE!


  1. package com.csh.springcloud.config;

  2. import org.springframework.cloud.client.loadbalancer.LoadBalanced;

  3. import org.springframework.context.annotation.Bean;

  4. import org.springframework.context.annotation.Configuration;

  5. import org.springframework.web.client.RestTemplate;

  6. @Configuration

  7. public class ConfigBean {

  8. @Bean

  9. @LoadBalanced

  10. public RestTemplate getrestTemplate()

  11. {

  12. return new RestTemplate() ;}

  13. }

创建CONTROLLER包,编写DEPTCONSUMERCONTROLLER类


  1. package com.csh.springcloud.controller;

  2. import com.csh.springcloud.pojo.Dept;

  3. import org.springframework.beans.factory.annotation.Autowired;

  4. import org.springframework.web.bind.annotation.PathVariable;

  5. import org.springframework.web.bind.annotation.RequestMapping;

  6. import org.springframework.web.bind.annotation.RestController;

  7. import org.springframework.web.client.RestTemplate;

  8. import java.util.List;

  9. @RestController

  10. public class DeptComsumerController {

  11. @Autowired

  12. RestTemplate restTemplate;

  13. private static final String REST_URL_PREFIX=”http://localhost:8001″;

  14. @RequestMapping(“/consumer/dept/add”)

  15. public boolean add(Dept dept)

  16. {

  17. return restTemplate.getForObject(REST_URL_PREFIX+”/dept/add”,boolean.class);

  18. }

  19. @RequestMapping(“/consumer/dept/get/{id}”)

  20. public Dept getbyid(@PathVariable(“id”) int id)

  21. {

  22. return restTemplate.getForObject(REST_URL_PREFIX+”/dept/find/”+id,Dept.class);

  23. }

  24. @RequestMapping(“/consumer/dept/list”)

  25. public List<Dept> list()

  26. {

  27. return restTemplate.getForObject(REST_URL_PREFIX+”/dept/selectall”,List.class);

  28. }

  29. }

RestTemplate提供了多种便捷访问远程Http服务的方法,是一种简单便捷的访问restful服务模板
类,是Spring提供的用于访问Rest服务的客户端模板工具集
使用RestTemplate访问restful接口非常的简单粗暴且无脑
(url,requsetMap,ResponseBean.class) 这三个参数分别代表REST请求地址,请求参数,
Http响应转换 被 转换成的对象类型

编写主启动类


  1. package com.csh.springcloud;

  2. import org.springframework.boot.SpringApplication;

  3. import org.springframework.boot.autoconfigure.SpringBootApplication;

  4. import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

  5. @EnableEurekaClient

  6. @SpringBootApplication

  7. public class Springcloudconsumer80 {

  8. public static void main(String[] args) {

  9. SpringApplication.run(Springcloudconsumer80.class,args);

  10. }

  11. }

2.访问测试

 

 

 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

jakeonil

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值