- 服务发现服务(microservice-eureka-server)
- 用户服务提供者(microservice-provider-user)
- 电影服务消费者(microservice-consumer-movie)
工具及软件
- JDK:建议使用 JDK 1.8
- Spring Boot:实验使用 Spring Boot 2.0.7.RELEASE 版本。
- Spring Cloud:实验使用 Spring Cloud Finchley.SR2 版本。
- Maven:实验使用 Maven 3.6.0 构建项目
Failed to execute goal org.apache.maven.plugins:maven-archetype-plugin:3.2.0:generate (default-cli) on project standalone-pom: The desired archetype does not exist (org.springframework.boot:spring-boot-starter-parent:2.0.7.RELEASE)
1 2 3 4 5 6 7 8 9 10 | <dependencies> <!-- https: //mvnrepository.com/artifact/org.apache.maven.plugins/maven-archetype-plugin --> <dependency> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-archetype-plugin</artifactId> <version> 3.2 . 0 </version> </dependency> </dependencies> |
Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
1 2 3 4 5 | <properties> <maven.compiler.source> 8 </maven.compiler.source> <maven.compiler.target> 8 </maven.compiler.target> <project.build.sourceEncoding>UTF- 8 </project.build.sourceEncoding> </properties> |
JAR will be empty - no content was marked for inclusion!
springbootapplication注解 不能导入
1 2 3 | File—>Settings(Ctrl+Alt+S)—>Build—>Build Tools—>Maven 是不是idea的maven仓库和系统配置的maven仓库是不是不相同 |
pom.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | <?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.lzj1234</groupId> <artifactId>findservice</artifactId> <version> 1.0 -SNAPSHOT</version> <properties> <maven.compiler.source> 8 </maven.compiler.source> <maven.compiler.target> 8 </maven.compiler.target> <project.build.sourceEncoding>UTF- 8 </project.build.sourceEncoding> </properties> <dependencies> <!-- https: //mvnrepository.com/artifact/org.apache.maven.plugins/maven-archetype-plugin --> <dependency> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-archetype-plugin</artifactId> <version> 3.2 . 0 </version> </dependency> <!-- https: //mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-netflix-eureka-server --> <!-- https: //mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-netflix-eureka-server --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> <version> 3.0 . 1 </version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <version> 2.4 . 2 </version> </dependency> </dependencies> <!-- 引入spring cloud的依赖,不能少,主要用来管理Spring Cloud生态各组件的版本 --> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Finchley.SR2</version> <type>pom</type> <scope> import </scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version> 2.4 . 2 </version> </plugin> </plugins> </build> </project> |
App.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | package com.lzj1234; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @SpringBootApplication @EnableEurekaServer public class App { public static void main(String[] args) { SpringApplication.run(App. class ,args); } } |
resources/application.yml
server:
port: 8080
eureka:
instance:
hostname: localhost
client:
registerWithEureka: false
fetchRegistry: false
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
1 2 3 4 | registerWithEureka: 表示是否需要将自身也加入 Eureka Server 中,默认为 true 。当前实例本身就是 Eureka Server,故设置为 false 。 fetchRegistry:是否需要从其他 Eureka Server 获取注册信息,默认为 true 。本次实验采用的是一个单点 Server,无需同步其他 Server 的注册信息,故设置为 false 。 serviceUrl.defaultZone:当前 Eureka Server 的交互地址,多个地址可用 ',' 号隔开。 若有需要自定义配置,如搭建高可用的 Eureka Server,可查看官方文档:https: //cloud.spring.io/spring-cloud-netflix/multi/multi_spring-cloud-eureka-server.html |
mvn spring-boot:run
1 2 3 4 5 6 7 8 9 | [ERROR] Failed to execute goal org.springframework.boot:spring-boot-maven-plugin: 2.4 . 2 :run ( default -cli) on project findservice: Application finished with exit code: 1 -> [Help 1 ] [ERROR] 2021 - 02 - 19 14 : 22 : 20.229 ERROR 12132 --- [ main] o.s.b.web.embedded.tomcat.TomcatStarter : Error starting Tomcat context. Exception: org.springframework.beans.factory.BeanC reationException. Message: Error creating bean with name 'formContentFilter' defined in class path resource [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguratio n. class ]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.boot.web. servlet.filter.OrderedFormContentFilter]: Factory method 'formContentFilter' threw exception; nested exception is org.springframework.beans.BeanInstantiationException: Failed to inst antiate [com.fasterxml.jackson.datatype.jsr310.JavaTimeModule]: Constructor threw exception; nested exception is java.lang.NoClassDefFoundError: com/fasterxml/jackson/datatype/jsr310 /ser/ZoneIdSerializer<br><br><br> @EnableAutoConfiguration <br> @SpringBootConfiguration <dependency><br> <groupId>org.springframework.boot</groupId><br> <artifactId>spring-boot-starter-web</artifactId><br> <version> 2.4 . 2 </version><br></dependency><dependency><br> <groupId>org.springframework.cloud</groupId><br> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId><br> <version> 2.2 . 0 .RELEASE</version><br></dependency><br><br>应该是这个依赖包的问题<br>依赖冲突<br>可以使用parent标签 |
org.springframework.context.ApplicationContextException: Unable to start web server; nested exception is org.springframework.boot.web.server.WebServerException: Unable to start embed
ded Tomcat
o.s.b.web.embedded.tomcat.TomcatStarter : Error starting Tomcat context. Exception: org.springframework.beans.factory.BeanCre
ationException. Message: Error creating bean with name 'formContentFilter' defined in class path resource [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration.cl
ass]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.boot.web.servle
t.filter.OrderedFormContentFilter]: Factory method 'formContentFilter' threw exception; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate
[com.fasterxml.jackson.datatype.jsr310.JavaTimeModule]: Constructor threw exception; nested exception is java.lang.NoClassDefFoundError: com/fasterxml/jackson/datatype/jsr310/ser/ZoneI
dSerializer
java.util.concurrent.ExecutionException: org.apache.catalina.LifecycleException: Failed to start component [NonLoginAuthenticator[StandardEngine[Tomcat].StandardHost[localhost].TomcatE
mbeddedContext[]]]
The class hierarchy was loaded from the following locations:
javax.servlet.ServletContext: file:/D:/kaifa/reposmaven/javax/servlet/servlet-api/2.5/servlet-api-2.5.jar
Action:
Correct the classpath of your application so that it contains a single, compatible version of javax.servlet.ServletContext
删除jar包
The method's class, javax.servlet.ServletContext, is available from the following locations:
jar:file:/D:/kaifa/reposmaven/javax/servlet/servlet-api/2.5/servlet-api-2.5.jar!/javax/servlet/ServletContext.class
jar:file:/D:/kaifa/reposmaven/org/apache/tomcat/embed/tomcat-embed-core/9.0.41/tomcat-embed-core-9.0.41.jar!/javax/servlet/ServletContext.class
The class hierarchy was loaded from the following locations:
javax.servlet.ServletContext: file:/D:/kaifa/reposmaven/javax/servlet/servlet-api/2.5/servlet-api-2.5.jar
Action:
Correct the classpath of your application so that it contains a single, compatible version of javax.servlet.ServletContext
[INF
Description:
An attempt was made to call a method that does not exist. The attempt was made from the following location:
org.springframework.context.support.GenericApplicationContext.setApplicationStartup(GenericApplicationContext.java:165)
The following method did not exist:
org.springframework.beans.factory.support.DefaultListableBeanFactory.setApplicationStartup(Lorg/springframework/core/metrics/ApplicationStartup;)V
The method's class, org.springframework.beans.factory.support.DefaultListableBeanFactory, is available from the following locations:
jar:file:/D:/kaifa/reposmaven/org/springframework/spring-beans/5.2.1.RELEASE/spring-beans-5.2.1.RELEASE.jar!/org/springframework/beans/factory/support/DefaultListableBeanFactory.cl
ass
The class hierarchy was loaded from the following locations:
org.springframework.beans.factory.support.DefaultListableBeanFactory: file:/D:/kaifa/reposmaven/org/springframework/spring-beans/5.2.1.RELEASE/spring-beans-5.2.1.RELEASE.jar
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory: file:/D:/kaifa/reposmaven/org/springframework/spring-beans/5.2.1.RELEASE/spring-beans-5.2.1.RELEASE.ja
r
org.springframework.beans.factory.support.AbstractBeanFactory: file:/D:/kaifa/reposmaven/org/springframework/spring-beans/5.2.1.RELEASE/spring-beans-5.2.1.RELEASE.jar
org.springframework.beans.factory.support.FactoryBeanRegistrySupport: file:/D:/kaifa/reposmaven/org/springframework/spring-beans/5.2.1.RELEASE/spring-beans-5.2.1.RELEASE.jar
org.springframework.beans.factory.support.DefaultSingletonBeanRegistry: file:/D:/kaifa/reposmaven/org/springframework/spring-beans/5.2.1.RELEASE/spring-beans-5.2.1.RELEASE.jar
org.springframework.core.SimpleAliasRegistry: file:/D:/kaifa/reposmaven/org/springframework/spring-core/5.3.3/spring-core-5.3.3.jar
Action:
Correct the classpath of your application so that it contains a single, compatible version of org.springframework.beans.factory.support.DefaultListableBeanFactory
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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.7.RELEASE</version>
<relativePath/>
</parent>
<groupId>com.lzj1234</groupId>
<artifactId>findservice</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!-- 引入 Netflix 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>
<!-- 引入spring cloud的依赖,不能少,主要用来管理Spring Cloud生态各组件的版本 -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.SR2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<!-- 添加spring-boot的maven插件,不能少,打jar包时得用 -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
运行:
mvn spring-boot:run
注册微服务至 Eureka Server
更改服务提供者
添加依赖
pom.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 | <?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>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version> 2.0 . 7 .RELEASE</version> <relativePath/> </parent> <groupId>com.lzj1234</groupId> <artifactId>microservice-provider-user</artifactId> <version> 1.0 -SNAPSHOT</version> <properties> <java.version> 1.8 </java.version> </properties> <dependencies> <!-- https: //mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-jpa --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> <!-- <version> 2.4 . 2 </version>--> </dependency> <!-- https: //mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <!-- <version> 2.4 . 2 </version>--> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> <!-- <version> 2.4 . 2 </version>--> </dependency> <!-- 引入H2数据库,一种内嵌的数据库,语法类似MySQL --> <!-- https: //mvnrepository.com/artifact/com.h2database/h2 --> <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <version> 1.4 . 200 </version> <!-- <scope>test</scope>--> </dependency> <!-- 新增依赖 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <!-- https: //mvnrepository.com/artifact/org.projectlombok/lombok --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version> 1.18 . 18 </version> <scope>provided</scope> </dependency> <!-- https: //mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-test --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <!-- <version> 2.4 . 2 </version>--> <scope>test</scope> </dependency> <!-- https: //mvnrepository.com/artifact/org.apache.maven.plugins/maven-clean-plugin --> <dependency> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-clean-plugin</artifactId> <version> 2.5 </version> </dependency> </dependencies> <!-- 引入spring cloud的依赖,不能少,主要用来管理Spring Cloud生态各组件的版本 --> <dependencyManagement> <dependencies> <!-- https: //mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-dependencies --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Finchley.SR2</version> <type>pom</type> <scope> import </scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <!-- 添加spring-boot的maven插件,不能少,打jar包时得用 --> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <!-- <version> 2.4 . 2 </version>--> </plugin> <plugin> <!-- https: //mvnrepository.com/artifact/org.apache.maven.plugins/maven-compiler-plugin --> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version> 2.5 . 1 </version> <configuration> <source> 1.8 </source> <target> 1.8 </target> </configuration> </plugin> </plugins> </build> </project> |
添加注解
app.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | package com.src; import org.springframework.boot.ApplicationRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.context.annotation.Bean; import java.math.BigDecimal; import java.util.stream.Stream; @SpringBootApplication @EnableEurekaClient public class App { public static void main(String[] args) { SpringApplication.run(App. class , args); } @Bean ApplicationRunner init(UserRepository repository){ return args -> { User user1 = new User(1L, "account1" , "张三" , 20 , new BigDecimal( 100.00 )); User user2 = new User(2L, "account2" , "李四" , 28 , new BigDecimal( 180.00 )); User user3 = new User(3L, "account3" , "王五" , 32 , new BigDecimal( 280.00 )); Stream.of(user1, user2, user3) .forEach(repository::save); }; } } |
配置文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | server: # 指定Tomcat端口 port: 8000 spring: application: # 给这个服务取名叫做:microservice-provider-user name: microservice-provider-user jpa: # 让hibernate打印执行的SQL show-sql: true logging: level: root: INFO # 配置日志级别,让hibernate打印出执行的SQL参数 org.hibernate: INFO org.hibernate.type.descriptor.sql.BasicBinder: TRACE org.hibernate.type.descriptor.sql.BasicExtractor: TRACE #management: # endpoint: # health: # # 是否展示健康检查详情 # show-details: always management: endpoints: web: exposure: include: '*' health: # 是否展示健康检查详情 show-details: always # 新增配置 eureka: client: serviceUrl: defaultZone: http: //localhost:8080/eureka/ instance: prefer-ip-address: true |
简单介绍一下以上配置:
- eureka.client.serviceUrl.defaultZone:表示 Eureka Server 的交互地址,此前已经在 Eureka Server 中配置该 defaultZone。
- eureka.instance.prefer-ip-address:表示将自身服务器的 IP 地址注册到 Eureka Server 中。如果不配置或者设置为 false,则将会将服务器的 hostname 注册到 Eureka Server 中。
运行
mvn spring-boot:run
更改消费者
pom.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 | <?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.shiyanlou</groupId> <artifactId>microservice-consumer-movie</artifactId> <version> 1.0 -SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version> 2.0 . 7 .RELEASE</version> <relativePath/> </parent> <properties> <java.version> 1.8 </java.version> </properties> <dependencies> <!-- https: //mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-jpa --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> <!-- <version> 2.4 . 2 </version>--> </dependency> <!-- https: //mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <!-- <version> 2.4 . 2 </version>--> </dependency> <!-- 引入H2数据库,一种内嵌的数据库,语法类似MySQL --> <!-- https: //mvnrepository.com/artifact/com.h2database/h2 --> <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <version> 1.4 . 200 </version> <!-- <scope>test</scope>--> </dependency> <!-- https: //mvnrepository.com/artifact/org.projectlombok/lombok --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version> 1.18 . 18 </version> <scope>provided</scope> </dependency> <!-- https: //mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-test --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <!-- <version> 2.4 . 2 </version>--> <scope>test</scope> </dependency> <!-- https: //mvnrepository.com/artifact/org.apache.maven.plugins/maven-clean-plugin --> <dependency> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-clean-plugin</artifactId> <version> 2.5 </version> </dependency> <!-- 新增依赖 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> </dependencies> <!-- 引入spring cloud的依赖,不能少,主要用来管理Spring Cloud生态各组件的版本 --> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Finchley.SR2</version> <type>pom</type> <scope> import </scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <!-- <version> 2.4 . 2 </version>--> </plugin> <plugin> <!-- https: //mvnrepository.com/artifact/org.apache.maven.plugins/maven-compiler-plugin --> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version> 2.5 . 1 </version> <configuration> <source> 1.8 </source> <target> 1.8 </target> </configuration> </plugin> </plugins> </build> </project> |
app.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | package com.shiyanlou; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.context.annotation.Bean; import org.springframework.web.client.RestTemplate; @SpringBootApplication @EnableEurekaClient public class App { public static void main(String[] args) { SpringApplication.run(App. class , args); } @Bean public RestTemplate restTemplate(){ return new RestTemplate(); } } |
配置文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | server: port: 8010 spring: application: name: microservice-consumer-movie logging: level: root: INFO # 配置日志级别,让hibernate打印出执行的SQL参数 org.hibernate: INFO org.hibernate.type.descriptor.sql.BasicBinder: TRACE org.hibernate.type.descriptor.sql.BasicExtractor: TRACE # 新增配置 eureka: client: serviceUrl: defaultZone: http: //localhost:8080/eureka/ instance: prefer-ip-address: true |
mvn spring-boot:run
1 2 3 4 5 | 2021 - 02 - 19 17 : 40 : 26.655 INFO 5252 --- [nfoReplicator- 0 ] com.netflix.discovery.DiscoveryClient : DiscoveryClient_MICROSERVICE-CONSUMER-MOVIE/DESKTOP-SRVQTV7:microservice-consumer-movie: 8010 : registering service... 2021 - 02 - 19 17 : 40 : 26.701 INFO 5252 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8010 (http) with context path '' 2021 - 02 - 19 17 : 40 : 26.702 INFO 5252 --- [ main] .s.c.n.e.s.EurekaAutoServiceRegistration : Updating port to 8010 2021 - 02 - 19 17 : 40 : 26.703 INFO 5252 --- [nfoReplicator- 0 ] com.netflix.discovery.DiscoveryClient : DiscoveryClient_MICROSERVICE-CONSUMER-MOVIE/DESKTOP-SRVQTV7:microservice-consumer-movie: 8010 - registration status: 204 2021 - 02 - 19 17 : 40 : 26.705 INFO 5252 --- [ main] com.shiyanlou.App : Started App in 12.194 seconds (JVM running for 14.033 ) |
查看注册结果
第一是学会如何创建一个 Eureka Server,第二是将微服务注册到该 Server 上。但是仍然没有解决一个问题,那就是服务消费者请求服务提供者的时候,地址硬编码问题
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 使用C#创建一个MCP客户端
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 按钮权限的设计及实现