创建一个新的消费者模块并导入依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>springcloud</artifactId>
<groupId>com.kuang</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>springcloud-consumer-dept-feign</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<!-- 消费者:实体类+web 不需要连接数据库,跟前端打交道的 -->
<dependencies>
<!-- Ribbon 集成 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
<version>1.4.6.RELEASE</version>
</dependency>
<!-- Ribbon 集成 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
<version>1.4.6.RELEASE</version>
</dependency>
<!-- Eureka -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
<version>1.4.6.RELEASE</version>
</dependency>
<dependency>
<groupId>com.kuang</groupId>
<artifactId>springcloud-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!-- web 支持 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 热部署 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
</dependencies>
</project>
修改API模块里的
实体类
package com.kuang.springcloud.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import java.io.Serializable;
@NoArgsConstructor
@Accessors(chain = true)//链式写法
@Data
//实现序列化 实体类 ORM映射 mysql--dept
public class Dept implements Serializable {
private Long deptno;//主键
private String dname;
//这个数据库存在哪个数据库字段。~微服务,一个服务对应一个数据库,同一个信息可能存在不同的数据库
private String db_source;
public Dept(String dname) {
this.dname = dname;
}
/**
* 链式写法:
*Dept dept = new Dept();
* dept.setDeptNo(11).setDbane('sss').setDb_source("001");
*/
}
业务层接口,Feign基于接口实现的
package com.kuang.springcloud.service;
import com.kuang.springcloud.pojo.Dept;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import java.util.List;
@Component
@FeignClient(value = "SPRINGCLOUD-PROVIDER-DEPT")
public interface DeptClientService {
@GetMapping("/dept/get/{id}")
Dept queryById(@PathVariable("id") Long id);
@GetMapping("/dept/list")
List<Dept> queryAll();
@PostMapping("/dept/add")
String addDept(Dept dept);
}
消费者Feign服务
Controller
package com.kuang.springcloud.controller;
import com.kuang.springcloud.pojo.Dept;
import com.kuang.springcloud.service.DeptClientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import java.util.List;
@RestController
public class DeptConsumerController {
//理解:消费者,不应该有Service层
//模板有很多方法供我们去使用
//注入过来
//(String url, 实体:map, Class<T> responseType, Object... uriVariables)
//(url,实体,map,Class<T> responseType 返回值类型) 三个参数 五种请求方式
//Ribbon 我们这里的地址,应该是一个变量,通过服务名字来访问
// private static final String REST_URL_PREFIX="http://localhost:8001";
@Autowired
private DeptClientService service;
@RequestMapping("/consumer/dept/get/{id}")
public Dept get(@PathVariable("id")Long id){
return service.queryById(id);
}
@RequestMapping("/consumer/dept/add")
public String add(Dept dept){
return service.addDept(dept);
}
@RequestMapping("/consumer/dept/list")
public List<Dept> list(){
return service.queryAll();
}
}
主启动类加两个注解
package com.kuang.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.ribbon.RibbonClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.ComponentScan;
//Ribbon和Eureka 整合以后 ,客户端可以直接调用,不用关心IP地址和端口号~
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients(basePackages = "com.kuang.springcloud")
public class FeignDeptConsumer80 {
public static void main(String[] args) {
SpringApplication.run(FeignDeptConsumer80.class,args);
}
}
多封装了一层而已!
本质就是Ribbon