測試使用的是SpringBoot項目??梢允褂?a target="_blank">Spring官方文檔 提供的頁面直接下載。
1. 創(chuàng)建Eureka服務注冊中心
1.1.maven配置
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<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>
1.2.配置文件
#服務端口
server.port=8080
#服務名稱
spring.application.name=eureka-server
#服務地址
eureka.instance.hostname=localhost
#禁止自己當做服務注冊
eureka.client.register-with-eureka=false
#屏蔽注冊信息
eureka.client.fetch-registry=false
1.3.開啟Eureka功能
使用@EnableEurekaServer注解
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
1.4.啟動服務
打開http://localhost:8080/,進入eureka管理頁面,此時在Instances currently registered with Eureka一欄可以看到,還沒有服務注冊進來。
2.創(chuàng)建provider,注冊登記到服務中心
2.1.maven配置
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<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>
2.2.配置文件
#服務端口
server.port=8090
#服務名稱
spring.application.name=server-provider
#服務地址
eureka.instance.hostname=localhost
#注冊中心路徑,表示我們向這個注冊中心注冊服務,如果向多個注冊中心注冊,用“,”進行分隔
eureka.client.serviceUrl.defaultZone=http://localhost:8080/eureka
2.3.開啟自動注冊功能
@EnableEurekaClient注解,注意跟server不一樣
@SpringBootApplication
@EnableEurekaClient
public class ServerProviderApplication {
public static void main(String[] args) {
SpringApplication.run(ServerProviderApplication.class, args);
}
}
2.4.寫測試用例,對外提供服務的接口
@RestController
public class UserController {
@GetMapping("/user/{name}")
public String getUser(@PathVariable String name) {
return new User(name).getIdentity();
}
}
3.創(chuàng)建consumer,調(diào)用provider的接口
3.1.maven配置
跟2.1配置一樣
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<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>
3.2.配置文件
跟2.2.幾乎一樣,注意服務端口/名稱可能不一致
#服務端口
server.port=8091
#服務名稱
spring.application.name=server-consumer
#服務地址
eureka.instance.hostname=localhost
#注冊中心路徑,表示我們向這個注冊中心注冊服務,如果向多個注冊中心注冊,用“,”進行分隔
eureka.client.serviceUrl.defaultZone=http://localhost:8080/eureka
3.3.開啟自動注冊功能
@EnableEurekaClient注解
@SpringBootApplication
@EnableEurekaClient
public class ServerProviderApplication {
public static void main(String[] args) {
SpringApplication.run(ServerProviderApplication.class, args);
}
}
3.4.使用RestTemplate調(diào)用服務
@Configuration
public class RestConfig {
@Bean
@LoadBalanced //負載均衡
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
3.5.寫測試用例
@RestController
@Slf4j
public class TestController {
# 注冊在服務中心的服務名
private String providerServerName = "server-provider";
private final RestTemplate restTemplate;
public TestController(RestTemplate restTemplate) {
this.restTemplate = restTemplate;
}
@GetMapping("/test")
public String getUserIdentity() {
val identity = restTemplate.getForEntity(String.format("http://%s/user/test", providerServerName), String.class).getBody();
return identity;
}
}
4.測試
啟動三個服務,調(diào)用consumer的/test方法,查看范圍結(jié)果。