springCloud的一大优势就是分布式,下面来看下各微服务模块间的调用
首先创建一个maven项目引入如下依赖
<?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>
<groupId>com.spring.cloud.client1</groupId>
<artifactId>springCloudClient1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>springCloudClient11</name>
<description>springCloudClient1</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.M1</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</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>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
</project>
配置文件(bootstrap.properties)
这个是优于application.properties文件加载的
#注册中心地址
eureka.client.serviceUrl.defaultZone=http://localhost:8888/eureka/
#对应git的分支。如果配置中心使用的是本地存储,则该参数无用
spring.cloud.config.label= master
#对应{profile}部分
spring.cloud.config.profile=test
#对应{application}部分
spring.cloud.config.name=application
#配置中心的具体地址
#spring.cloud.config.uri=http://localhost:8889
#自动寻找配置中心
spring.cloud.config.discovery.enabled=true
spring.cloud.config.discovery.serviceId=config-server
配置文件(application.properties)
spring.application.name=config-client-1
server.port=8891
eureka.client.serviceUrl.defaultZone=http://localhost:8888/eureka/
修改启动类
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
@EnableDiscoveryClient
@EnableFeignClients
@EnableEurekaClient
@SpringBootApplication
public class SpringCloudClient11Application {
public static void main(String[] args) {
SpringApplication.run(SpringCloudClient11Application.class, args);
}
实体类,同上届中实体类方便测试
public class User {
private String id;
private String name;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
微服务间调用方法
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Component
//要调用的服务名
@FeignClient("config-client")
public interface FeignClientTest {
//要调用的服务的暴露接口
@RequestMapping("/client1")
@ResponseBody
User testClient(User user);
}
最后通过调用接口实现调用另一个微服务的方法
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.net.URLEncoder;
@RestController
public class TestClass {
@Autowired
private FeignClientTest feignClientTest;
@RequestMapping("/client2")
public User port(@RequestBody User user)
{
return feignClientTest.testClient(user);
}
}
调用结果如下
这个微服务间调用的好处就是,就算其它服务换了ip,只要服务名不换就可以动态调用