一、 【microcloud-consumer-feign】爲了可使用到 feign 支持,須要修改 pom.xml 配置文件,引入相關依賴包:須要JAVA Spring Cloud大型企業分佈式微服務雲構建的B2B2C電子商務平臺源碼 一零三八七七四六二六java
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
複製代碼
feign 包含了 Ribbon 支持,因此導入了以上的依賴包以後就表示項目之中已經存在有了 ribbon 相關支持庫。web
二、 【microcloud-service】創建一個新的模塊,這個模塊專門負責客戶端接口的定義;spring
三、 【microcloud-service】修改 pom.xml 配置文件,引用「microcloud-api」模塊,這樣就可使用到 VO 類了;api
<dependency>
<groupId>cn.study</groupId>
<artifactId>microcloud-api</artifactId>
</dependency>
複製代碼
四、 【microcloud-service】此時若是要經過 Feign 進行遠程 Rest 調用,那麼必需要考慮服務的認證問題。bash
此時能夠刪除原始的 RestConfig 進行的配置處理,而後添加feign的認證配置類app
package cn.study.commons.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import feign.auth.BasicAuthRequestInterceptor;
@Configuration
public class FeignClientConfig {
@Bean
public BasicAuthRequestInterceptor getBasicAuthRequestInterceptor() {
return new BasicAuthRequestInterceptor("studyjava", "hello");
}
}
複製代碼
五、 【microcloud-service】創建一個 IDeptClientService 接口;負載均衡
package cn.study.service;
import java.util.List;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import cn.study.commons.config.FeignClientConfig;
import cn.study.vo.Dept;
/**
* 經過註解@FeignClient添加接口對應的遠程微服務名稱value="MICROCLOUD-PROVIDER-DEPT"和
* 服務的認證configuration=FeignClientConfig.class
*
*/
@FeignClient(value="MICROCLOUD-PROVIDER-DEPT",configuration=FeignClientConfig.class)
public interface IDeptClientService {
@RequestMapping(method=RequestMethod.GET,value="/dept/get/{id}")
public Dept get(@PathVariable("id") long id) ;
@RequestMapping(method=RequestMethod.GET,value="/dept/list")
public List<Dept> list() ;
@RequestMapping(method=RequestMethod.POST,value="/dept/add")
public boolean add(Dept dept) ;
}
複製代碼
六、 【microcloud-consumer-feign】修改 pom.xml 配置文件,引入 microcloud-service 開發包:分佈式
<dependency>
<groupId>cn.study</groupId>
<artifactId>microcloud-service</artifactId>
</dependency>
複製代碼
七、 【microcloud-consumer-feign】修改 ConsumerDeptController 控制器程序類;微服務
package cn.study.microcloud.controller;
import javax.annotation.Resource;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import cn.study.service.IDeptClientService;
import cn.study.vo.Dept;
@RestController
public class ConsumerDeptController {
@Resource
private IDeptClientService deptService ;
@RequestMapping(value = "/consumer/dept/get")
public Object getDept(long id) {
return this.deptService.get(id);
}
@RequestMapping(value = "/consumer/dept/list")
public Object listDept() {
return this.deptService.list();
}
@RequestMapping(value = "/consumer/dept/add")
public Object addDept(Dept dept) throws Exception {
return this.deptService.add(dept);
}
}
複製代碼
八、 【microcloud-consumer-feign】修改程序啓動主類,追加操做處理。測試
package cn.study.microcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients(basePackages={"cn.study.service"})//進行接口IDeptClientService的掃描生成使得能夠注入到ConsumerDeptController裏面
public class Consumer_80_StartSpringCloudApplication {
public static void main(String[] args) {
SpringApplication.run(Consumer_80_StartSpringCloudApplication.class,
args);
}
}
複製代碼
九、 啓動測試:client.com/consumer/de… ,能夠發現 Feign 在處理的時候自帶有負載均衡的配置項。java B2B2C Springboot電子商務平臺源碼