個人博客:蘭陵笑笑生,歡迎瀏覽博客!java
上一章 SpringCloud基礎教程(一)-微服務與springCloud當中,咱們介紹了什麼是微服務的架構,以及微服務架構的經常使用組件。本章將繼續探索SpringClud當中的服務發現,Eureka。web
Eureka 是Netflix開源的一款提供服務註冊和發現的產品,SpringCloud集成幷包裝了它。通俗的講,Eureka就是一個服務中心,是將全部的能夠提供的服務都註冊到這裏去管理。調用者須要的時候就去註冊中心獲取,而後在發起調用。spring
因此,服務中心是很是的重要的,一旦宕機,會影響所有的服務,爲此,咱們須要搭建Eureka的集羣。架構
新建Maven項目,在pom.xml文件中引入org.springframework.cloud的依賴,<scope>import</scope>表示須要時在導入,此處的SpringCloud版本是Greenwich.SR3,早期的eureka依賴和當前不大同樣,artifactId 是spring-cloud-starter-eureka-server。這裏用的是發行版比較新的,SpringBoot使用的是2.1.1版本,以下:app
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Greenwich.SR3</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
新建 EurekaServerApplication.java類,添加@EnableEurekaServer註解:maven
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @SpringBootApplication @EnableEurekaServer public class EurekaServerApplication { public static void main(String[] args) { SpringApplication.run(EurekaServerApplication.class, args); } }
啓動程序後,打開http://localhost:8080/ 就能夠看到以下圖的監控頁面。ide
服務端application.yml配置文件spring-boot
#eureka 服務端 spring: application: name: eureka-server server: port: 8080 eureka: instance: hostname: 192.168.0.102 #服務端的實例名稱 client: service-url: # 設置與註冊中心交互的url , defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ #false表示本身就是服務中心 fetch-registry: false #不註冊本身到註冊中心 register-with-eureka: false
新建maven項目爲服務提供方,並在pom.xml中添加依賴微服務
<!--springBoot的核心文件--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <!--web依賴--> <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>
新建ServerProviderApplication.java啓動類和 ProviderController.java控制器fetch
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; @SpringBootApplication @EnableEurekaClient public class ServerProviderApplication { public static void main(String[] args) { SpringApplication.run(ServerProviderApplication.class, args); } }
控制器
@RestController public class ProviderController { @RequestMapping("/sayHello") public String sayHello(String name) { return "hello!,"+name; } }
在application.yml配置eureka的服務地址和自身的服務名稱
#服務提供方 spring: application: name: server-provider server: port: 9001 eureka: instance: hostname: 192.168.0.102 #eureka服務端的實例名稱 client: service-url: # 與註冊中心交互的url defaultZone: http://${eureka.instance.hostname}:8080/eureka/
啓動ServerProviderApplication後,再去查看Eureka的監控頁面,應該能夠看到服務已經註冊到Eureka了
新建maven項目,爲服務調用方,在pom.xml文件中添加依賴
<dependencies> <!--springBoot的核心文件--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <!--web依賴--> <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-client</artifactId> </dependency> </dependencies>
新建ServerConsumerApplication.java啓動類,添加@EnableEurekaClient註解:
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; @SpringBootApplication @EnableEurekaClient public class ServerConsumerApplication { public static void main(String[] args) { SpringApplication.run(ServerConsumerApplication.class, args); } }
編寫控制器,
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cloud.client.ServiceInstance; import org.springframework.cloud.client.discovery.DiscoveryClient; import org.springframework.context.annotation.Bean; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; import java.util.List; @RestController public class ConsumerController { @Bean public RestTemplate restTemplate() { return new RestTemplate(); } @Autowired private DiscoveryClient discoveryClient; @Autowired RestTemplate restTemplate; @RequestMapping("/sayHello") public String sayHello(String name) { List<ServiceInstance> instances = discoveryClient.getInstances("server-provider"); if (!instances.isEmpty()) { ServiceInstance serviceInstance = instances.get(0); String res = restTemplate.getForObject( serviceInstance.getUri().toString() + "/sayHello?name=" + name, String.class); return res; } return "failed"; } }
此時,啓動調用方服務,咱們經過HTTP請求調用/sayHello接口,就能夠看到服務方從Eureka獲取服務提供方的信息,並進行調用返回信息了。以下圖:
這一篇文章簡單的介紹了Eureka組件的使用,並結合示例搭建了服務提供方、調用方等服務。可以對Eureka註冊中心進行初步的瞭解和使用。
以上就是本期的分享,你還能夠關注本博客的#Spring Cloud基礎教程!#
本文由博客一文多發平臺 OpenWrite 發佈!
個人博客地址蘭陵笑笑生,歡迎瀏覽!