<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          Spring Cloud 微服務(wù)架構(gòu)解決方案

          共 18703字,需瀏覽 38分鐘

           ·

          2020-11-06 02:52

          點(diǎn)擊上方藍(lán)色字體,選擇“標(biāo)星公眾號”

          優(yōu)質(zhì)文章,第一時間送達(dá)

          66套java從入門到精通實(shí)戰(zhàn)課程分享

          1. 目標(biāo)

          1、能說出微服務(wù)架構(gòu)特點(diǎn)。

          2、能說出微服務(wù)技術(shù)棧的構(gòu)成。

          3、理解Spring Cloud Eureka服務(wù)治理的流程。

          4、理解Spring Cloud Ribbon負(fù)載均衡的應(yīng)用方法。

          5、理解Spring Cloud Hystrix 斷路保護(hù)的應(yīng)用方法。

          6、理解Spring Cloud Zuul 網(wǎng)關(guān)的作用及方法。

          7、了解Spring Cloud Config、Spring Cloud Bus、Spring Cloud Sleuth等其它子項(xiàng)目的應(yīng)用場景。

          2. 理解微服務(wù)

          2.1 軟件架構(gòu)演進(jìn)

          • 軟件架構(gòu)的發(fā)展經(jīng)歷了從單體結(jié)構(gòu)、垂直架構(gòu)、SOA架構(gòu)到微服務(wù)架構(gòu)的過程。

          2.1.1 單體架構(gòu)

          特點(diǎn):

          1. 所有的功能集成在一個項(xiàng)目工程中。

          2. 所有的功能打一個war包部署到服務(wù)器。

          3. 應(yīng)用與數(shù)據(jù)庫分開部署。

          4. 通過部署應(yīng)用集群和數(shù)據(jù)庫集群來提高系統(tǒng)的性能。

          優(yōu)點(diǎn):

          • 項(xiàng)目架構(gòu)簡單,前期開發(fā)成本低,周期短,小型項(xiàng)目的首選。

          缺點(diǎn):

          1. 全部功能集成在一個工程中,對于大型項(xiàng)目不易開發(fā)、擴(kuò)展及維護(hù)。

          2. 系統(tǒng)性能擴(kuò)展只能通過擴(kuò)展集群結(jié)點(diǎn),成本高、有瓶頸。

          3. 技術(shù)棧受限。

          2.1.2 垂直架構(gòu)

          特點(diǎn):

          1. 以單體結(jié)構(gòu)規(guī)模的項(xiàng)目為單位進(jìn)行垂直劃分項(xiàng)目即將一個大項(xiàng)目拆分成一個一個單體結(jié)構(gòu)項(xiàng)目。

          2. 項(xiàng)目與項(xiàng)目之間的存在數(shù)據(jù)冗余,耦合性較大,比如上圖中三個項(xiàng)目都存在客戶信息。

          3. 項(xiàng)目之間的接口多為數(shù)據(jù)同步功能,如:數(shù)據(jù)庫之間的數(shù)據(jù)庫,通過網(wǎng)絡(luò)接口進(jìn)行數(shù)據(jù)庫同步。

          優(yōu)點(diǎn):

          1. 項(xiàng)目架構(gòu)簡單,前期開發(fā)成本低,周期短,小型項(xiàng)目的首選。

          2. 通過垂直拆分,原來的單體項(xiàng)目不至于無限擴(kuò)大。

          3. 不同的項(xiàng)目可采用不同的技術(shù)。

          缺點(diǎn):

          1. 全部功能集成在一個工程中,對于大型項(xiàng)目不易開發(fā)、擴(kuò)展及維護(hù)。

          2. 系統(tǒng)性能擴(kuò)展只能通過擴(kuò)展集群結(jié)點(diǎn),成本高、有瓶頸。

          2.1.3 SOA架構(gòu)


          特點(diǎn):

          1. 基于SOA的架構(gòu)思想將重復(fù)公用的功能抽取為組件以服務(wù)的方式給各各系統(tǒng)提供服務(wù)

          2. 各各項(xiàng)目(系統(tǒng))與服務(wù)之間采用webservice、rpc等方式進(jìn)行通信。

          3. ESB企業(yè)服務(wù)總線作為項(xiàng)目與服務(wù)之間通信的橋梁

          優(yōu)點(diǎn):

          1. 將重復(fù)的功能抽取為服務(wù),提高開發(fā)效率,提高系統(tǒng)的可重用性、可維護(hù)性

          2. 可以針對不同服務(wù)的特點(diǎn)制定集群及優(yōu)化方案。

          3. 采用ESB減少系統(tǒng)中的接口耦合。

          缺點(diǎn):

          1. 系統(tǒng)與服務(wù)的界限模糊,不利于開發(fā)及維護(hù)。

          2. 雖然使用了ESB,但是服務(wù)的接口協(xié)議不固定,種類繁多,不利于系統(tǒng)維護(hù)。

          3. 抽取的服務(wù)的粒度過大,系統(tǒng)與服務(wù)之間耦合性高

          2.1.1 微服務(wù)架構(gòu)

          特點(diǎn):

          1. 將系統(tǒng)服務(wù)層完全獨(dú)立出來,并將服務(wù)層抽取為一個一個的微服務(wù)

          2. 微服務(wù)遵循單一原則

          3. 微服務(wù)之間采用RESTful等輕量協(xié)議傳輸

          優(yōu)點(diǎn):

          1. 服務(wù)拆分粒度更細(xì),有利于資源重復(fù)利用,提高開發(fā)效率。

          2. 可以更加精準(zhǔn)的制定每個服務(wù)的優(yōu)化方案,提高系統(tǒng)可維護(hù)性。

          3. 微服務(wù)架構(gòu)采用去中心化思想,服務(wù)之間采用RESTful等輕量協(xié)議通信,相比ESB更輕量。

          4. 適用于互聯(lián)網(wǎng)時代,產(chǎn)品迭代周期更短。

          缺點(diǎn):

          1. 微服務(wù)過多,服務(wù)治理成本高,不利于系統(tǒng)維護(hù)。

          2. 分布式系統(tǒng)開發(fā)的技術(shù)成本高(容錯、分布式事務(wù)等),對團(tuán)隊(duì)挑戰(zhàn)大。

          2.2 什么是微服務(wù)

          為適應(yīng)企業(yè)的業(yè)務(wù)發(fā)展,提高軟件研發(fā)的生產(chǎn)力,降低軟件研發(fā)的成本,軟件架構(gòu)也作了升級和優(yōu)化,將一個獨(dú)立的系統(tǒng)拆分成若干小的服務(wù),每個小服務(wù)運(yùn)行在不同的進(jìn)程中,服務(wù)與服務(wù)之間采用http 輕量協(xié)議(比如流行的RESTful)傳輸數(shù)據(jù),每個服務(wù)所擁有的功能具有獨(dú)立性強(qiáng)、高內(nèi)聚的特點(diǎn),這樣的設(shè)計(jì)就實(shí)現(xiàn)了單個服務(wù)的高內(nèi)聚,服務(wù)與服務(wù)之間的低耦合效果,這一個一個的小服務(wù)就是微服務(wù),基于這種方法設(shè)計(jì)的系統(tǒng)架構(gòu)即微服務(wù)架構(gòu)

          2.3 Spring Cloud技術(shù)棧

          2.3.1 微服務(wù)的技術(shù)棧

          • 負(fù)載均衡,網(wǎng)關(guān)路由:高可用、集群部署,校驗(yàn)、請求轉(zhuǎn)發(fā)、服務(wù)集成。

          • 服務(wù)治理:服務(wù)注冊、發(fā)現(xiàn)。

          • 容錯:避免雪崩。

          • 監(jiān)控跟蹤:監(jiān)控資源利用、服務(wù)響應(yīng)、容器資源利用情況。

          • 消息總線:消息隊(duì)列、異步通信。

          • 配置管理:統(tǒng)一配置管理。

          2.3.2 Spring Cloud是什么

          • Spring Cloud為開發(fā)人員構(gòu)建微服務(wù)架構(gòu)提供了完整的解決方案,SpringCloud是若干個框架的集合,它包括spring-cloud-config、spring-cloud-bus等近20個子項(xiàng)目,它提供了服務(wù)治理、服務(wù)網(wǎng)關(guān)、智能路由、負(fù)載均衡、斷路器、監(jiān)控跟蹤、分布式消息隊(duì)列、配置管理等領(lǐng)域的解決方案

          2.3.3 Spring Cloud技術(shù)棧

          微服務(wù)的興起出現(xiàn)了很多優(yōu)秀的公司和技術(shù):
          服務(wù)治理:Dubbo(阿里巴巴)、Dubbox(當(dāng)當(dāng))、Eureka(Netflix)等 。
          配置管理:Disconf(百度)、QConf(360)、Diamood(淘寶)等 。
          服務(wù)跟蹤:Hydra(京東)、Zipkin(Twitter)、Sleuth(Spring Cloud)等 。

          Spring Cloud 提供一站式的微服務(wù)架構(gòu)解決方案,如下圖:

          2.3.4 為什么使用Spring Cloud

          • 微服務(wù)架構(gòu)的優(yōu)點(diǎn)表明它可以提高我們的生產(chǎn)力,但是分布式系統(tǒng)本身的技術(shù)成本問題給互聯(lián)網(wǎng)那些創(chuàng)業(yè)型公司不少的挑戰(zhàn),阿里、百度等巨頭所提供的微服務(wù)技術(shù)只是解決其中某個問題,而整合封裝這些優(yōu)秀的技術(shù)恐怕是Spring最擅長的領(lǐng)域了,Spring Cloud也正因?yàn)榇硕Q生。

          • 使用Spring Cloud來構(gòu)建微服務(wù)架構(gòu)可以省去你整合各家技術(shù)的成本,Spring Cloud為我們構(gòu)建微服務(wù)架構(gòu)提供了一站式的解決方案,就好比當(dāng)初Spring誕生是為解決EJB企業(yè)應(yīng)用開發(fā)的眾多問題而提供的一站式輕量級企業(yè)應(yīng)用開發(fā)解決方案一樣,隨著使用Spring Cloud公司數(shù)量的增加,相信微服務(wù)將被Spring Cloud一統(tǒng)江湖。

          2.3.5 Netflix公司介紹

          Spring Cloud的很多技術(shù)來源于Netfix(https://netflix.github.io/),摘自百度百科的信息如下:

          3. 開發(fā)環(huán)境

          3.1 Java8

          • Spring Cloud是基于Java構(gòu)建,本課程使用Java8作為基礎(chǔ)平臺。

          3.2 Maven

          • 優(yōu)秀的架構(gòu)離不開優(yōu)秀的項(xiàng)目構(gòu)建工具,本課程采用Maven來構(gòu)建(使用apache-maven-3.3.9-bin)。

          3.3 Spring Boot

          Spring Cloud是基于Spring Boot構(gòu)建,本課程使用Spring Boot 1.5.4版本。

          ???
          ????????org.springframework.boot
          ????????spring-boot-starter-parent
          ????????1.5.4.RELEASE
          ????


          3.4 Spring Cloud

          Spring Cloud為了避免和各子項(xiàng)目的版本名稱混淆,它采用倫敦地鐵站命名。
          當(dāng)前版本情況如下:

          本課程 使用Dalston.SR3版本。

          ?
          ????
          ????????
          ????????????
          ????????????????org.springframework.cloud
          ????????????????spring-cloud-dependencies
          ????????????????Dalston.SR3
          ????????????????<type>pomtype>
          ????????????????import
          ????????????

          ????????

          ????


          3.5 MySQL

          MySQL作為數(shù)據(jù)庫的第二把交椅甚至直逼Oralce這個老大,在互聯(lián)網(wǎng)開發(fā)中MySQL的應(yīng)用是最廣泛的。本課程采用MySQL5來構(gòu)建架構(gòu)。

          3.6 IDEA

          IntelliJ IDEA功能的強(qiáng)大及易用性不亞于Eclipse,據(jù)統(tǒng)計(jì)其使用人數(shù)已直逼Eclipse,成為老大指日可待。
          本課程 使用IDEA作為開發(fā)工具。

          4. 服務(wù)治理

          4.1 什么是服務(wù)治理

          微服務(wù)架構(gòu)的缺點(diǎn)中最主要的就是由于微服務(wù)數(shù)量眾多導(dǎo)致維護(hù)成本巨大,服務(wù)治理為解決此問題而產(chǎn)生的。服務(wù)治理的作用是讓維護(hù)人員從人工維護(hù)中解放出來由服務(wù)自維護(hù),微服務(wù)作為服務(wù)提供方主動向服務(wù)治理中心注冊,服務(wù)的消費(fèi)方通過服務(wù)治理中心查詢需要的服務(wù)并進(jìn)行調(diào)用。

          如下圖:

          4.2 Spring Cloud Eureka

          Spring Cloud Eureka 是對Netflix公司的Eureka的二次封裝它實(shí)現(xiàn)了服務(wù)治理的功能,Spring Cloud Eureka提供服務(wù)端與客戶端,服務(wù)端即是服務(wù)注冊中心,客戶端完成服務(wù)的注冊與發(fā)現(xiàn)。服務(wù)端和客戶端均采用Java語言編寫(Eureka支持多語言)。

          如下圖顯示了Eureka Server與Eureka Client的關(guān)系:

          4.3 架構(gòu)

          4.4 實(shí)戰(zhàn)

          4.4.1 流程圖

          4.4.2 開發(fā)并部署 Eureka Server

          1、創(chuàng)建Spring Boot工程

          SpringBoot啟動類

          package?cn.itcast.microservice;
          import?org.springframework.boot.SpringApplication;
          import?org.springframework.boot.autoconfigure.SpringBootApplication;
          import?org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

          @EnableEurekaServer?//聲明這是一個Eureka服務(wù)
          @SpringBootApplication
          public?class?SpringcloudEurekaApplication?{

          ?public?static?void?main(String[]?args)?{
          ??SpringApplication.run(SpringcloudEurekaApplication.class,?args);
          ?}

          }


          2、在pom.xml中添加依賴(spring boot 、spring cloud、Eureka Server)

          "1.0"?encoding="UTF-8"?>
          "http://maven.apache.org/POM/4.0.0"?xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          ?xsi:schemaLocation="http://maven.apache.org/POM/4.0.0?https://maven.apache.org/xsd/maven-4.0.0.xsd">
          ?4.0.0

          ?cn.itcast
          ?springcloud-eureka
          ?0.0.1-SNAPSHOT
          ?springcloud-eureka
          ?Demo?project?for?Spring?Boot

          ????
          ????????org.springframework.boot
          ????????spring-boot-starter-parent
          ????????1.5.4.RELEASE
          ?????????
          ????


          ????
          ????
          ????????
          ????????????
          ????????????????org.springframework.cloud
          ????????????????spring-cloud-dependencies
          ????????????????Dalston.SR3
          ????????????????<type>pomtype>
          ????????????????import
          ????????????

          ????????

          ????

          ????
          ????????
          ????????
          ????????????org.springframework.cloud
          ????????????spring-cloud-starter-eureka-server
          ????????

          ????


          ????
          ????????${project.artifactId}
          ????????
          ????????????
          ????????????
          ????????????????org.apache.maven.plugins
          ????????????????maven-resources-plugin
          ????????????????
          ????????????????????UTF-8
          ????????????????

          ????????????????2.6
          ????????????

          ????????????
          ????????????
          ????????????????org.apache.maven.plugins
          ????????????????maven-compiler-plugin
          ????????????????
          ????????????????????<source>1.8source>
          ????????????????????1.8
          ????????????????????UTF-8
          ????????????????

          ????????????

          ????????

          ????




          3、配置application.yml

          server:
          ??port:?${port}?#服務(wù)端口?通過啟動腳本傳入?yún)?shù)

          spring:
          ??application:
          ????name:?itcast-microservice-eureka?#指定服務(wù)名

          eureka:
          ??client:
          ????registerWithEureka:?true?#是否將自己注冊到Eureka服務(wù)中,本身就是所有無需注冊
          ????fetchRegistry:?true?#是否從Eureka中獲取注冊信息
          ????serviceUrl:?#Eureka客戶端與Eureka服務(wù)端進(jìn)行交互的地址
          ??????defaultZone:?${eureka.server}
          ??instance:
          ????prefix-ip-address:?true?#將自己的ip地址注冊到Eureka服務(wù)中
          ????ip-address:?127.0.0.1
          ????instance-id:?${spring.application.name}:${server.port}?#指定實(shí)例id
          ??server:
          ????enable-self-preservation:?false?#禁用自我保護(hù)模式
          ????eviction-interval-timer-in-ms:?60000?#清理間隔(單位毫秒?默認(rèn)是60*1000)

          4、部署兩臺Eureka Server,并且互相注冊,實(shí)現(xiàn)高可用。

          注意:如果沒有設(shè)置Eureka Server的復(fù)制結(jié)點(diǎn)eureka默認(rèn)會找8761端口。

          5、配置兩個啟動腳本



          6、啟動eureka1和eureka2,并分別訪問地址 http://localhost:6868/


          http://localhost:6869/

          4.4.1 開發(fā)用戶信息服務(wù)

          用戶信息服務(wù)實(shí)現(xiàn)用戶信息查詢、用戶注冊、用戶信息修改等功能。
          開發(fā)用戶信息服務(wù)采用Spring Boot、SpringMVC、Mybatis架構(gòu),數(shù)據(jù)庫采用MySqL5。

          SQL語句:

          CREATE?TABLE?user?(
          ??`id`?bigint(20)?NOT?NULL?AUTO_INCREMENT,
          ??`username`?varchar(50)?NOT?NULL?COMMENT?'用戶名',
          ??`password`?varchar(32)?NOT?NULL?COMMENT?'密碼,加密存儲',
          ??`name`?varchar(50)?DEFAULT?NULL?COMMENT?'名字',
          ???email?varchar(30),
          ??`birthday`?datetime?NOT?NULL?COMMENT?'生日',
          ???sex?char(2)?not?null,
          ???state?char(1)?not?null,
          ???code?varchar(10),
          ??PRIMARY?KEY?(`id`),
          ??UNIQUE?KEY?`username`?(`username`)?USING?BTREE
          )?ENGINE=InnoDB?AUTO_INCREMENT=28?DEFAULT?CHARSET=utf8?COMMENT='用戶表';


          4.4.2 服務(wù)注冊

          服務(wù)注冊完成將用戶信息服務(wù)注冊到Eureka Server中,供服務(wù)消費(fèi)方查詢。

          1、在用戶信息服務(wù)中配置Eureka服務(wù)中心地址及用戶信息服務(wù)名稱。
          2、在SpringBoot的啟動類中添加注解@EnableDiscoveryClient
          3、注意Eureka服務(wù)中心地址配置兩個,因?yàn)橛袃膳_Eureka服務(wù)中心。
          4、用戶服務(wù)注冊中心啟動兩個服務(wù),為后邊測試負(fù)載均衡準(zhǔn)備。

          項(xiàng)目結(jié)構(gòu):
          UserController.class

          package?com.itheima.microservice.user.controll;
          import?com.itheima.microservice.user.pojo.User;
          import?com.itheima.microservice.user.service.UserService;
          import?org.springframework.beans.factory.annotation.Autowired;
          import?org.springframework.cloud.context.config.annotation.RefreshScope;
          import?org.springframework.web.bind.annotation.GetMapping;
          import?org.springframework.web.bind.annotation.PathVariable;
          import?org.springframework.web.bind.annotation.RestController;

          /**
          ?*?Created?by?Administrator?on?2017/12/6.
          ?*?需要熱加載的bean需要加上@RefreshScope注解,
          ?*?當(dāng)配置發(fā)生變更的時候可以在不重啟應(yīng)用的前提下完成bean中相關(guān)屬性的刷新。
          ?*/
          @RestController
          @RefreshScope
          public?class?UserController?{

          ????@Autowired
          ????private?UserService?userService;

          ????/**
          ?????*?對外提供接口服務(wù),根據(jù)賬號查詢用戶信息
          ?????*
          ?????*?@param?username
          ?????*?@return
          ?????*/
          ????@GetMapping(value?=?"user/{username}")
          ????public?User?getUserByUsername(@PathVariable("username")?String?username)?{
          ????????return?this.userService.getUserByUsername(username);
          ????}
          }


          UserService.class

          package?com.itheima.microservice.user.service;
          import?com.itheima.microservice.user.mapper.UserMapper;
          import?com.itheima.microservice.user.pojo.User;
          import?org.springframework.beans.factory.annotation.Autowired;
          import?org.springframework.stereotype.Service;


          /**
          ?*?Created?by?Administrator?on?2017/12/6.
          ?*/
          @Service
          public?class?UserService?{

          ????@Autowired
          ????private?UserMapper?userMapper;

          ????/**
          ?????*?根據(jù)賬號查詢用戶
          ?????*
          ?????*?@param?username
          ?????*?@return
          ?????*/
          ????public?User?getUserByUsername(String?username)?{
          ????????return?userMapper.getUserByUsername(username);
          ????}

          }


          UserApplication.class 啟動類

          package?com.itheima.microservice.user.mail;
          import?com.itheima.microservice.user.util.SpringContextUtils;
          import?org.mybatis.spring.annotation.MapperScan;
          import?org.springframework.boot.SpringApplication;
          import?org.springframework.boot.autoconfigure.SpringBootApplication;
          import?org.springframework.cloud.client.discovery.EnableDiscoveryClient;
          import?org.springframework.context.ApplicationContext;
          import?org.springframework.context.annotation.ComponentScan;

          /**
          ?*?Created?by?Administrator?on?2017/12/6.
          ?*/
          @EnableDiscoveryClient
          @SpringBootApplication
          @ComponentScan(basePackages={"com.itheima.microservice"})
          @MapperScan("com.itheima.microservice.user.mapper")
          public?class?UserApplication?{
          ????public?static?void?main(String[]?args)?{
          ????????ApplicationContext?applicationContext?=?SpringApplication.run(UserApplication.class,?args);
          ????????SpringContextUtils.setApplicationContext(applicationContext);
          ????}

          }


          UserMapper.class

          package?com.itheima.microservice.user.mapper;
          import?com.itheima.microservice.user.pojo.User;

          /**
          ?*?Created?by?Administrator?on?2017/12/8.
          ?*/

          public?interface?UserMapper?{
          ????User?getUserByUsername(String?username);
          }


          User.class

          package?com.itheima.microservice.user.pojo;
          import?java.util.Date;

          /**
          ?*?Created?by?Administrator?on?2017/12/10.
          ?*/
          public?class?User?{
          ????private?String?id;

          ????private?String?username;

          ????private?String?password;

          ????private?String?name;

          ????private?String?email;

          ????private?Date?birthday;

          ????private?String?sex;

          ????private?int?state;

          ????private?String?code;

          ????public?String?getId()?{
          ????????return?id;
          ????}

          ????public?String?getUsername()?{
          ????????return?username;
          ????}

          ????public?String?getPassword()?{
          ????????return?password;
          ????}

          ????public?String?getName()?{
          ????????return?name;
          ????}

          ????public?String?getEmail()?{
          ????????return?email;
          ????}

          ????public?Date?getBirthday()?{
          ????????return?birthday;
          ????}

          ????public?String?getSex()?{
          ????????return?sex;
          ????}

          ????public?int?getState()?{
          ????????return?state;
          ????}

          ????public?String?getCode()?{
          ????????return?code;
          ????}

          ????public?void?setId(String?id)?{
          ????????this.id?=?id;
          ????}

          ????public?void?setUsername(String?username)?{
          ????????this.username?=?username;
          ????}

          ????public?void?setPassword(String?password)?{
          ????????this.password?=?password;
          ????}

          ????public?void?setName(String?name)?{
          ????????this.name?=?name;
          ????}

          ????public?void?setEmail(String?email)?{
          ????????this.email?=?email;
          ????}

          ????public?void?setBirthday(Date?birthday)?{
          ????????this.birthday?=?birthday;
          ????}

          ????public?void?setSex(String?sex)?{
          ????????this.sex?=?sex;
          ????}

          ????public?void?setState(int?state)?{
          ????????this.state?=?state;
          ????}

          ????public?void?setCode(String?code)?{
          ????????this.code?=?code;
          ????}
          ????public?User(String?id,?String?username,?String?password,?String?name,?String?email,?Date?birthday,?String?sex,?Integer?state,?String?code)?{
          ????????this.id?=?id;
          ????????this.username?=?username;
          ????????this.password?=?password;
          ????????this.name?=?name;
          ????????this.email?=?email;
          ????????this.birthday?=?birthday;
          ????????this.sex?=?sex;
          ????????this.state?=?state;
          ????????this.code?=?code;
          ????}

          ????public?User()?{
          ????}
          ????@Override
          ????public?String?toString()?{
          ????????return?"User{"?+
          ????????????????"id='"?+?id?+?'\''?+
          ????????????????",?username='
          "?+?username?+?'\''?+
          ????????????????"
          ,?password='"?+?password?+?'\''?+
          ????????????????",?name='"?+?name?+?'\''?+
          ????????????????",?email='
          "?+?email?+?'\''?+
          ????????????????"
          ,?birthday="?+?birthday?+
          ????????????????"
          ,?sex='"?+?sex?+?'\''?+
          ????????????????",?state="?+?state?+
          ????????????????",?code='"?+?code?+?'\''?+
          ????????????????'
          }';
          ????}
          }

          SpringContextUtils.class

          package?com.itheima.microservice.user.util;
          import?org.springframework.context.ApplicationContext;

          /**
          ?*?Created?by?Administrator?on?2017/12/8.
          ?*/
          public?class?SpringContextUtils?{
          ????private?static?ApplicationContext?applicationContext;

          ????public?static?void?setApplicationContext(ApplicationContext?context)?{
          ????????applicationContext?=?context;
          ????}

          ????public?static?Object?getBean(String?beanId)?{
          ????????return?applicationContext.getBean(beanId);
          ????}
          }


          UserMapper.xml

          "1.0"?encoding="UTF-8"??>
          "-//mybatis.org//DTD?Mapper?3.0//EN"?"http://mybatis.org/dtd/mybatis-3-mapper.dtd"?>
          "com.itheima.microservice.user.mapper.UserMapper">

          ????"getUserByUsername"?resultType="User">
          ???????????????SELECT?id,username,password,name,email,birthday,sex,state
          ???????????????FROM?user?WHERE?username?=?#{username}
          ????


          application.yml

          server:
          ??port:?${port}?#服務(wù)端口
          spring:
          ??application:
          ????name:?itcast-microservice-user?#指定服務(wù)名
          ################################################################
          #?mysql?屬性配置
          ??datasource:
          ????driver-class-name:?com.mysql.cj.jdbc.Driver
          ????type:?com.alibaba.druid.pool.DruidDataSource
          ????url:?jdbc:mysql://127.0.0.1:3306/store?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8
          ????username:?root
          ????password:?root

          #??jpa:
          #????hibernate:
          #??????#ddl-auto:?create?#ddl-auto:設(shè)為create表示每次都重新建表
          #??????ddl-auto:?update?#ddl-auto:設(shè)為update表示每次都不會重新建表
          #????show-sql:?true
          ################################################################
          ################################################################
          #spring集成Mybatis環(huán)境
          #pojo別名掃描包
          mybatis:
          ??type-aliases-package:?com.itheima.microservice.user.pojo
          ################################################################
          ################################################################
          eureka:
          ??client:
          ????registerWithEureka:?true?#是否將自己注冊到Eureka服務(wù)中,默認(rèn)為true
          ????fetchRegistry:??true??#是否從Eureka中獲取注冊信息,默認(rèn)為true
          ????serviceUrl:?#Eureka客戶端與Eureka服務(wù)端進(jìn)行交互的地址
          ??????defaultZone:?http://127.0.0.1:6868/eureka/,http://127.0.0.1:6869/eureka/
          ????eurekaServerConnectTimeoutSeconds:?60
          ????eurekaServerReadTimeoutSeconds:?60
          ??instance:
          ????prefer-ip-address:??true??#將自己的ip地址注冊到Eureka服務(wù)中
          ????ip-address:?127.0.0.1
          ????instance-id:?${spring.application.name}:${server.port}?#指定實(shí)例id
          ????lease-expiration-duration-in-seconds:?30?#續(xù)約更新時間間隔(默認(rèn)30秒)
          ????lease-renewal-interval-in-seconds:?10?#?續(xù)約到期時間(默認(rèn)90秒)
          ????leaseRenewalIntervalInSeconds:?10?#心跳時間
          ################################################################

          ################################################################
          logging:
          ??level:
          ????root:?debug
          #????org.springframework.web:?DEBUG
          ??file:?/log/log/my_provide.log
          ??pattern:
          ????console:?"%d{yyyy/MM/dd-HH:mm:ss}?[%thread]?%-5level?%logger-?%msg%n"
          ????file:?"%d{yyyy/MM/dd-HH:mm}?[%thread]?%-5level?%logger-?%msg%n"
          ################################################################

          management:
          ??security:
          ????enabled:?false?#是否開啟actuator安全認(rèn)證

          pom.xml

          "1.0"?encoding="UTF-8"?>
          "http://maven.apache.org/POM/4.0.0"?xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          ?xsi:schemaLocation="http://maven.apache.org/POM/4.0.0?https://maven.apache.org/xsd/maven-4.0.0.xsd">
          ?4.0.0

          ?cn.itcast
          ?user
          ?0.0.1-SNAPSHOT

          ????
          ????????org.springframework.boot
          ????????spring-boot-starter-parent
          ????????1.5.4.RELEASE
          ????

          ????
          ????
          ????????
          ????????????
          ????????????????org.springframework.cloud
          ????????????????spring-cloud-dependencies
          ????????????????Dalston.SR3
          ????????????????<type>pomtype>
          ????????????????import
          ????????????

          ????????

          ????


          ????
          ????????
          ????????
          ????????????org.springframework.boot
          ????????????spring-boot-starter-actuator
          ????????

          ????????
          ????????
          ????????????org.springframework.boot
          ????????????spring-boot-starter-web
          ????????

          ????????
          ????????
          ????????????org.springframework.cloud
          ????????????spring-cloud-starter-eureka-server
          ????????????
          ????????????????
          ????????????????????com.fasterxml.jackson.dataformat
          ????????????????????jackson-dataformat-xml
          ????????????????

          ????????????

          ????????

          ????????
          ????????
          ????????????org.mybatis.spring.boot
          ????????????mybatis-spring-boot-starter
          ????????????1.1.1
          ????????

          ????????
          ????????????mysql
          ????????????mysql-connector-java
          ????????????8.0.11
          ????????

          ????????
          ????????
          ????????????com.alibaba
          ????????????druid
          ????????????1.0.9
          ????????

          ????????
          ????????
          ????????????org.springframework.cloud
          ????????????spring-cloud-starter-config
          ????????

          ????????
          ????????
          ????????????org.springframework.cloud
          ????????????spring-cloud-starter-bus-amqp
          ????????

          ????????
          ????????
          ????????????org.springframework.retry
          ????????????spring-retry
          ????????

          ????????
          ????????????org.springframework.boot
          ????????????spring-boot-starter-aop
          ????????

          ????????
          ????????
          ????????
          ????????
          ????????
          ????????
          ????

          ????
          ????????${project.artifactId}
          ????????
          ????????????
          ????????????
          ????????????????org.apache.maven.plugins
          ????????????????maven-resources-plugin
          ????????????????
          ????????????????????UTF-8
          ????????????????

          ????????????????2.6
          ????????????

          ????????????
          ????????????
          ????????????????org.apache.maven.plugins
          ????????????????maven-compiler-plugin
          ????????????????
          ????????????????????<source>1.8source>
          ????????????????????1.8
          ????????????????????UTF-8
          ????????????????

          ????????????

          ????????????
          ????????????
          ????????????????org.springframework.boot
          ????????????????spring-boot-maven-plugin
          ????????????

          ????????

          ????




          添加啟動配置


          訪問如下:user服務(wù)已經(jīng)注冊到eureka server中


          訪問地址,數(shù)據(jù)成功返回

          5. 負(fù)載均衡

          5.1 什么是負(fù)載均衡

          • 負(fù)載均衡是微服務(wù)架構(gòu)中必須使用的技術(shù),通過負(fù)載均衡來實(shí)現(xiàn)系統(tǒng)的高可用、集群擴(kuò)容等功能。負(fù)載均衡可通過硬件設(shè)備及軟件來實(shí)現(xiàn),硬件比如:F5、Array等,軟件比如:LVS、Nginx等 。

          如下圖是負(fù)載均衡的架構(gòu)圖:

          • 用戶請求先到達(dá)負(fù)載均衡器(也相當(dāng)于一個服務(wù)),負(fù)載均衡器根據(jù)負(fù)載均衡算法將請求轉(zhuǎn)發(fā)到微服務(wù)。負(fù)載均衡算法有:輪訓(xùn)、隨機(jī)、加權(quán)輪訓(xùn)、加權(quán)隨機(jī)、地址哈希等方法,負(fù)載均衡器維護(hù)一份服務(wù)列表,根據(jù)負(fù)載均衡算法將請求轉(zhuǎn)發(fā)到相應(yīng)的微服務(wù)上,所以負(fù)載均衡可以為微服務(wù)集群分擔(dān)請求,降低系統(tǒng)的壓力。

          5.2 Spring Cloud Ribbon

          Spring Cloud Ribbon是基于客戶端的負(fù)載均衡工具,負(fù)載均衡分為服務(wù)端負(fù)載均衡和客戶端負(fù)載均衡,3.1小節(jié)的圖形指的是服務(wù)端負(fù)載均衡,客戶端負(fù)載均衡與服務(wù)端負(fù)載均衡的區(qū)別在于客戶端要維護(hù)一份服務(wù)列表,Ribbon從Eureka Server獲取服務(wù)列表,Ribbon根據(jù)負(fù)載均衡算法直接請求到具體的微服務(wù),中間省去了負(fù)載均衡服務(wù)。
          如下圖是Ribbon負(fù)載均衡的流程圖:

          • 1、在消費(fèi)微服務(wù)中使用Ribbon實(shí)現(xiàn)負(fù)載均衡,Ribbon先從Eureka Server中獲取服務(wù)列表。

          • 2、Ribbon根據(jù)負(fù)載均衡的算法進(jìn)行負(fù)載均衡,將請求轉(zhuǎn)發(fā)到其它微服務(wù)

          5.3 實(shí)戰(zhàn)

          5.3.1 開發(fā)單點(diǎn)登錄服務(wù)

          單點(diǎn)登錄服務(wù)提供用戶登錄、用戶退出等功能。
          單點(diǎn)登錄服務(wù)需要調(diào)用用戶信息服務(wù)查詢用戶信息。
          單點(diǎn)登錄服務(wù)采用Spring Boot、SpringMVC開發(fā)。

          5.3.2 服務(wù)發(fā)現(xiàn)

          單點(diǎn)登錄服務(wù)需要從EurekaServer查詢用戶信息服務(wù)。
          1、配置Eureka Server服務(wù)地址。
          2、在SpringBoot的啟動類中添加注解@EnableDiscoveryClient

          5.3.3 Ribbon編程

          1、定義RestTemplate對象
          2、配置 @LoadBalanced
          3、設(shè)置負(fù)載均衡算法。
          4、使用RestTemplate調(diào)用微服務(wù)。


          6. 容錯保護(hù)

          6.1 什么是容錯保護(hù)

          • 容錯保護(hù)是指微服務(wù)在執(zhí)行過程中出現(xiàn)錯誤并從錯誤中恢復(fù)的能力。微服務(wù)容錯性不好很容易導(dǎo)致雪崩效應(yīng),什么是雪崩效應(yīng)?摘自百度百科中的定義:

          • 微服務(wù)的雪崩效應(yīng)表現(xiàn)在服務(wù)與服務(wù)之間調(diào)用,當(dāng)其中一個服務(wù)無法提供服務(wù)可能導(dǎo)致其它服務(wù)也死掉,比如:單點(diǎn)登錄服務(wù)調(diào)用用戶信息服務(wù)查詢用戶信息,由于用戶信息服務(wù)無法提供服務(wù)導(dǎo)致單點(diǎn)登錄服務(wù)一直等待,從而導(dǎo)致用戶登錄、用戶退出功能無法使用,像這樣由一個服務(wù)所引起的一連串的多個服務(wù)無法提供服務(wù)即是微服務(wù)的雪崩效應(yīng)

          6.2 Spring Cloud Hystrix

          Spring Cloud Hystrix 是基于Netflix的開源框架Hystrix的整合,它實(shí)現(xiàn)了斷路器、線程隔離、信號隔離等容錯功能
          下圖是Hystrix斷路器示意圖:

          6.3 實(shí)戰(zhàn)

          1.要在服務(wù)消費(fèi)方添加hystrix。

          2、使用Hystrix實(shí)現(xiàn)容錯。
          3、在Spring boot的啟動類上添加@EnableHystrix注解

          7. 服務(wù)網(wǎng)關(guān)

          7.1 什么是服務(wù)網(wǎng)關(guān)

          • 服務(wù)網(wǎng)關(guān)是在微服務(wù)前邊設(shè)置一道屏障,請求先到服務(wù)網(wǎng)關(guān),網(wǎng)關(guān)會對請求進(jìn)行過慮、校驗(yàn)、路由等處理。有了服務(wù)網(wǎng)關(guān)可以提高微服務(wù)的安全性,校驗(yàn)不通過的請求將被拒絕訪問。

          • 前邊介紹的Ribbon客戶端負(fù)載均衡技術(shù)可以不用經(jīng)過網(wǎng)關(guān),因?yàn)橥ǔJ褂肦ibbon完成微服務(wù)與微服務(wù)之間的內(nèi)部調(diào)用,而對那些對外提供服務(wù)的微服務(wù),比如:用戶登錄、提交訂單等,則必須經(jīng)過網(wǎng)關(guān)來保證微服務(wù)的安全。

          7.2 Spring Cloud Zuul

          Spring Cloud Zuul是整合Netflix公司的Zuul開源項(xiàng)目實(shí)現(xiàn)的微服務(wù)網(wǎng)關(guān),它實(shí)現(xiàn)了請求路由、負(fù)載均衡、校驗(yàn)過慮等 功能。

          7.3 實(shí)戰(zhàn)


          1、部署用戶信息服務(wù)A、單點(diǎn)登錄服務(wù)B,每個服務(wù)部署至少兩臺機(jī)器。
          2、將用戶信息服務(wù)A、單點(diǎn)登錄服務(wù)B注冊到EurekaServer中。
          3、開發(fā)并部署zuul。

          4、在zuul中配置路由
          5、可以定義filter,需要集成zuul提供filter類,進(jìn)行校驗(yàn)攔截。
          6、在spring boot的啟動類中配置注解
          7、根據(jù)上邊的路由配置訪問微服務(wù)。

          凡是以/sso/打頭的請求,路由到 itcast-microservice-sso微服務(wù)。

          7.4 總結(jié)

          zuul網(wǎng)關(guān),不僅提供對外服務(wù)訪問 ,微服務(wù)也可以通過zuul請求其它的微服務(wù)。



          版權(quán)聲明:本文為博主原創(chuàng)文章,遵循?CC 4.0 BY-SA?版權(quán)協(xié)議,轉(zhuǎn)載請附上原文出處鏈接和本聲明。

          本文鏈接:

          https://blog.csdn.net/qq_22075913/article/details/109429800





          粉絲福利:實(shí)戰(zhàn)springboot+CAS單點(diǎn)登錄系統(tǒng)視頻教程免費(fèi)領(lǐng)取

          ???

          ?長按上方微信二維碼?2 秒
          即可獲取資料



          感謝點(diǎn)贊支持下哈?

          瀏覽 55
          點(diǎn)贊
          評論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點(diǎn)贊
          評論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  99国产毛片 | 久久久91人妻无蜜桃码 | 丁香婷婷五月色成人网站 | 91精品久久久久久久久久入口 | 大鸡巴久久久久久久久久 |