一县钥、代碼實例
說明:此處使用的SpringBoot版本為2.1.13.RELEASE伺绽,SpringCloud版本為Greenwich.SR5
Euraka Client是將提供的服務注冊到Eureka供消費者調用,主要以REST方式
1.maven依賴
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<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-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
2.application.yml配置
server:
port: 8001
spring:
application:
name: client
eureka:
instance:
hostname: localhost
prefer-ip-address: true
instance-id: client-8001
client:
service-url:
defaultZone: http://localhost:7001/eureka/
#info信息
info:
app:
name: client-8001
company:
name: www.xxx.com
build:
artifactId: ${project.artifactId}
version: ${project.version}
說明一下:此版本的artifactId可以通過${xxx}或@xxx@方式獲取埂奈,建議用${xxx}方式迄损,@xxx@后續(xù)動態(tài)獲取配置時會報一個錯誤,之前版本應該是$xxx$方式
3.啟動類
此版本無需添加@EnableEurekaClient
@SpringBootApplication
public class Client8001Application {
public static void main(String[] args) {
SpringApplication.run(Client8001Application.class,args);
}
}
4账磺、controller類
提供rest服務
@RestController
public class HelloController {
@GetMapping("/hello/{name}")
public String hello(@PathVariable String name){
return "hello,"+name;
}
}
二芹敌、測試驗證
先后啟動server、client服務訪問http://eureka7001:7001/
服務注冊成功
鼠標點擊client-8001可以查看application.yml中配置的info信息
測試client提供的服務
訪問http://localhost:8001/hello/zs
結果如下:
參考:
https://blog.csdn.net/forezp/article/details/70148833
http://www.itmuch.com/spring-cloud/spring-cloud-index/
還有尚硅谷周陽老師的視頻