Spring Cloud(一):發現和註冊服務(eureka)

最近的項目需要將原來的項目重構爲Spring Boot項目,正好也能利用一些Spring Cloud的工具,就正好學習學習_(:з」∠*)_。
首先需要新建2個Spring Boot項目,一個作爲服務註冊中心,一個作爲服務生產者/提供者。
如下
在這裏插入圖片描述
PS.我這裏用的是Spring Boot 2.X版本

一.服務註冊中心
新建Spring Boot項目SpringCloudServiceCenter
(1)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>com.service.center</groupId>
   <artifactId>SpringCloundServiceCenter</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   <packaging>jar</packaging>

   <name>SpringCloundServiceCenter</name>
   <description>com.service.center</description>

   <parent>
   	<groupId>org.springframework.boot</groupId>
   	<artifactId>spring-boot-starter-parent</artifactId>
   	<version>2.1.0.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>Greenwich.M1</spring-cloud.version>
   </properties>

   <dependencies>
   	<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>

   <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-milestones</id>
   		<name>Spring Milestones</name>
   		<url>https://repo.spring.io/milestone</url>
   		<snapshots>
   			<enabled>false</enabled>
   		</snapshots>
   	</repository>
   </repositories>
</project>

(2)application.properties配置

#服務端口
server.port=8761
#服務名稱
eureka.instance.hostname=serviceCenter
#禁止本身註冊
eureka.client.register-with-eureka=false
#禁止本身註冊
eureka.client.fetch-registry=false
#服務中心地址
eureka.client.service-url.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/

(3)SpringCloundServiceCenterApplication.java啓動文件中添加@EnableEurekaServer

package com.service.center;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication
@EnableEurekaServer
public class SpringCloundServiceCenterApplication {

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

來表明該服務爲服務發現和註冊中心
整體結構
在這裏插入圖片描述
(4)配置完成後啓動,然後訪問http://localhost:8761/ 就能進入eureka server監控界面,可以看到紅框中的信息,現在還沒有服務註冊。
在這裏插入圖片描述


二.服務生產者
新建spring boot項目SpringCloudServiceI
(1)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>com.my.serviceI</groupId>
   <artifactId>SpringCloundServiceI</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   <packaging>jar</packaging>

   <name>SpringCloundServiceI</name>
   <description>com.my.serviceI</description>

   <parent>
   	<groupId>org.springframework.boot</groupId>
   	<artifactId>spring-boot-starter-parent</artifactId>
   	<version>2.1.0.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>Greenwich.M1</spring-cloud.version>
   </properties>

   <dependencies>
   	<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>
   	<dependency>
   		<groupId>org.springframework.boot</groupId>
   		<artifactId>spring-boot-starter-test</artifactId>
   		<scope>test</scope>
   	</dependency>
   </dependencies>

   <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-milestones</id>
   		<name>Spring Milestones</name>
   		<url>https://repo.spring.io/milestone</url>
   		<snapshots>
   			<enabled>false</enabled>
   		</snapshots>
   	</repository>
   </repositories>
</project>

(2)application.properties配置

server.servlet.context-path=/myServiceI 
server.port=8762
spring.application.name=myServiceI  #服務名,在註冊時所用
eureka.client.service-url.defautZone=http://serviceCenter:8761/eureka/

(3)在啓動文件SpringCloundServiceIApplication.java中添加@EnableEurekaClient

package com.my.serviceI;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@SpringBootApplication
@EnableEurekaClient
public class SpringCloundServiceIApplication {

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

(4)新建一個controller文件,提供服務
例ServiceApiController.java

package com.my.serviceI.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping(value="/Api")
public class ServiceApiController {
   @ResponseBody
   @RequestMapping(value="/getInfo")
   public String getInfo() {
   	return "123+";
   }
}

整體結構如下
在這裏插入圖片描述
可以啓動測試下是否能訪問接口服務,因爲spring boot會內置一個tomcat容器,且在啓動時會省略掉項目名,所以我這裏在application.properties中配置server.servlet.context-path=/myServiceI 來把項目名加上了,所以現在接口的訪問路徑爲http://localhost:8762/myServiceI/Api/getInfo
在這裏插入圖片描述
然後刷新下eureka server監控界面,就能看到剛纔的服務了,能從信息中得知服務的名字和端口號,上面紅條是另一個服務。
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章