SpringCloud:(一)服務註冊與發現

最近跟着方誌明老師學習SpringCloud,博客地址以下:java

https://blog.csdn.net/forezp/article/details/81040925web

本身也跟着擼了一遍,紙上得來終覺淺,絕知此事要躬行,如下是本身擼的時候的總結&注意事項:spring

(一)建立maven項目apache

打開idea瀏覽器

File—new—project—mavenspringboot

建立maven項目app

Pom文件導入相應服務的依賴maven

<?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>springcloud</groupId>
<artifactId>clouddemo</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>

<name>clouddemo</name>
<description>Demo project for Spring Boot</description>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.1.RELEASE</version>
<relativePath/>
</parent>

<modules>
<module>eureka-server</module>
<module>service-hi</module>
</modules>

<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.RELEASE</spring-cloud.version>
</properties>

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

</project>
注:
version版本升級到了2.1.1,否則會報

java.lang.AbstractMethodError: null at org.springframework.core.OrderComparator.findOrder等錯誤
<version>2.1.1.RELEASE</version>
(二)建立2個model工程做爲服務註冊中心:Eureka server & Eureka client

2.1Eureka serveride

2.1.1spring-boot

父項目—new—module—spring initializr—cloud discovery—eureka server

 

繼承父pom文件,引入spring—cloud—starter—Netflix—eureka—server的依賴

<?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>
<parent>
<groupId>springcloud</groupId>
<artifactId>clouddemo</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>

<groupId>com.springcloud</groupId>
<artifactId>eureka-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>eureka-server</name>
<description>Demo project for Spring cloud</description>

<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR1</spring-cloud.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>

</dependencies>

</project>
2.1.2
啓動一個服務註冊中心,須要一個註解@EnableEurekaServer,這個註解須要在springboot工程的啓動application類上加:
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {

public static void main(String[] args) {

SpringApplication.run(EurekaServerApplication.class, args);
}

}
2.1.3配置文件appication.yml,指定server:
server:
port: 8761

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/

spring:
application:
name: eurka-server
完成之後,啓動服務,打開啓動工程,打開瀏覽器訪問:

http://localhost:8761
頁面,顯示沒有服務--沒有註冊服務固然不可能有服務被發現了。。。。

 

 

2.2 Eureka client--server-hi

步驟同2.1,建立module項目(服務提供者)

pom文件

<?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>
<parent>
<groupId>springcloud</groupId>
<artifactId>clouddemo</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>

<groupId>com.eureka.client</groupId>
<artifactId>service-hi</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>service-hi</name>
<description>Demo project for Spring Boot</description>

<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR1</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-web</artifactId>
</dependency>
</dependencies>


<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>
2.2.1啓動類添加註解,註解@EnableEurekaClient 代表本身是一個eurekaclient

@SpringBootApplication
@EnableEurekaClient
@RestController
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(value = "name", defaultValue = "clouddemo") String name) {
return "hi " + name + " ,i am from port:" + port;
}

}


2.2.2在配置文件中註明本身的服務註冊中心的地址
 
server:
port: 8762

spring:
application:
name: service-hi

eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/

3.1啓動eureka-server服務,打開 http://localhost:8761
3.2啓動service-hi服務,訪問service-hi頁面,http://localhost:8762/hi?name=clouddemo

 



3.3刷新eureka-server訪問的頁面
服務已經註冊在服務中了,服務名爲SERVICE-HI ,端口爲7862
相關文章
相關標籤/搜索