在学习《史上最简单的Spring Cloud教程》时突发奇想,把原来的项目转为微服务
记录学习历程。
1.首先创建一个Maven主工程,在pom文件中添加模块
本机Eclipse中安装了STS即(Spring Tool Suite),在Eclipse中Help->Eclipse MarketPlace->搜索Spring
可以快速创建SpringBoot应用
先创建一个SpringBoot项目,pom.xml添加SpringCloud版本Finchley,如下
<?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.damionew</groupId>
<artifactId>Damionew</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<name>Damionew</name>
<description>SpringCloud主Maven项目</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<!-- 引入SpringBoot模块 -->
<modules>
<module>damionew-eureka</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</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2.创建两个模块,Eureka服务注册中心和Eureka客户端进行测试
右键主项目,选择new->other->收入Maven Module搜索添加
如果出现
The parent project must have a packaging type of POM
则是因为在主项目中packing属性不是pom,修改即可
next->next->next
生成pom,整理并添加eureka-server依赖
发现会报'dependencies.dependency.version' is missing for xxx的错误,检查后找到原因是:在主项目的pom.xml中没有添加
<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>
进行依赖版本控制,添加后解决
在服务注册中心的启动类中添加@EnableEurekaServer以开启SpringCloud Eureka Server
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class DamionewEurekaApplication {
public static void main(String[] args) {
SpringApplication.run(DamionewEurekaApplication.class, args);
}
}
包名不能有-,稍作调整,调整为SpringBoot项目结构即可
此时项目结构如图
配置application.properties
# 应用程序名称
#spring.application.name=eureka-server
# 应用端口
server.port=8001
spring.freemarker.prefer-file-system-access=false
# 服务注册中心配置
# 服务注册中心实例的主机名
eureka.instance.hostname=localhost
# 是否向服务注册中心注册自己
eureka.client.register-with-eureka=false
# 是否检索服务
eureka.client.fetch-registry=false
# 服务中心的配置内容,指定服务注册中心的位置
eureka.client.service-url.defaultZone:http://${eureka.instance.hostname}:${server.port}/eureka/
但是当启动项目后,访问localhost:8001时报404
搞了很久不得其解
先试着做了一下Eureka Client,发现可以找到服务注册中心,只是服务注册中心无法进入管理页面
如下
pom.xml,因为加了一个Controller测试所以加了-web的依赖,另外此处引用Eureka的客户端依赖
<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>com.damionew</groupId>
<artifactId>Damionew-Hi</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Damionew-Hi</name>
<url>http://maven.apache.org</url>
<parent>
<groupId>com.damionew</groupId>
<artifactId>Damionew</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<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-web</artifactId>
</dependency>
</dependencies>
</project>
启动类
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.RestController;
@EnableEurekaClient
@SpringBootApplication
@RestController
public class HiApplication {
public static void main(String[] args) {
SpringApplication.run(HiApplication.class, args);
}
@Value("${server.port}")
String port;
@RequestMapping("/hi")
public String hi() {
return "hi";
}
}
配置文件,注意端口要和服务注册中心端口不同,并且eureka.client.service-url.defaultZone使用服务注册中心地址
# 应用程序名称
spring.application.name=eureka-server
# 应用端口
server.port=8002
eureka.client.service-url.defaultZone:http://localhost:8001/eureka/
启动Eureka和Hi项目后可以通过控制台查看
Hi项目打印
2018-09-13 17:18:39.698 INFO 20064 --- [nfoReplicator-0] com.netflix.discovery.DiscoveryClient : DiscoveryClient_EUREKA-SERVER/localhost:eureka-server:8002: registering service...
2018-09-13 17:18:39.835 INFO 20064 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8002 (http) with context path ''
2018-09-13 17:18:39.839 INFO 20064 --- [ main] .s.c.n.e.s.EurekaAutoServiceRegistration : Updating port to 8002
2018-09-13 17:18:39.846 INFO 20064 --- [ main] com.hi.HiApplication : Started HiApplication in 12.78 seconds (JVM running for 14.074)
2018-09-13 17:18:39.924 INFO 20064 --- [nfoReplicator-0] com.netflix.discovery.DiscoveryClient : DiscoveryClient_EUREKA-SERVER/localhost:eureka-server:8002 - registration status: 204
如果没有连接到服务注册中心,会连续报错,而服务注册中心有心跳检测功能,每隔几秒就会检测一次
com.netflix.discovery.shared.transport.TransportException: Cannot execute request on any known server
at com.netflix.discovery.shared.transport.decorator.RetryableEurekaHttpClient.execute(RetryableEurekaHttpClient.java:112) ~[eureka-client-1.9.2.jar:1.9.2]
at com.netflix.discovery.shared.transport.decorator.EurekaHttpClientDecorator.getDelta(EurekaHttpClientDecorator.java:149) ~[eureka-client-1.9.2.jar:1.9.2]
at com.netflix.discovery.shared.transport.decorator.EurekaHttpClientDecorator$7.execute(EurekaHttpClientDecorator.java:152) ~[eureka-client-1.9.2.jar:1.9.2]
at com.netflix.discovery.shared.transport.decorator.SessionedEurekaHttpClient.execute(SessionedEurekaHttpClient.java:77) ~[eureka-client-1.9.2.jar:1.9.2]
at com.netflix.discovery.shared.transport.decorator.EurekaHttpClientDecorator.getDelta(EurekaHttpClientDecorator.java:149) ~[eureka-client-1.9.2.jar:1.9.2]
at com.netflix.discovery.DiscoveryClient.getAndUpdateDelta(DiscoveryClient.java:1085) [eureka-client-1.9.2.jar:1.9.2]
at com.netflix.discovery.DiscoveryClient.fetchRegistry(DiscoveryClient.java:967) [eureka-client-1.9.2.jar:1.9.2]
at com.netflix.discovery.DiscoveryClient.refreshRegistry(DiscoveryClient.java:1471) [eureka-client-1.9.2.jar:1.9.2]
at com.netflix.discovery.DiscoveryClient$CacheRefreshThread.run(DiscoveryClient.java:1438) [eureka-client-1.9.2.jar:1.9.2]
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [na:1.8.0_144]
at java.util.concurrent.FutureTask.run(FutureTask.java:266) [na:1.8.0_144]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [na:1.8.0_144]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [na:1.8.0_144]
at java.lang.Thread.run(Thread.java:748) [na:1.8.0_144]
但是没有Eureka管理页面真的很难受
修改了一下pom文件,将spring改为1.X且Cloud改为Brixton.SR5后管理页面就出来了。
到家以后换了个电脑发现,用原来的版本就可以显示
猜测是有些jar包还是使用的以前仓库中的,没有更新,导致不匹配