1. 程式人生 > >spring cloud註冊服務與發現(踩著坑往前爬)

spring cloud註冊服務與發現(踩著坑往前爬)

lse value 都是 一個 人員 aging 分享圖片 idea put

spring cloud簡介

Spring Cloud為開發人員提供了快速構建分布式系統中的一些通用模式(例如配置管理,服務發現,斷路器,智能路由,微代理,控制總線,一次性令牌,全局鎖,領導選舉,分布式 會話,群集狀態)。

分布式系統的協調導致了鍋爐板模式,並且使用Spring Cloud開發人員可以快速地站起來實現這些模式的服務和應用程序。 它們可以在任何分布式環境中正常工作,包括開發人員自己的筆記本電腦,裸機數據中心和受管平臺,如Cloud Foundry。

創建註冊中心

使用工具:idea

新建一個maven工程,啥都不要勾選

然後再選一個module

技術分享圖片

技術分享圖片

填寫項目名和包名

技術分享圖片

創建完了之後啥也不要改

創建完了之後pom.xml如下

技術分享圖片
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion
>4.0.0</modelVersion> <groupId>cn.zhiwei</groupId> <artifactId>eureka-server</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>eureka-server</name> <description>Demo project for Spring Boot</
description> <!--父節點--> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.1.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <!--編碼配置--> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> <spring-cloud.version>Finchley.BUILD-SNAPSHOT</spring-cloud.version> </properties> <!--會實際下載jar包--> <dependencies> <!--eureka-server服務--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> <!--測試用的--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <!--//只是對版本進行管理,不會實際引入jar --> <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> <!--插件--> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> <!--版本庫--> <repositories> <repository> <id>spring-snapshots</id> <name>Spring Snapshots</name> <url>https://repo.spring.io/snapshot</url> <snapshots> <enabled>true</enabled> </snapshots> </repository> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/milestone</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> </project>
View Code

我的idea創建的spring boot的版本是二點多的

啟動一個服務註冊中心,只需要一個註解@EnableEurekaServer,這個註解需要在springboot工程的啟動application類上加

package cn.zhiwei;

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);
	}
}

  eureka是一個高可用的組件,它沒有後端緩存,每一個實例註冊之後需要向註冊中心發送心跳(因此可以在內存中完成),

在默認情況下erureka server也是一個eureka client ,必須要指定一個 server。eureka server的配置文件appication.yml

server:
  port: 8761
server的配置文件appication.yml
eureka:
  instance:
    hostname: localhost
  client:
  #通過eureka.client.registerWithEureka:false和fetchRegistry:false來表明自己是一個eureka server.
  #不給這幾句話會報錯
    registerWithEureka: false
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

eureka server 是有界面的,啟動工程,打開瀏覽器訪問:http://localhost:8761 ,界面如下:

技術分享圖片

創建一個服務提供者 (eureka client)

當client向server註冊時,它會提供一些元數據,例如主機和端口,URL,主頁等。Eureka server 從每個client實例接收心跳消息。 如果心跳超時,則通常將該實例從註冊server中刪除。

和剛才一樣新建一的module

在pom.xm裏面新加一個節點

<!--web-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

通過註解@EnableEurekaClient 表明自己是一個eurekaclient.

package cn.zhiwei;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
@EnableEurekaClient//表明自己是個客戶端
public class ServiceHiApplication {

	public static void main(String[] args) {
		SpringApplication.run(ServiceHiApplication.class, args);
	}

	@Value("${server.port}")
	String port;
	@RequestMapping("/hi")
	public String home(@RequestParam String name) {
		return "hi "+name+",i am from port:" +port;
	}
}

  僅僅@EnableEurekaClient是不夠的,還需要在配置文件中註明自己的服務註冊中心的地址,application.yml配置文件如下:

eureka:
  client:
    serviceUrl:
    #註冊地址
      defaultZone: http://localhost:8761/eureka/
server:
  port: 8763
spring:
  application:
  #spring.application.name,這個很重要,這在以後的服務與服務之間相互調用一般都是根據這個name
    name: service-hi

啟動工程,打開http://localhost:8761 ,即eureka server 的網址:

技術分享圖片

你會發現多了一個服務

你會發現一個服務已經註冊在服務中了,服務名為SERVICE-HI ,端口為7863

這時打開 http://localhost:8763/hi?name=威哥 ,你會在瀏覽器上看到 :

hi 威哥,i am from port:8763

  

spring cloud註冊服務與發現(踩著坑往前爬)