1. 程式人生 > >Spring Cloud入門程序——註冊服務提供者

Spring Cloud入門程序——註冊服務提供者

ann disco align prope not -a out net col

1、創建Spring Starter project

2、引入依賴

技術分享圖片

點擊finish

技術分享圖片

3、創建啟動類

package com.hello;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

/***
 * 
 * @EnableDiscoveryClient
 * 讓服務使用eureka服務器
 * 實現服務註冊和發現
 *
 
*/ @EnableDiscoveryClient @SpringBootApplication public class Springmvc012HelloServiceApplication { public static void main(String[] args) { SpringApplication.run(Springmvc012HelloServiceApplication.class, args); } }

4、創建controller

package com.hello;

import java.util.List;

import
org.springframework.beans.factory.annotation.Autowired; import org.springframework.cloud.client.ServiceInstance; import org.springframework.cloud.client.discovery.DiscoveryClient; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController
public class HelloController { @Autowired DiscoveryClient discoveryClient; @RequestMapping("/hello") public String hello() { List<ServiceInstance> list = discoveryClient.getInstances("STORES"); System.out.println("discoveryClient.getServices().size() = " + discoveryClient.getServices().size()); for( String s : discoveryClient.getServices()){ System.out.println("services " + s); List<ServiceInstance> serviceInstances = discoveryClient.getInstances(s); for(ServiceInstance si : serviceInstances){ System.out.println(" services:" + s + ":getHost()=" + si.getHost()); System.out.println(" services:" + s + ":getPort()=" + si.getPort()); System.out.println(" services:" + s + ":getServiceId()=" + si.getServiceId()); System.out.println(" services:" + s + ":getUri()=" + si.getUri()); System.out.println(" services:" + s + ":getMetadata()=" + si.getMetadata()); } } System.out.println(list.size()); if (list != null && list.size() > 0 ) { System.out.println( list.get(0).getUri() ); } return "hello,this is hello-service"; } }

5、配置項目的 application.properties

server.port=9090
#設置服務名
spring.application.name=hello-service
#設置服務註冊中心的URL,本服務要向該服務註冊中心註冊自己
eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka

6、瀏覽器訪問

(1)訪問服務提供者項目 的請求 /hello:

技術分享圖片

控制臺打印:

技術分享圖片

(2)訪問服務註冊中心:

之前:

技術分享圖片

之後,再次訪問localhost:1111,發現有服務註冊到註冊中心了

技術分享圖片

7、總結:

服務治理可以說是微服務架構中最為核心和基礎的模塊,它主要用來實現各個微服務實例的自動化註冊發現

Spring Cloud Eureka是Spring Cloud Netflix 微服務套件的一部分,主要負責完成微服務架構中的服務治理功能。

本文通過簡單的小例子來分享下如何通過Eureka進行服務治理:

  • 搭建服務註冊中心
  • 註冊服務提供者(本文)
  • 服務發現和消費

實踐終極目標:手把手,以上實例實現了基本的服務治理:

  • 通過spring-cloud-starter-eureka-server和@EnableEurekaServer實現服務註冊中心
  • 通過spring-cloud-starter-eureka和@EnableDiscoveryClient使用並註冊到服務註冊中心
  • 通過spring-cloud-starter-eureka和@EnableDiscoveryClient使用註冊中心並發現服務,通過spring-cloud-starter-ribbon來實現負載均衡消費服務

Donate捐贈

如果我的文章幫助了你,可以贊賞我 1 元給我支持,讓我繼續寫出更好的內容)

技術分享圖片 技術分享圖片

(微信) (支付寶)

微信/支付寶 掃一掃

Spring Cloud入門程序——註冊服務提供者