当前位置: 首页 > news >正文

样本之家登录网站设计深圳

样本之家登录网站,设计深圳,山西省城乡和建设厅网站,个人网页设计思维导图这是本人学习的总结,主要学习资料如下 马士兵教育 目录1、Spring Cloud 简介2、Eureka3、建立Spring Cloud项目3.1、启动Server3.1.1、dependency3.1.2、配置文件3.1.3、Server端启动代码3.2、启动Client3.2.1、dependency3.2.2、配置文件3.3.3、Client端启动代码3…

这是本人学习的总结,主要学习资料如下

  • 马士兵教育

目录

  • 1、Spring Cloud 简介
  • 2、Eureka
  • 3、建立Spring Cloud项目
    • 3.1、启动Server
      • 3.1.1、dependency
      • 3.1.2、配置文件
      • 3.1.3、Server端启动代码
    • 3.2、启动Client
      • 3.2.1、dependency
      • 3.2.2、配置文件
      • 3.3.3、Client端启动代码
    • 3.3、服务之间获取信息

1、Spring Cloud 简介

spring cloud是为了解决微服务架构的难题而诞生的微服务全家桶框架。

确定spring cloud的版本,要根据spring boot来确定,官网上有对应的表格。

注意下面的提示,Dalston, Edgware, Finchley, Greenwich已经不再维护,所以实际开发中不要再使用这四个spring cloud的版本。

在这里插入图片描述

https://spring.io/projects/spring-cloud

2、Eureka

EurekaSpringCloud Nexflix的核心子模块,其中包含ServerClient

Server提供服务注册,存储所有可用服务节点。

Client用于简化和Server的通讯复杂度

下面是Eureka的简单架构图

在这里插入图片描述

每一个服务节点需要在Eureka Server中注册,如果需要其他节点的服务,则需要远程调用Service ProviderProvider会访问Server,由Server找到一个合适的节点提供服务给cumsumer

3、建立Spring Cloud项目

接下来就是代码展示如何配置启动serverclient,以及client之间获取信息

3.1、启动Server

3.1.1、dependency

<dependencies><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</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-dependencies</artifactId><version>2.3.7.RELEASE</version><type>pom</type><scope>import</scope></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>Hoxton.SR12</version><type>pom</type><scope>import</scope></dependency>
</dependencies>

3.1.2、配置文件

spring:application:name: msb-eureka-server
server:port: 8761eureka:instance:#注册实例名称hostname: localhost#是否将自己的ip注册到eureka中,默认false 注册 主机名prefer-ip-address: true# Eureka客户端需要多长时间发送心跳给Eureka,表明他仍然或者,默认是30# 通过下面方式我们可以设置,默认单位是秒lease-renewal-interval-in-seconds: 10# Eurkea服务器在接受到实例最后一次发送的心跳后,需要等待多久可以将次实例删除# 默认值是90# 通过下面方式我们可以设置,默认单位是秒lease-expiration-duration-in-seconds: 30client:#是否注册到eureka服务中register-with-eureka: false#是否拉取其他服务fetch-registry: false

3.1.3、Server端启动代码

@EnableEurekaServer
@SpringBootApplication
public class EureakServerApplication {public static void main(String[] args) {SpringApplication.run(EureakServerApplication.class);}
}

启动以后打开网页检查。
请添加图片描述


3.2、启动Client

3.2.1、dependency

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-dependencies</artifactId><version>2.3.7.RELEASE</version><type>pom</type><scope>import</scope>
</dependency>
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>Hoxton.SR12</version><type>pom</type><scope>import</scope>
</dependency>
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>

3.2.2、配置文件

# 节点在server中注册的名字
spring:application:name: msb-order
server:port: 9002eureka:client:# 这个一定要配对,server地址后面默认要加一个上下文eurekaservice-url:defaultZone: http://localhost:8761/eurekamanagement:endpoints:web:exposure:include: shutdown #暴露shutdown端点endpoint:shutdown:enabled: true #再次确认暴露shutdown端点feign:tokenId: 11111111111111111111

3.3.3、Client端启动代码

注意有两个注解可以将其标注为Client,分别是@EnableDiscoveryClient@EnableEurekaClient

这里推荐使用@EnableDiscoveryClient,因为后者是netfliex提供的,如果使用后者,后期要更换其它注册中心就需要更换注解,比较麻烦。

@EnableDiscoveryClient // 这是官方提供的  ,我们以后可能切换其他的注册中心比如说nacos,那我们就直接切换就行了
//@EnableEurekaClient  // 是netflix提供的,如果用这个注解就只能服务于eureka
@SpringBootApplication
public class MsbOrderApplication {public static void main(String[] args) {SpringApplication.run(MsbOrderApplication.class);}
}

同样的配置和代码,再启动一个叫msb-stock的服务

到Server的页面查看,两个服务都注册成功。

请添加图片描述

3.3、服务之间获取信息

引入LoadBalancerClient,从这个bean中可以获得其他注册的client元数据,比如地址,端口号等。

下面这个例子展示了如何获取其他client的元信息并且调用其它client的服务。

@Service
public class OrderService {@Autowiredprivate LoadBalancerClient eurekaClient;@Autowiredprivate RestTemplate restTemplate;public void getUser() {ServiceInstance instance = eurekaClient.choose("msb-user");String hostname = instance.getHost();int port = instance.getPort();String uri = "/getUserInfo?userId=" + userId;String url = "http://" + hostname + ":" + port + uri;return restTemplate.getForObject(url, String.class);}}
http://www.yayakq.cn/news/522616/

相关文章:

  • 在线网站创做简历常德建设公司网站
  • 电商网站设计规划书国外分销平台有哪些
  • 网站制作wap页面德阳房产网站建设
  • 网站漂浮二维码东莞专业做网站
  • 泊头哪有哪家做网站做的号计算机软件开发是干什么的
  • 淘客采集网站怎么做的甘肃省广电网络公司网站
  • 临沂营销型网站建设为什么会有免费制作网站
  • 天津建站服务discuz建站流程
  • 网站设计销售北京朝阳区房子多少钱一平米
  • seo网站介绍个人网站可以做资讯吗
  • wordpress站内全文检索建设银行支付宝网站
  • 中关村在线官方网站做网站和网页有区别吗
  • 青岛网站建设‘’网站建设维护员是做什么的
  • 石家庄制作网站公司有哪些网站设计制作的四大优势
  • 建设安全协会网站开发比较实用的软件
  • 卫浴洁具网站模板网站空间怎么买
  • 制作钓鱼网站属于什么罪湖南创研科技股份有限公司
  • Php外贸网站建设新浪博客项目推广方式有哪些
  • 班级网站设计论文河南做网站企起
  • 软件下载网站源码直通车关键词优化口诀
  • 张店易宝网站建设网页设计的详细流程
  • 兰州网站建设哪家好三亚网络推广
  • 中国小康建设网 是个什么网站东莞网站公司星鑫
  • 区县12380网站建设情况wordpress帮助手册
  • 网站的数据库在哪里网站单页面怎么做
  • 南充网站建设114网站源码被注册为商标
  • 做英文网站哪个网站比较好wordpress自定义路由
  • 湖北黄石网站群建设南通做外贸的公司网站
  • 网站首页改版影响优化公众平台是什么
  • 柘城网站建设微信网址