在本节之前,我们先把各个模块都会用到的类CommonResult剥离出来成一个单独的模块,参考SpringCloud 从入门到精通 03--- 自动生成数据模型,创建一个公共模块cloud-common,然后把CommonResult放在此模块中,同时移除cloud-payment-8001 cloud-order-8000 中都引用到的CommonResult,详细过程不再赘述。剥离后项目如图
为了方便其他模块引入cloud-common中的公用类,记得要在父pom.xml的dependencyManagement中添加cloud-common,然后cloud-payment-8001 和 cloud-order-8000 都添加对`cloud-common`的引用,并删除自己模块下的CommonResult
<dependency> <groupId>com.felix</groupId> <artifactId>cloud-common</artifactId> <version>1.0-SNAPSHOT</version></dependency>
复制代码
再创建一个模块cloud-eureka-server-7000,作为 Eureka 的服务端,如下图
配置文件如下
server: port: 7000
eureka: instance: hostname: localhost client: register-with-eureka: false #是否注册自己到注册中心 fetch-registry: false #是否需要检索服务 service-url: defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
复制代码
创建一个类EurekaServerApplication
package com.felix.eureka;
import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication@EnableEurekaServerpublic class EurekaServerApplication { public static void main(String[] args) { SpringApplication.run(EurekaServerApplication.class,args); }}
复制代码
接下来就可以启动Eureka服务端了,当然,不出意外的话会出现如下错误
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'configurationPropertiesBeans' defined in class path resource [org/springframework/cloud/autoconfigure/ConfigurationPropertiesRebinderAutoConfiguration.class]: Post-processing of merged bean definition failed; nested exception is java.lang.IllegalStateException: Failed to introspect Class [org.springframework.cloud.context.properties.ConfigurationPropertiesBeans] from ClassLoader [sun.misc.Launcher$AppClassLoader@18b4aac2] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:586) ~[spring-beans-5.3.2.jar:5.3.2] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:531) ~[spring-beans-5.3.2.jar:5.3.2] at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335) ~[spring-beans-5.3.2.jar:5.3.2]
复制代码
这是由于SpringBoot和SpringCloud的版本不匹配,修改根`pom.xml`中的版本
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>2.3.2.RELEASE</version> <type>pom</type> <scope>import</scope></dependency><dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Hoxton.SR8</version> <type>pom</type> <scope>import</scope></dependency>
复制代码
修改完以后,Eureka服务器就可以启动起来了,直接访问本地地址localhost:7000即可打开Eureka的界面
评论