当前位置: 首页> 汽车> 报价 > 网站建设哪家性价比高_如何自己写一个网页_软件外包网站_优化英文

网站建设哪家性价比高_如何自己写一个网页_软件外包网站_优化英文

时间:2025/7/9 5:57:33来源:https://blog.csdn.net/wmh_1234567/article/details/142317954 浏览次数: 0次
网站建设哪家性价比高_如何自己写一个网页_软件外包网站_优化英文

目录

Eureka

搭建Eureka Server

创建Eureka-server子模块

引入eureka-server依赖

项目构建插件

完善启动类

编写配置文件

启动服务并访问

服务注册

引入eureka-client依赖

完善配置文件

启动服务并访问

服务发现

引入依赖

完善配置文件

启动服务并访问

Eureka和Zookeeper的区别


Eureka

Eureka是Netflix OSS套件中关于服务注册和发现的解决⽅案. Spring Cloud对Eureka进⾏了集成, 并作为优先推荐⽅案进⾏宣传, 虽然⽬前Eureka 2.0已经停⽌维护, 新的微服务架构设计中, 也不再建议使⽤, 但是⽬前依然有⼤量公司的微服务系统使⽤Eureka作为注册中心。

Eureka主要分为两个部分:

Eureka Server: 作为注册中⼼Server端, 向微服务应⽤程序提供服务注册, 发现, 健康检查等能⼒.
Eureka Client: 服务提供者, 服务启动时, 会向Eureka Server 注册⾃⼰的信息(IP,端⼝,服务信息等),Eureka Server 会存储这些信息。

关于Eureka的学习, 主要包含以下三个部分:

1. 搭建Eureka Server
2. 将order-service, product-service 都注册到Eureka
3. order-service远程调⽤时, 从Eureka中获取product-service的服务列表, 然后进⾏交互

搭建Eureka Server

Eureka-server 是⼀个独⽴的微服务.

创建Eureka-server子模块

引入eureka-server依赖
<dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-server</artifactId></dependency>
</dependencies>
项目构建插件
<build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins>
</build>
完善启动类
package eureka;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {public static void main(String[] args) {SpringApplication.run(EurekaServerApplication.class, args);}
}
编写配置文件
server:port: 10010
spring:application:name: eureka-server
eureka:instance:hostname: localhostclient:fetch-registry: false # 表示是否从Eureka Server获取注册信息,默认为true.因为这是一个单点的Eureka Server,不需要同步其他的Eureka Server节点的数据,这里设置为falseregister-with-eureka: false # 表示是否将自己注册到Eureka Server,默认为true.由于当前应用就是Eureka Server,故而设置为false.service-url:# 设置Eureka Server的地址,查询服务和注册服务都需要依赖这个地址defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
logging:pattern:console: '%d{MM-dd HH:mm:ss.SSS} %c %M %L [%thread] %m%n'
启动服务并访问

可以看到eureka-server已经启动成功了。 

服务注册

接下来我们把product-service 注册到eureka-server中。

引入eureka-client依赖
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
完善配置文件

添加如下配置

spring:application:name: product-service
eureka:client:service-url:defaultZone: http://127.0.0.1:10010/eureka

完整的配置文件

server:port: 9090
spring:application:name: product-servicedatasource:url: jdbc:mysql://127.0.0.1:3306/cloud_product?characterEncoding=utf8&useSSL=falseusername: rootpassword: #密码driver-class-name: com.mysql.cj.jdbc.Driver
# 设置 Mybatis 的 xml 保存路径
mybatis:configuration: # 配置打印 MyBatis 执行的 SQL#    log-impl: org.apache.ibatis.logging.stdout.StdOutImplmap-underscore-to-camel-case: true  #自动驼峰转换
eureka:client:service-url:defaultZone: http://127.0.0.1:10010/eureka/
logging:pattern:console: '%d{MM-dd HH:mm:ss.SSS} %c %M %L [%thread] %m%n'
启动服务并访问

可以看到product-service已经注册到 eureka上了。

服务发现

接下来我们修改order-service, 在远程调⽤时, 从eureka-server拉取product-service的服务信息, 实现服务发现。

引入依赖

服务注册和服务发现都封装在eureka-client依赖中, 所以服务发现时, 也是引⼊eureka-client依赖。

<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
完善配置文件

添加如下配置

spring:application:name: order-service
eureka:client:service-url:defaultZone: http://127.0.0.1:10010/eureka

完整的配置文件

server:port: 8080
spring:application:name: order-servicedatasource:url: jdbc:mysql://127.0.0.1:3306/cloud_order?characterEncoding=utf8&useSSL=falseusername: rootpassword: #密码driver-class-name: com.mysql.cj.jdbc.Driver
# 设置 Mybatis 的 xml 保存路径
mybatis:configuration: # 配置打印 MyBatis 执行的 SQL
#    log-impl: org.apache.ibatis.logging.stdout.StdOutImplmap-underscore-to-camel-case: true  #自动驼峰转换
eureka:client:service-url:defaultZone: http://127.0.0.1:10010/eureka/
logging:pattern:console: '%d{MM-dd HH:mm:ss.SSS} %c %M %L [%thread] %m%n'
启动服务并访问

可以看到order-service已经注册到 eureka上了。 

可以看到, 远程调⽤也成功了。

Eureka和Zookeeper的区别

Eureka和Zookeeper都是⽤于服务注册和发现的⼯具,区别如下:

1. Eureka是Netflix开源的项⽬, ⽽Zookeeper是Apache开源的项⽬.
2. Eureka 基于AP原则, 保证⾼可⽤, Zookeeper基于CP原则, 保证数据⼀致性.
3. Eureka 每个节点 都是均等的, Zookeeper的节点区分Leader 和Follower 或 Observer, 也正因为这个原因, 如果Zookeeper的Leader发⽣故障时, 需要重新选举, 选举过程集群会有短暂时间的不可⽤。

关键字:网站建设哪家性价比高_如何自己写一个网页_软件外包网站_优化英文

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: