這兩年能夠說微服務是熱詞,也是新領域,學習的成本也較高,基礎東西太多好比什麼Dubbo,zookeeper,Springboot等等。Dubbo也是實現服務治理又是阿里出的一套開源RPC框架,可是SpringCloud出現後便當即熱火朝天,固然阿里也加入了SpringCloud的孵化。css
什麼是微服務,分佈式?html
分佈式:不一樣的模塊部署在不一樣的服務器上,能夠更好的解決網站高併發。java
微服務:架構設計概念,各服務間隔離(分佈式也是隔離),自治(分佈式依賴總體組合)其它特性(單一職責,邊界,異步通訊,獨立部署)是分佈式概念的跟嚴格執行 SOA到微服務架構的演進過程
。簡單的說把項目拆分紅各各模塊,用註冊中心的方式把服務都連接在一塊兒web
服務提供者與消費關係算法
服務提供者:提供服務被人調用spring
消費者:調用服務緩存
今天咱們說下SpringCloud的Eureka註冊中心tomcat
官方:springboot
group 'com.wang' version '1.0-SNAPSHOT' apply plugin: 'java' sourceCompatibility = 1.8 buildscript { ext {//定義一個變量,統一規定springboot的版本 springBootVersion = '1.5.10.RELEASE' } repositories { maven { url 'http://maven.aliyun.com/nexus/content/groups/public/' } jcenter() mavenCentral() //Spring repo maven { url "http://repo.spring.io/snapshot" } maven { url "http://repo.spring.io/milestone" } maven { url "http://repo.spring.io/release" } maven { url 'http://repo.spring.io/plugins-snapshot' } } dependencies {//用來打包 classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") } } //設定當前全部的項目的配置 allprojects { group 'com.ssx' version '1.0-SNAPSHOT' ext { springCloudVersion = 'Edgware.SR2' } repositories { maven { url 'http://maven.aliyun.com/nexus/content/groups/public/' } jcenter() mavenCentral() //Spring repo maven { url "http://repo.spring.io/snapshot" } maven { url "http://repo.spring.io/milestone" } maven { url "http://repo.spring.io/release" } maven { url 'http://repo.spring.io/plugins-snapshot' } } } //設定當前模塊項目中的配置 subprojects { apply plugin: 'java' apply plugin: 'idea' apply plugin: 'spring-boot' dependencies { //使用springboot-web組件,可是排除tomcat組件 compile ('org.springframework.boot:spring-boot-starter-web'){ exclude module:"spring-boot-starter-tomcat" } //使用undertow做爲應用服務,做爲servlet容器 compile 'org.springframework.boot:spring-boot-starter-undertow' //使用健康檢查組件 compile 'org.springframework.boot:spring-boot-starter-actuator' testCompile( group: 'junit', name: 'junit', version: '4.12' ) } dependencyManagement { imports { //加上此配置後,若是須要引用springcloud的組件,就不須要再提供版本信息 mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}" } } }
3.註冊中心啓動類服務器
package org.gw.reigster.conter; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; /** * @DATA 2019-02-27 19:56 * @Author 張國偉 WeChat:17630376104 * @Description TODO */ @EnableEurekaServer #這個是表示這個項目做爲euraka註冊中心 @SpringBootApplication public class ReigsterConterProvider { public static void main(String[] args) { SpringApplication.run(ReigsterConterProvider.class,args); } }
4.註冊中心yml
server: port: 8888 spring: application: name: reigster-conter eureka: client: register-with-eureka: false #啓動時不註冊 原本就是註冊中心 表示本身是註冊中心 fetch-registry: false #不向註冊中心拉取數據 server: enable-self-preservation: false #關閉自我保護 測試環境用 生產環境通常不用
5.啓動註冊中心訪問8888
這個爲Eureka的信息頁面 能夠看到這個時候 Instances currently registered with Eureka裏面是No instances available 也就是這個時候尚未服務註冊進來,下面咱們寫註冊方
這個模塊爲solr
package org.gw.solr; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; /** * @DATA 2019-02-27 20:38 * @Author 張國偉 WeChat:17630376104 * @Description TODO */ @EnableEurekaClient #這裏爲Eureka客戶端也就是服務 @SpringBootApplication public class ProjectSolrProvider { public static void main(String[] args) { SpringApplication.run(ProjectSolrProvider.class, args); } }
yml
server: port: 10002 spring: application: name: project-solr eureka: client: service-url: defaultZone: http://localhost:8888/eureka/ instance: prefer-ip-address: true
這時候咱們啓動這個服務
這個時候能夠看到 有個服務已經註冊進來
這個就是實現了 註冊也發現,那咱們緊接這 如何實現服務與服務之間調用呢?我如今想project-shopping-mall模塊的controller調用solr的controller如何實現呢
package org.gw.shopping.mall; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.context.annotation.Bean; import org.springframework.web.client.RestTemplate; /** * @DATA 2019-02-27 20:24 * @Author 張國偉 WeChat:17630376104 * @Description TODO */ @EnableEurekaClient @SpringBootApplication public class ShoppingMallProvider { @LoadBalanced @Bean public RestTemplate resultTemplate() { RestTemplate template = new RestTemplate(); return template; } public static void main(String[] args) { SpringApplication.run(ShoppingMallProvider.class, args); } }
RestTemplate表示網絡請求 在啓動類裏把他實例
package org.gw.shopping.mall.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; /** * @DATA 2019-02-27 20:45 * @Author 張國偉 WeChat:17630376104 * @Description TODO */ @Controller public class ShopController { @Autowired private RestTemplate restTemplate; @RequestMapping("/toIndex") public String toIndex(Model model) { String msg = restTemplate.getForEntity("http://PROJECT-SOLR/solrSearc", String.class).getBody(); model.addAttribute("msg", msg); return "index"; } }
這個爲調用solr的controller根據名字調用
package org.gw.solr.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** * @DATA 2019-02-27 20:40 * @Author 張國偉 WeChat:17630376104 * @Description TODO */ @RestController public class SolrSearchController { @RequestMapping("/solrSearc") public String solrSearc(){ return "從solr查詢到數據"; } }
意思就是說 我在shop裏調用solr的controller他會返回一個從solr查詢到數據
咱們來嘗試下
shop模塊裏有thymeleaf模板展現數據
<!DOCTYPE html>
<html lang="en" xmlxs:th="http://www.thymeleaf.org" xmlns:xmlxs="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.w3.org/1999/xhtml">
<head>
<meta charset="UTF-8"/>
<title>Title</title>
</head >
<body>
<h1>歡迎進入商城</h1>
<h2>從solr微服務中查到的數據::::<span th:text="${msg}"></span></h2>
</body>
</html>
能夠看到 已經成功拿到數據
到這裏,咱們已經實現服務的註冊與發現,下章說 鏈路跟蹤,有問題請及時評論。