【Eureka】服務發現調用html
轉載:http://www.javashuo.com/article/p-mgxeiwwd-e.htmljava
一、使用 Netfix Feign 客戶端調用服務web
首先引入 spring-cloud-starter-openfeign 依賴,那個工程使用那個工程就引用spring
<?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>ycx</groupId> <version>0.0.1-SNAPSHOT</version> <artifactId>demo-server</artifactId> <name>demo-server</name> <description>sc server</description> <properties> <java.version>1.8</java.version> <spring-cloud.version>Greenwich.SR1</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>ycx</groupId> <artifactId>common-api</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> <!-- spring boot --> <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-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- spring cloud --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> <!-- other --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> </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>
其次使用 @FeignClient 定義接口和實現,也就是被調用者apache
接口,注意:必定要指定 value 或 name,其值是註冊服務的應用名, @FeignClient(value = "common-server")api
package ycx.common.feign; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import ycx.common.bean.Result; @FeignClient(value = "common-server") @RequestMapping("/common/feign") public interface CommonFeign { @GetMapping("/info") Result<String> info(); }
實現app
package ycx.common.feign.impl; import org.springframework.http.HttpStatus; import org.springframework.web.bind.annotation.RestController; import ycx.common.bean.Result; import ycx.common.feign.CommonFeign; @RestController public class CommonFeignImpl implements CommonFeign { @Override public Result<String> info() { Result<String> result = new Result<>(); result.setStatus(String.valueOf(HttpStatus.OK.value())); result.setMessage(HttpStatus.OK.getReasonPhrase()); result.setData("Common"); return result; } }
接口端 pommaven
<?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>ycx</groupId> <version>0.0.1-SNAPSHOT</version> <artifactId>common-api</artifactId> <name>common-api</name> <description>sc server</description> <properties> <java.version>1.8</java.version> <spring-cloud.version>Greenwich.SR1</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>ycx</groupId> <artifactId>common-api</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> <!-- spring boot --> <!-- spring cloud --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> <!-- other --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> </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> </project>
再次調用者啓動類使用 @EnableFeignClients 註解客戶端,注意:指定掃描的包ide
@SpringBootApplication @EnableDiscoveryClient @EnableFeignClients({"ycx.*.feign"}) @RestController public class DemoServerApplication
最後使用接口調用服務spring-boot
package ycx.demo; import org.springframework.beans.factory.annotation.Autowired; 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.Bean; import org.springframework.http.HttpMethod; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.util.CollectionUtils; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import ycx.common.bean.Result; import ycx.common.feign.CommonFeign; import java.util.List; @SpringBootApplication @EnableDiscoveryClient //可不寫 @EnableFeignClients({"ycx.common.feign"}) @RestController public class DemoServerApplication { public static void main(String[] args) { SpringApplication.run(DemoServerApplication.class, args); } @Autowired CommonFeign commonFeign; @GetMapping("/fn") public Result<String> fn() { return commonFeign.info(); } }
二、使用帶有 Ribbon 功能的 RestTemplate 調用服務
必須使用 @LoadBalanced 標註 RestTemplate Bean
@LoadBalanced
@Bean
public RestTemplate getRestTemplate() {
return new RestTemplate();
}
/** * 使用帶有 Ribbon 功能的 RestTemplate 訪問服務 * @return */ @GetMapping("/rest") public Result<String> rest() { ResponseEntity<Result> restExchange = restTemplate.exchange( "http://common-server/", HttpMethod.GET, null, Result.class); return restExchange.getBody(); }
三、使用 DiscoveryClient 和 普通 RestTemplate 調用服務
@Autowired DiscoveryClient discoveryClient; /** * 使用 DiscoveryClient 查找服務,使用標準的 RestTemplate 訪問服務 */ @GetMapping("/") public Result<String> ok() { List<ServiceInstance> instanceList = discoveryClient.getInstances("common-server"); if(CollectionUtils.isEmpty(instanceList)) { Result<String> result = new Result<>(); result.setStatus(String.valueOf(HttpStatus.NOT_FOUND)); result.setMessage(HttpStatus.NOT_FOUND.getReasonPhrase()); return result; } else { RestTemplate restTemplate = new RestTemplate(); String uri = instanceList.get(0).getUri().toString(); ResponseEntity<Result> restExchange = restTemplate.exchange(uri, HttpMethod.GET, null, Result.class); return restExchange.getBody(); } }