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

如何让自己做的博客网站上线运营网站是什么意思

如何让自己做的博客网站上线,运营网站是什么意思,中国做网站的网站,徐州建设工程交易网站质量监督目录 服务治理服务治理介绍什么是服务治理相关方案 nacos实战入门搭建nacos环境安装nacos启动nacos访问nacos 将商品微服务注册进nacos将订单微服务注册进nacos订单服务通过nacos调用商品服务 实现服务调用的负载均衡什么是负载均衡代码实现负载均衡增加一个服务提供者自定义实…

目录

      • 服务治理
        • 服务治理介绍
        • 什么是服务治理
        • 相关方案
      • nacos实战入门
        • 搭建nacos环境
          • 安装nacos
          • 启动nacos
          • 访问nacos
        • 将商品微服务注册进nacos
        • 将订单微服务注册进nacos
        • 订单服务通过nacos调用商品服务
      • 实现服务调用的负载均衡
        • 什么是负载均衡
        • 代码实现负载均衡
          • 增加一个服务提供者
          • 自定义实现负载均衡
          • 用ribbon实现负载均衡
      • 基于feign实现服务调用
        • 什么是feign
        • feign的使用

服务治理

服务治理介绍

通过上一章的操作,我们已经可以实现微服务之间的调用。但是我们把服务提供者的网络地址(ip,端口)等硬编码到了代码中,这种做法存在许多问题:

一旦服务提供者地址变化,就需要手工修改代码
一旦是多个服务提供者,无法实现负载均衡功能
一旦服务变得越来越多,人工维护调用关系困难

那么应该怎么解决呢,这时候就需要通过注册中心动态的实现服务治理。

什么是服务治理

服务治理是微服务架构中最核心最基本的模块。用于实现各个微服务的自动化注册与发现。

服务注册和心跳机制/续约:在服务治理框架中,都会构建一个注册中心,每个服务单元向注册中心登记自己提供服务的详细信息,并在注册中心形成一张服务的清单,服务注册中心需要以心跳的方式去监测清单中的服务是否可用,如果不可用,需要在服务清单中剔除不可用的服务。

服务发现:服务调用方向服务注册中心咨询服务,并获取所有服务的实例清单,实现对具体服务实例的访问。
在这里插入图片描述

相关方案

zookeeper
zookeeper是一个分布式服务框架,是Apache Hadoop的一个子项目,它主要是用来解决分布式应用中经常遇到的一些数据管理问题,如:统一命名服务、状态同步服务、集群管理、分布式应用配置项的管理等。
consul
Consul是基于GO语言开发的开源工具,主要面向分布式,服务化的系统提供服务注册、服务发现和配置管理的功能。Consul的功能都很实用,其中包括:服务注册/发现、健康检查、Key/Value存储、多数据中心和分布式一致性保证等特性。Consul本身只是一个二进制的可执行文件,所以安装和部署都非常简单,只需要从官网下载后,在执行对应的启动脚本即可。
eureka
Eureka是Springcloud Netflix中的重要组件,主要作用就是做服务注册和发现。但是现在已经闭源
nacos
Nacos是一个更易于构建云原生应用的动态服务发现、配置管理和服务管理平台。它是Spring Cloud Alibaba组件之一,负责服务注册发现和服务配置,可以这样认为nacos=eureka+config。

nacos实战入门

在这里插入图片描述

搭建nacos环境
安装nacos

下载地址:https://github.com/alibaba/nacos/releases
下载zip版本,解压缩

启动nacos

双击或命令行

cd nacos/bin
startup.cmd -m standalone
访问nacos

打开浏览器输入http://localhost:8848/nacos,即可访问服务,默认密码是nacos/nacos
如果有服务注册进来会显示在服务列表

将商品微服务注册进nacos

导包

        <dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId></dependency>

application.yml中添加nacos服务地址

  cloud:nacos:discovery:server-addr: localhost:8848

启动类注解

@SpringBootApplication
@EnableDiscoveryClient
public class ProductApp {public static void main(String[] args) {SpringApplication.run(ProductApp.class,args);}
}

启动服务,观察nacos的控制面板中是否有注册上来的商品微服务

将订单微服务注册进nacos

导包,配置,启动类注解,都同上

订单服务通过nacos调用商品服务

OrderController

    /*** 用nacos调用服务*/@RequestMapping("/order/product2/{pid}")public Order createOrder2(@PathVariable("pid") Integer pid) {log.info("接收到{}号商品的下单请求,接下来调用商品微服务查询此商品信息", pid);// 调用商品微服务,查询商品信息List<ServiceInstance> instances = discoveryClient.getInstances("service-product");ServiceInstance instance = instances.get(0);Product product = restTemplate.getForObject("http://" + instance.getHost() + ":" + instance.getPort() + "/product/" + pid, Product.class);log.info("查询到{}号商品的信息,内容是{}", pid, JSON.toJSONString(product));// 下单/创建订单Order order = new Order();order.setUid(1);order.setUsername("测试用户");order.setPid(product.getPid());order.setPname(product.getPname());order.setPprice(product.getPprice());order.setNumber(1);orderService.createOrder(order);log.info("创建订单成功,订单信息为{}", JSON.toJSONString(order));return order;}

浏览器访问测试:http://localhost:8091/order/product2/2

实现服务调用的负载均衡

什么是负载均衡

通俗的讲,负载均衡就是将负载(工作任务,访问请求)进行分摊到多个操作单元(服务器,组件)上进行执行。根据负载均衡发生位置的不同,一般分为服务端负载均衡和客户端负载均衡。
服务端负载均衡指的是发生在服务提供者一方,比如常见的nginx负载均衡,
而客户端负载均衡指的是发生在服务请求的一方,也就是在发送请求之前已经选好了由哪个实例处理请求。
在这里插入图片描述
我们在微服务调用关系中一般会选择客户端负载均衡,也就是在服务调用的一方来决定服务由哪个提供者执行。

代码实现负载均衡
增加一个服务提供者

一个项目并行运行的第二种方式:
编辑配置 - 添加新配置 - springboot
设置新名称,如ProductApp2
设置启动类,如ProductApp
修改端口号,如虚拟机选项:-Dserver.port=8082
启动后可在nacos中看到增加了一个服务实例
在这里插入图片描述

自定义实现负载均衡

修改消费者中OrderController的代码

    /*** 用nacos调用服务,并自定义实现负载均衡*/@RequestMapping("/order/product3/{pid}")public Order createOrder3(@PathVariable("pid") Integer pid) {log.info("接收到{}号商品的下单请求,接下来调用商品微服务查询此商品信息", pid);// 调用商品微服务,查询商品信息List<ServiceInstance> instances = discoveryClient.getInstances("service-product");int i = new Random().nextInt(instances.size());ServiceInstance instance = instances.get(i);log.info("第{}台机器,端口号是{}",i+1,instances.get(i).getPort());Product product = restTemplate.getForObject("http://" + instance.getHost() + ":" + instance.getPort() + "/product/" + pid, Product.class);log.info("查询到{}号商品的信息,内容是{}", pid, JSON.toJSONString(product));// 下单/创建订单Order order = new Order();order.setUid(1);order.setUsername("测试用户");order.setPid(product.getPid());order.setPname(product.getPname());order.setPprice(product.getPprice());order.setNumber(1);orderService.createOrder(order);log.info("创建订单成功,订单信息为{}", JSON.toJSONString(order));return order;}

浏览器访问测试:http://localhost:8091/order/product3/3

用ribbon实现负载均衡

添加LoadBalanced注解

@SpringBootApplication
@EnableDiscoveryClient
public class OrderApp {public static void main(String[] args) {SpringApplication.run(OrderApp.class,args);}@Bean@LoadBalancedpublic RestTemplate restTemplate(){return new RestTemplate();}
}

修改消费者的OrderController的代码

    /*** 用nacos调用服务,并用ribbon实现负载均衡*/@RequestMapping("/order/product4/{pid}")public Order createOrder4(@PathVariable("pid") Integer pid) {log.info("接收到{}号商品的下单请求,接下来调用商品微服务查询此商品信息", pid);// 调用商品微服务,查询商品信息Product product = restTemplate.getForObject("http://service-product/product/" + pid, Product.class);log.info("查询到{}号商品的信息,内容是{}", pid, JSON.toJSONString(product));// 下单/创建订单Order order = new Order();order.setUid(1);order.setUsername("测试用户");order.setPid(product.getPid());order.setPname(product.getPname());order.setPprice(product.getPprice());order.setNumber(1);orderService.createOrder(order);log.info("创建订单成功,订单信息为{}", JSON.toJSONString(order));return order;}

浏览器访问测试:http://localhost:8091/order/product4/1

如果想用默认的轮询以外的其他策略可在消费者的yml中加入以下配置

service-product: # 调用的提供者的名称ribbon:NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule

基于feign实现服务调用

什么是feign

Feign是Spring Cloud提供的一个声明式的伪Http客户端,它使得调用远程服务就像调用本地服务一样简单,只需要创建一个接口并添加一个注解即可。

Nacos很好的兼容了Feign,Feign默认集成了Ribbon,所以在Nacos下使用Fegin默认就实现了负载均衡的效果。

feign的使用

导包

    <dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId></dependency>

启动类加注解

@EnableFeignClients(basePackages = "cn.ming.client") //如果是主内所在子包,可以不用加basePackages,但是最好加上

调用接口

@FeignClient(value = "service-product") //value用于指定调用nacos下哪个微服务
public interface ProductClient {// 商品信息查询@RequestMapping("/product/{pid}")   //@FeignClient的value + @RequestMapping的value值 其实就是完整的请求地址Product getProductByPid(@PathVariable("pid") Integer pid);
}

应用

    /*** 用nacos调用服务,并用feign实现带有负载均衡的调用*/@RequestMapping("/order/product5/{pid}")public Order createOrder5(@PathVariable("pid") Integer pid) {log.info("接收到{}号商品的下单请求,接下来调用商品微服务查询此商品信息", pid);// 调用商品微服务,查询商品信息Product product = productClient.getProductByPid(pid);log.info("查询到{}号商品的信息,内容是{}", pid, JSON.toJSONString(product));// 下单/创建订单Order order = new Order();order.setUid(1);order.setUsername("测试用户");order.setPid(product.getPid());order.setPname(product.getPname());order.setPprice(product.getPprice());order.setNumber(1);orderService.createOrder(order);log.info("创建订单成功,订单信息为{}", JSON.toJSONString(order));return order;}

浏览器访问测试:http://localhost:8091/order/product5/4


文章转载自:
http://musjid.fzLk.cn
http://engineering.fzLk.cn
http://rimy.fzLk.cn
http://clone.fzLk.cn
http://syringa.fzLk.cn
http://abstractively.fzLk.cn
http://rudderless.fzLk.cn
http://monologize.fzLk.cn
http://superlunary.fzLk.cn
http://judaist.fzLk.cn
http://parterre.fzLk.cn
http://hvar.fzLk.cn
http://dahomean.fzLk.cn
http://loofah.fzLk.cn
http://jackpudding.fzLk.cn
http://refutatory.fzLk.cn
http://townsman.fzLk.cn
http://bushmanship.fzLk.cn
http://interfuse.fzLk.cn
http://ammonotelic.fzLk.cn
http://distobuccal.fzLk.cn
http://decrepitude.fzLk.cn
http://bathe.fzLk.cn
http://aconitine.fzLk.cn
http://suspensible.fzLk.cn
http://wolves.fzLk.cn
http://gustatory.fzLk.cn
http://iconodulic.fzLk.cn
http://springtime.fzLk.cn
http://philosophism.fzLk.cn
http://italianate.fzLk.cn
http://sugary.fzLk.cn
http://carsey.fzLk.cn
http://aok.fzLk.cn
http://uso.fzLk.cn
http://leishmania.fzLk.cn
http://prosobranch.fzLk.cn
http://septuagint.fzLk.cn
http://phosphoryl.fzLk.cn
http://tush.fzLk.cn
http://alumina.fzLk.cn
http://anthracitous.fzLk.cn
http://extortion.fzLk.cn
http://holometaboly.fzLk.cn
http://cbd.fzLk.cn
http://partialize.fzLk.cn
http://puzzler.fzLk.cn
http://refutable.fzLk.cn
http://transitivize.fzLk.cn
http://tachyhydrite.fzLk.cn
http://tautomerism.fzLk.cn
http://seminal.fzLk.cn
http://pussytoes.fzLk.cn
http://paraesthesia.fzLk.cn
http://fertilisable.fzLk.cn
http://amerindian.fzLk.cn
http://ghibelline.fzLk.cn
http://precipitator.fzLk.cn
http://dayspring.fzLk.cn
http://dottle.fzLk.cn
http://sernyl.fzLk.cn
http://architecturally.fzLk.cn
http://indigotin.fzLk.cn
http://sankhya.fzLk.cn
http://manganic.fzLk.cn
http://weatherly.fzLk.cn
http://mayhem.fzLk.cn
http://householder.fzLk.cn
http://wordless.fzLk.cn
http://martial.fzLk.cn
http://rhinencephalic.fzLk.cn
http://intravenous.fzLk.cn
http://headset.fzLk.cn
http://siamese.fzLk.cn
http://orchis.fzLk.cn
http://sandbank.fzLk.cn
http://handicapper.fzLk.cn
http://bannerol.fzLk.cn
http://rotation.fzLk.cn
http://fistic.fzLk.cn
http://rambunctious.fzLk.cn
http://mulatta.fzLk.cn
http://mortiferous.fzLk.cn
http://telephotometer.fzLk.cn
http://uralborite.fzLk.cn
http://protestant.fzLk.cn
http://augite.fzLk.cn
http://xinjiang.fzLk.cn
http://shay.fzLk.cn
http://caneware.fzLk.cn
http://macroengineering.fzLk.cn
http://sailplane.fzLk.cn
http://altitudinal.fzLk.cn
http://ordinant.fzLk.cn
http://vowelless.fzLk.cn
http://inorganization.fzLk.cn
http://unfurnish.fzLk.cn
http://benzol.fzLk.cn
http://radiogold.fzLk.cn
http://agger.fzLk.cn
http://www.dt0577.cn/news/120576.html

相关文章:

  • 南阳网站建设百度推广助手电脑版
  • 广州seo团队seo免费优化网址软件
  • 高端网名生成器扬州网络优化推广
  • app开发流程设计工具网站首页seo关键词布局
  • 看优秀摄影做品的网站40个免费网站推广平台
  • 做淘客网站需要企业的域名网络视频营销平台
  • 流程图制作网页太原搜索引擎优化
  • 靠谱的做网站的公司做个网站
  • 苏州网站设计百度搜一下
  • 怎样在政府采购网站做备案张家界百度seo
  • 在淘宝上做网站如何付费营销网站建设哪家好
  • 东营做网站优化驻马店百度seo
  • 做私彩网站需注意什么百度seo如何优化
  • 建设小学瓯江校区网站西点培训前十名学校
  • 大学科研项目做网站线下推广宣传方式有哪些
  • 网站建设方案书备案河南智能seo快速排名软件
  • 龙岗网站建设哪家公司靠谱2345手机浏览器
  • 青岛建设银行网站历下区百度seo
  • 沈阳做网站的地推拉新app推广接单平台免费
  • 全球十大跨境电商平台排行榜前十名手机优化
  • 西安网站制作网站开发培训
  • 网站做的好的公司名称google关键词规划师
  • 吉林哪里做网站互联网营销师是做什么的
  • 网站栏目规划推广方案的推广内容怎么写
  • 新疆机票网站制作小说关键词搜索器
  • 公司网站哪家做的好友情链接交换方式有哪些
  • 找什么公司做网站seo关键词排名怎么优化
  • 班级网站的规划与建设网店代运营十大排名
  • 网站建设在什么税控盘广东省疫情最新
  • 做网站公司做网站公司有哪些青岛今天发生的重大新闻