Feign:使用接口方式调用服务 springcloud-consumer-dept-feign
导入依赖:pom.xml
<?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 "> <parent> <artifactId>springcloud</artifactId> <groupId>com.kuang</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>springcloud-api</artifactId> <properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> </properties> <dependencies> <!-- feign--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-feign</artifactId> <version>1.4.6.RELEASE</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> </dependencies> </project>config
ConfigBean.java
package com.kuang.springcloud.config; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.client.RestTemplate; @Configuration public class ConfigBean { // @Configuration 相当于---spring 里面的applicationContext.xml //配置负载均衡实现RestTemplate //IRule // AvailabilityFilteringRule :会先过滤掉,跳闸,访问故障的服务,对剩下的进行轮询 // RoundRobinRule: 轮询 //RandomRule: 随机 // RetryRule: 会先按照轮询获取服务~,如果服务获取失败,则会在指定的时间内进行重试 @Bean @LoadBalanced//Ribbon public RestTemplate getRestTemplate(){ return new RestTemplate(); } }controller
DeptConsumerController.java
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 { @Autowired private DeptClientService service = null; @RequestMapping("/consumer/dept/add") public boolean add(Dept dept){ return this.service.addDept(dept); } @RequestMapping("/consumer/dept/get/{id}") public Dept get(@PathVariable("id")Long id) { return this.service.queryById(id); } @RequestMapping("/consumer/dept/list") public List<Dept> list(){ return this.service.queryAll(); } }FeignDeptConsumer.java
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.openfeign.EnableFeignClients; import org.springframework.context.annotation.ComponentScan; //Ribbon和Eureka整合以后,客户端可以直接调用 不用关心IP地址和端口号 @SpringBootApplication @EnableEurekaClient @EnableFeignClients(basePackages = {"com.kuang.springcloud"}) @ComponentScan("com.kuang.springcloud") public class FeignDeptConsumer_80 { public static void main(String[] args){ SpringApplication.run( FeignDeptConsumer_80.class,args); } }application.yml
server: port: 80 # Eureka配置 eureka: client: register-with-eureka: false # 不向Eureka 注册自己 service-url: defaultZone: :7001/eureka/,:7002/eureka/,:7003/eureka/ springcloud-apipojo
Dept.java
package com.kuang.springcloud.pojo; import lombok.Data; import lombok.NoArgsConstructor; import lombok.experimental.Accessors; import java.io.Serializable; @Data @NoArgsConstructor @Accessors(chain = true)//链式写法 //所有的实体类务必实现序列化 public class Dept implements Serializable {// Dept 实体类 ,orm 对象关系映射(列表关系映射) mysql里面的表 对应----dept这个类 private Long deptno;//主键 private String dname; //这个数据存在哪个数据库的字段, 微服务, 一个服务对应一个数据库 同一个信息可能存在不同的数据库所以这里要用 db_source作为区分 private String db_source; //构造器只需要一个就行 deptno主键自增 db_source:数据库函数自动生成 public Dept(String dname) { this.dname = dname; } /* 链式写法: Dept dept = new Dept(); dept.setDeptNo(11).setDname(\'ssss\').setDb_source(\'00\') 链式写法就是可以连续的点下一个 而不用分开写 */ }service