做网站彩票代理多少钱啊,网站的绝对路径,营销型网站价格实惠,自适应手机模板前言
Eureka 是 Netflix 公司开发的一款开源的服务注册与发现组件。 Spring Cloud 使用 Spring Boot 思想为 Eureka 增加了自动化配置#xff0c;开发人员只需要引入相关依赖和注解#xff0c;就能将 Spring Boot 构建的微服务轻松地与 Eureka 进行整合。
1、Eureka 两大组…前言
Eureka 是 Netflix 公司开发的一款开源的服务注册与发现组件。 Spring Cloud 使用 Spring Boot 思想为 Eureka 增加了自动化配置开发人员只需要引入相关依赖和注解就能将 Spring Boot 构建的微服务轻松地与 Eureka 进行整合。
1、Eureka 两大组件
Eureka 采用 CSClient/Server客户端/服务器 架构它包括以下两大组件Eureka Server、Eureka Client
组件介绍Eureka ServerEureka 服务注册中心主要用于提供服务注册功能Eureka ClientEureka 客户端通常指的是微服务系统中各个微服务
2、Eureka 服务注册与发现 功能介绍服务注册中心Register Service它是一个 Eureka Server用于提供服务注册和发现功能。服务提供者Provider Service它是一个 Eureka Client用于提供服务。它将自己提供的服务注册到服务注册中心以供服务消费者发现。服务消费者Consumer Service它是一个 Eureka Client用于消费服务。它可以从服务注册中心获取服务列表调用所需的服务。
3、案例
3.1、创建主工程
名称SpringCloud
3.1.1、主工程pom.xml配置 ?xml version1.0 encodingUTF-8?
project xmlnshttp://maven.apache.org/POM/4.0.0 xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersionpackagingpom/packagingparentgroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-parent/artifactIdversion2.6.13/versionrelativePath/ !-- lookup parent from repository --/parentgroupIdcom.hqyj/groupIdartifactIddrp/artifactIdversion0.0.1-SNAPSHOT/versionnamedrp-parent/namedescriptionDemo project for Spring Boot/descriptionpropertiesmaven.compiler.source8/maven.compiler.sourcemaven.compiler.target8/maven.compiler.targetproject.build.sourceEncodingUTF-8/project.build.sourceEncodingmaven.compiler.source1.8/maven.compiler.sourcemaven.compiler.target1.8/maven.compiler.targetjunit.version4.12/junit.versionlog4j.version1.2.17/log4j.versionlombok.version1.16.18/lombok.version/propertiesdependencyManagementdependenciesdependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-dependencies/artifactIdversion2021.0.5/versiontypepom/typescopeimport/scope/dependency/dependencies/dependencyManagementbuildpluginsplugingroupIdorg.apache.maven.plugins/groupIdartifactIdmaven-compiler-plugin/artifactIdversion3.5.1/versionconfigurationsource${maven.compiler.source}/sourcetarget${maven.compiler.target}/target/configuration/plugin/plugins/build/project3.2、创建子公共模块common-api
3.2.1、添加module 3.2.2、pom.xml配置
?xml version1.0 encodingUTF-8?
project xmlnshttp://maven.apache.org/POM/4.0.0xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersionparentgroupIdcom.hqyj/groupIdartifactIdSpringCloud/artifactIdversion0.0.1-SNAPSHOT/version/parentartifactIdcommon-api/artifactIdpropertiesmaven.compiler.source8/maven.compiler.sourcemaven.compiler.target8/maven.compiler.targetproject.build.sourceEncodingUTF-8/project.build.sourceEncoding/propertiesdependenciesdependencygroupIdorg.projectlombok/groupIdartifactIdlombok/artifactId/dependency/dependencies/project
3.2.3、maven非springboot项目增加main入口
添加Main.java指定main入口防止Maven package / install打包失败
public class Main {public static void main(String[] args) {System.out.println(common-api);}}
3.3、创建Eureka注册中心模块eureka-server 3.3.1、配置pom.xml
?xml version1.0 encodingUTF-8?
project xmlnshttp://maven.apache.org/POM/4.0.0xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersionparentgroupIdcom.hqyj/groupIdartifactIdSpringCloud/artifactIdversion0.0.1-SNAPSHOT/version/parentartifactIdeureka-server/artifactIdpropertiesmaven.compiler.source8/maven.compiler.sourcemaven.compiler.target8/maven.compiler.targetproject.build.sourceEncodingUTF-8/project.build.sourceEncodingskip-teststrue/skip-tests/propertiesdependenciesdependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-test/artifactIdscopetest/scope/dependencydependencygroupIdorg.projectlombok/groupIdartifactIdlombok/artifactIdoptionaltrue/optional/dependencydependencygroupIdcom.github.enesusta/groupIdartifactIdspring-devtools/artifactIdversion1.0.1/versionoptionaltrue/optional/dependencydependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-starter-netflix-eureka-server/artifactId/dependency/dependencies/project
3.3.2、配置application.yml
在resource目录下新建application.yml文件
编辑application.yml文件添加eureka配置
server:port: 7001eureka:instance:hostname: localhost #eureka服务端的实例名称client:register-with-eureka: false #false表示不向注册中心注册自己。fetch-registry: false #false表示自己端就是注册中心我的职责就是维护服务实例并不需要去检索服务service-url:defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ #单机版服务注册中心
3.3.3、启动eureka-server
创建EurekaServerApplication.java启动文件 import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;SpringBootApplication
EnableEurekaServer
public class EurekaServerApplication {public static void main(String[] args) {SpringApplication.run(EurekaServerApplication.class,args);}
}3.3.3.1、编译eureka-server 3.3.3.2、运行EurekaServerApplication.java文件 启动http://localhost:7001/
3.4、创建用户服务模块user-service
3.4.1、配置pom.xml
?xml version1.0 encodingUTF-8?
project xmlnshttp://maven.apache.org/POM/4.0.0xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersionparentgroupIdcom.hqyj/groupIdartifactIdSpringCloud/artifactIdversion0.0.1-SNAPSHOT/version/parentartifactIduser-service/artifactIdpropertiesmaven.compiler.source8/maven.compiler.sourcemaven.compiler.target8/maven.compiler.targetproject.build.sourceEncodingUTF-8/project.build.sourceEncoding/propertiesdependenciesdependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependency!--devtools 开发工具--dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-devtools/artifactIdscoperuntime/scopeoptionaltrue/optional/dependency!--Spring Boot 测试--dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-test/artifactIdscopetest/scope/dependency!--junit 测试--dependencygroupIdjunit/groupIdartifactIdjunit/artifactIdversion4.12/version/dependency!-- 修改后立即生效热部署 --dependencygroupIdorg.springframework/groupIdartifactIdspringloaded/artifactIdversion1.2.8.RELEASE/version/dependencydependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-starter-netflix-eureka-client/artifactId/dependencydependencygroupIdcom.hqyj/groupIdartifactIdcommon-api/artifactIdversion0.0.1-SNAPSHOT/version/dependency/dependencies/project3.4.2、配置application.yml
server:port: 8001
spring:application:name: user-service #微服务名称
eureka:client: #将客户端注册到 eureka 服务列表内service-url:defaultZone: http://localhost:7001/eureka #这个地址是 7001注册中心在 application.yml 中暴露出来额注册地址 单机版3.4.3、启动user-service
创建UserApplication.java启动文件
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;SpringBootApplication
EnableDiscoveryClient
public class UserApplication {public static void main(String[] args) {SpringApplication.run(UserApplication.class,args);}
}3.4.3.1、编译user-service 3.4.3.2、运行UserApplication.java文件 3.4.3.3、测试
http://localhost:8001/user/userInfoList
3.5、查看编译后的包