重慶分公司,新征程啟航
為企業提供網站建設、域名注冊、服務器等服務
為企業提供網站建設、域名注冊、服務器等服務
一、創建SpringBoot的項目springcloud-eureka-customer,操作同上一篇
創新互聯專注為客戶提供全方位的互聯網綜合服務,包含不限于網站設計、網站制作、普陀網絡推廣、微信小程序定制開發、普陀網絡營銷、普陀企業策劃、普陀品牌公關、搜索引擎seo、人物專訪、企業宣傳片、企業代運營等,從售前售中售后,我們都將竭誠為您服務,您的肯定,是我們最大的嘉獎;創新互聯為所有大學生創業者提供普陀建站搭建服務,24小時服務熱線:028-86922220,官方網址: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、啟動文件
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客戶端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、調用文件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調用";
}
/**
* 利用Fegin客戶端實現RPC調用order服務
* @return
*/
@RequestMapping(value = "/order/test", method = RequestMethod.GET)
public String test(){
return testService.test();
}
}
三、啟動文件
1、服務中心
2、服務消費
2.1本地調用
2.2RPC調用
四、FeignClient本身集成了本地負載均衡Netflix的Ribbon,所以可以輕松實現負載均衡。
1、修改client-order端口號6011,多啟動一個服務
2、消費服務多次調用結果如下: