您好,登錄后才能下訂單哦!
一、創(chuàng)建SpringBoot的項(xiàng)目springcloud-eureka-customer,操作同上一篇
二、配置文件
1、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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.jane</groupId>
<artifactId>springcloud-eureka-customer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springcloud-eureka-customer</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR1</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--open feign-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
<version>2.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-openfeign-core</artifactId>
<version>2.0.2.RELEASE</version>
</dependency>
<!--feign-->
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
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客戶端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客戶端實(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é)果如下:
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。