上一篇文章我們介紹了eureka服務(wù)注冊中心的搭建,這篇文章介紹一下如何使用eureka服務(wù)注冊中心,搭建一個簡單的服務(wù)端注冊服務(wù)地回,客戶端去調(diào)用服務(wù)使用的案例。
案例中有三個角色:服務(wù)注冊中心俊鱼、服務(wù)提供者刻像、服務(wù)消費者,其中服務(wù)注冊中心就是我們上一篇的eureka單機版啟動既可并闲,流程是首先啟動注冊中心细睡,服務(wù)提供者生產(chǎn)服務(wù)并注冊到服務(wù)中心中,消費者從服務(wù)中心中獲取服務(wù)并執(zhí)行帝火。
服務(wù)提供
我們假設(shè)服務(wù)提供者有一個hello方法溜徙,可以根據(jù)傳入的參數(shù)湃缎,提供輸出“hello xxx,this is first messge”的服務(wù)
1蠢壹、pom包配置
創(chuàng)建一個springboot項目嗓违,pom.xml中添加如下配置:
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>復制代碼
2、配置文件
application.properties配置如下:
spring.application.name=spring-cloud-producer
server.port=9000
eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/復制代碼
參數(shù)在上一篇都已經(jīng)解釋過图贸,這里不多說蹂季。
3、啟動類
啟動類中添加@EnableDiscoveryClient
注解
@SpringBootApplication
@EnableDiscoveryClient
public class ProducerApplication {
public static void main(String[] args) {
SpringApplication.run(ProducerApplication.class, args);
}
}復制代碼
4疏日、controller
提供hello服務(wù)
@RestController
public class HelloController {
@RequestMapping("/hello")
public String index(@RequestParam String name) {
return "hello "+name+"偿洁,this is first messge";
}
}復制代碼
添加@EnableDiscoveryClient
注解后,項目就具有了服務(wù)注冊的功能沟优。啟動工程后涕滋,就可以在注冊中心的頁面看到SPRING-CLOUD-PRODUCER服務(wù)。
image.png
到此服務(wù)提供者配置就完成了净神。