一、創(chuàng)建SpringBoot的項(xiàng)目springcloud-eureka-customer,操作同上一篇
創(chuàng)新互聯(lián)專(zhuān)注為客戶(hù)提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于網(wǎng)站設(shè)計(jì)、網(wǎng)站制作、普陀網(wǎng)絡(luò)推廣、微信小程序定制開(kāi)發(fā)、普陀網(wǎng)絡(luò)營(yíng)銷(xiāo)、普陀企業(yè)策劃、普陀品牌公關(guān)、搜索引擎seo、人物專(zhuān)訪、企業(yè)宣傳片、企業(yè)代運(yùn)營(yíng)等,從售前售中售后,我們都將竭誠(chéng)為您服務(wù),您的肯定,是我們最大的嘉獎(jiǎng);創(chuàng)新互聯(lián)為所有大學(xué)生創(chuàng)業(yè)者提供普陀建站搭建服務(wù),24小時(shí)服務(wù)熱線:028-86922220,官方網(wǎng)址:www.cdcxhl.com
二、配置文件
1、pom.xml
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.1.4.RELEASE
com.jane
springcloud-eureka-customer
0.0.1-SNAPSHOT
springcloud-eureka-customer
Demo project for Spring Boot
1.8
Greenwich.SR1
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-starter-web
org.springframework.cloud
spring-cloud-starter-openfeign
2.0.2.RELEASE
org.springframework.cloud
spring-cloud-openfeign-core
2.0.2.RELEASE
org.springframework.cloud
spring-cloud-dependencies
${spring-cloud.version}
pom
import
org.springframework.boot
spring-boot-maven-plugin
2、啟動(dòng)文件
package com.jane.springcloudeurekacustomer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScans;
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients(basePackages = "com.jane")
@ComponentScan("com.jane")
public class SpringcloudEurekaCustomerApplication {
public static void main(String[] args) {
SpringApplication.run(SpringcloudEurekaCustomerApplication.class, args);
}
}
3、application.properties
server.port=6020
spring.application.name=springcloud-eureka-customer
eureka.client.service-url.defaultZone=http://127.0.0.1:6001/eureka/
eureka.client.fetchRegistry=true
4、Fegin客戶(hù)端TestFeginServiceClient
package com.jane.service;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@FeignClient("springcloud-eureka-client-order")
public interface TestFeginServiceClient {
@RequestMapping(value = "/test", method = RequestMethod.GET)
public String test();
}
5、調(diào)用文件TestController
package com.jane.controller;
import com.jane.service.TestFeginServiceClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestController {
@Autowired
TestFeginServiceClient testService;
/**
* 普通Restful
* @return
*/
@RequestMapping(value = "/local", method = RequestMethod.GET)
public String local() {
return "本地local調(diào)用";
}
/**
* 利用Fegin客戶(hù)端實(shí)現(xiàn)RPC調(diào)用order服務(wù)
* @return
*/
@RequestMapping(value = "/order/test", method = RequestMethod.GET)
public String test(){
return testService.test();
}
}
三、啟動(dòng)文件
1、服務(wù)中心
2、服務(wù)消費(fèi)
2.1本地調(diào)用
2.2RPC調(diào)用
四、FeignClient本身集成了本地負(fù)載均衡Netflix的Ribbon,所以可以輕松實(shí)現(xiàn)負(fù)載均衡。
1、修改client-order端口號(hào)6011,多啟動(dòng)一個(gè)服務(wù)
2、消費(fèi)服務(wù)多次調(diào)用結(jié)果如下: