RestTemplate 是springweb组建
作为Spring Cloud的子项目之一,Spring Cloud OpenFeign 是一种声明式、模板化的 HTTP 客户端,在 Spring Cloud 中使用 OpenFeign,可以做到使用 HTTP请求远程服务时能与调用本地方法一样的编码体验,开发者完全感知不到这是远程方法,更感知不到这是个 HTTP 请求。同时OpenFeign通过集成Ribbon实现客户端的负载均衡
nacos-server : 注册中心,解决是服务的注册与发现
Ribbon:客户端负载均衡器,解决的是服务集群负载均衡的问题
OpenFeign:声明式 HTTP 客户端 、代替Resttemplate组件,实现远程调用
演示案例说明
订单跟积分 订单的增删改影响积分增删改
新建积分微服务
pom依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
</dependencies>
application.properties
spring:
application:
name: integral
cloud:
nacos:
discovery:
server-addr: 192.168.59.12:8848,192.168.59.12:8858,192.168.59.12:8868
username: nacos
password: nacos
server:
port: 9003
启动类
@SpringBootApplication
@EnableDiscoveryClient
public class IntegralApplication {
public static void main(String[] args) {
SpringApplication.run(IntegralApplication.class, args);
}
}
暴露接口
@RestController
@RequestMapping("/integral")
public class IntegralController {
@RequestMapping("/save")
public Map save(@RequestBody Integral integral){
System.out.println(integral);
Map<String, Object> map = new HashMap<>();
map.put("status",0);
map.put("msg","添加成功");
return map;
}
@RequestMapping("/update")
public Map update(@RequestBody Integral integral){
System.out.println(integral);
HashMap<String, Object> map = new HashMap<>();
map.put("status",0);
map.put("msg","修改成功");
return map;
}
@RequestMapping("/{id}")
public Map deleteById(@PathVariable("id") Integer id){
System.out.println("积分:"+id+"已经删除");
HashMap<String, Object> map = new HashMap<>();
map.put("status",0);
map.put("msg","删除成功");
return map;
}
@RequestMapping("/getOne")
public Integral getOne(Integer id){
System.out.println("查询的id为"+id+"积分对象");
Integral integral = new Integral();
integral.setId(id);
integral.setType("会员积分");
integral.setCount(500);
return integral;
}
@RequestMapping("/getByCondition")
public Integral getByCondition(Integer id,String type){
System.out.println("id"+id);
System.out.println("type"+type);
Integral integral = new Integral();
integral.setCount(8000);
integral.setType(type);
integral.setId(id);
return integral;
}
@RequestMapping("/getAll")
public List<Integral> getAll(){
Integral integral = new Integral();
integral.setId(1);
integral.setCount(800);
integral.setType("bb");
Integral integral1 = new Integral();
integral.setId(1);
integral.setCount(900);
integral.setType("aa");
Integral integral2 = new Integral();
integral.setId(1);
integral.setCount(100);
integral.setType("cc");
List<Integral> integrals = Arrays.asList(integral, integral1, integral2);
return integrals;
}
}
实体类
实体类放在common 项目里 通过依赖引用
public class Integral {
private Integer id;
private Integer count;
private String type;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getCount() {
return count;
}
public void setCount(Integer count) {
this.count = count;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
@Override
public String toString() {
return "Integral{" +
"id=" + id +
", count=" + count +
", type='" + type + '\'' +
'}';
}
}
Openfeign使用
openfeign依赖 在order项目使用
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
1.在order的启动类增加@EnableFeignClients 开启Openfeign 并且指定扫描哪里
2.写接口声明
@FeignClient 写了接口声明 指定是哪个暴露接口
这里是一个接口 方法名跟路径 参数等 要跟积分暴露接口里的完全一致
注意:这里如果参数时对象 要加@RequestBody注解
是路径参数{id}类似要加@PathVariable注解 delete/1
是参数kv形式的加@RwquestParam注解 ?id=1&&name=张三
@FeignClient("integral")
@RequestMapping("/integral")
public interface IntegralApi {
@RequestMapping("/save")
public Map save(@RequestBody Integral integral);
@RequestMapping("/update")
public Map update(@RequestBody Integral integral);
@RequestMapping("/{id}")
public Map deleteById(@PathVariable("id") Integer id);
@RequestMapping("/getOne")
public Integral getOne(@RequestParam("id") Integer id);
@RequestMapping("/getByCondition")
public Integral getByCondition(@RequestParam("id") Integer id,@RequestParam("Type") String type);
@RequestMapping("/getAll")
public List<Integral> getAll();
}
接口调用
@RestController
@RequestMapping("/orders")
public class OrderController {
@Autowired
private IntegralApi integralApi;
//
@RequestMapping("/saveIntegral")
public String saveIntegral() {
Integral integral = new Integral();
integral.setId(1);
integral.setCount(9527);
integral.setType("促销活动");
Map save = integralApi.save(integral);
System.out.println(save);
return "ok";
}
@RequestMapping("/update")
public String testUpdate() {
Integral integral = new Integral();
integral.setId(1);
integral.setCount(9527);
integral.setType("促销活动");
Map save = integralApi.update(integral);
System.out.println(save);
return "ok";
}
@RequestMapping("/delete/{id}")
public String testDelete(@PathVariable("id") Integer id){
Map map = integralApi.deleteById(id);
System.out.println(map);
return "ok";
}
@RequestMapping("/getOne")
public String getOne(Integer id){
System.out.println(id);
Integral one = integralApi.getOne(id);
System.out.println(one);
return "ok";
}
@RequestMapping("/getByCondition")
public String getByCondition(Integer id, String type){
Integral byCondition = integralApi.getByCondition(id, type);
System.out.println(byCondition);
return "ok";
}
@RequestMapping("/getAll")
public String getAll(){
List<Integral> all = integralApi.getAll();
System.out.println(all);
return "ok";
}
}
实例访问