创建注册管理中心
在模块的项目配置pom.xml文件中引入“eureka-server”的依赖
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency>
遇到的问题:Cannot resolve org.springframework.cloud:spring-cloud-starter-netflix-eure
解决办法:刷新maven
创建启动程序
package org.example; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @EnableEurekaServer @SpringBootApplication public class EurekaApplication { public static void main(String[] args){ SpringApplication.run(EurekaApplication.class,args); } }
遇到的问题:
1、Cannot resolve method 'run' in 'SpringBootApplication'
run方法是SpringApplication的,但是引用注解的时候引用@SpringBootApplication
2、类EurekaApplication 要在org.example包下面,而不是直接在java下面
3、注意springboot和spring cloud的版本对应问题
SpringCloud版本 SpringBoot版本 2022.0.0-M2 Spring Boot >=3.0.0-M2 and <3.1.0-M1 2022.0.0-M1 Spring Boot >=3.0.0-M1 and <3.0.0-M2 2021.0.3 Spring Boot >=2.6.1 and <3.0.0-M1 2021.0.0-RC1 Spring Boot >=2.6.0-RC1 and <2.6.1 2021.0.0-M3 Spring Boot >=2.6.0-M3 and <2.6.0-RC1 2021.0.0-M1 Spring Boot >=2.6.0-M1 and <2.6.0-M3 2020.0.5 Spring Boot >=2.4.0.M1 and <2.6.0-M1 Hoxton.SR12 Spring Boot >=2.2.0.RELEASE and <2.4.0.M1 Hoxton.BUILD-SNAPSHOT Spring Boot >=2.2.0.BUILD-SNAPSHOT Hoxton.M2 Spring Boot >=2.2.0.M4 and <=2.2.0.M5 Greenwich.BUILD-SNAPSHO Spring Boot >=2.1.9.BUILD-SNAPSHOT and <2.2.0.M4 Greenwich.SR2 Spring Boot >=2.1.0.RELEASE and <2.1.9.BUILD-SNAPSHOT Greenwich.M1 Spring Boot >=2.1.0.M3 and <2.1.0.RELEASE Finchley.BUILD-SNAPSHOT Spring Boot >=2.0.999.BUILD-SNAPSHOT and <2.1.0.M3 Finchley.SR4 Spring Boot >=2.0.3.RELEASE and <2.0.999.BUILD-SNAPSHOT Finchley.RC2 Spring Boot >=2.0.2.RELEASE and <2.0.3.RELEASE Finchley.RC1 Spring Boot >=2.0.1.RELEASE and <2.0.2.RELEASE Finchley.M9 Spring Boot >=2.0.0.RELEASE and <=2.0.0.RELEASE Finchley.M7 Spring Boot >=2.0.0.RC2 and <=2.0.0.RC2 Finchley.M6 Spring Boot >=2.0.0.RC1 and <=2.0.0.RC1 Finchley.M5 Spring Boot >=2.0.0.M7 and <=2.0.0.M7 Finchley.M4 Spring Boot >=2.0.0.M6 and <=2.0.0.M6 Finchley.M3 Spring Boot >=2.0.0.M5 and <=2.0.0.M5 Finchley.M2 Spring Boot >=2.0.0.M3 and <2.0.0.M5 Edgware.SR5 1.5.20.RELEASE Edgware.SR5 1.5.16.RELEASE Edgware.RELEASE 1.5.9.RELEASE Dalston.RC1 1.5.2.RELEASE |
工程的配置文件application.yml
server: port: 8761 eureka: instance: hostname: localhost client: register-with-eureka: false fetch-registry: false service-url: defaultZone: http://localhost:${server.port}/eureka/ spring.cloud.config.discovery.enable: true
register-with-eureka表示是否将本实例在Eureka Server中进行注册,默认为true。
fetch-registry表示是否从Eureka Server中取得本实例的注册信息,默认为true。
如果将注册管理中心进行多实例发布,可以将register-with-eureka设置为true
defaultZone表示从默认分区中访问Eureka Server
微服务怎样使用注册管理中心
在项目中增加Eureka的相关依赖引用:
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-zuul</artifactId> </dependency>
引用了zuul组件,zuul组件提供智能路由的服务。引用zuul组件后,Ribbon组件也会被自动引用进来,他提供了负载均衡功能。
在微服务的主程序中增加如下三个注解
@EnableDiscoveryClient//客户端服务发现功能 @EnableZuulProxy//启用ZuulPorxy代理以使用智能路由的功能 @EnableHystrix//使用Hystrix的断路由功能以提供服务降级和容错的机制
在工程相关的模块增加以下配置,即可接入注册管理中心
spring: application: name: catalogapi//实例名称,这个名称必须保证唯一性
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
instance: hostname: localhost prefer-ip-address: true//设置为true以IP地址注册到服务中心,相互注册使用IP地址
标签:架构设计,Spring,Boot,Finchley,eureka,---,2.0,cloud From: https://www.cnblogs.com/luckLjh/p/17632293.html