【SpringCloud】(五):服務註冊到Eureka Server
上篇文章我們建立了Eureka Server例項。本篇文章我們把使用者服務和電影服務註冊到Eureka Server上。(服務消費者和服務提供者)
首先我們以註冊使用者服務為整個講述核心
註冊文件,SpringCloud官網,我們定位到How to Include Eureka Client,跟著文件一步一步來。
一.基本註冊
1.How to Include Eureka Client
在使用者服務的POM.xml 中加入依賴
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency>
2.Registering with Eureka
package com.dynamic.cloud; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; @SpringBootApplication @EnableEurekaClient public class MicroserviceSimpleProviderUserApplication { public static void main(String[] args) { SpringApplication.run(MicroserviceSimpleProviderUserApplication.class, args); } }
3.在application.yml中加入Eureka的地址
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
至此基本的服務註冊就已經完成了,我們的服務已經成功的註冊在了Erueka上面。我們啟動Eureka,並啟動使用者服務,可以觀察到,註冊在Eureka上面的服務例項。
但是上面Application和Status有些問題。應用名稱是未知,狀態是電腦的名字。我們可以通過配置,進行定製,設定應用名稱和將狀態修改為主機+ 埠。
二.配置Application和Status
在使用者服務的application.yml配置檔案中那個進行配置。
1.配置Application的名稱
2.配置Status
重啟服務,檢視。
三.為Eureka Server進行認證;
1.配置Eureka服務的application.yml
2.在Eureka服務的POM.xml中新增依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
3.Eureka增加了認證,那麼服務在註冊的使用,也就需要訪問認證的Eureka。所以需要修改使用者的請求方式,修改使用者服務的Application.yml檔案。
四.監控和管理生產環境
在使用者服務的POM.xml中加入依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-actuator</artifactId>
</dependency>
五. Eureka’s Health Checks (健康檢查)
在Eureka專案的application.yml中新增配置;
六. Using the EurekaClient
在使用者微服務的Controller中加入:
ServiceId = Spring.application.name
我們不但將使用者服務註冊到了Eureka Server中,並進行了Application配置,同時對Eureka進行了認證和使用。
同樣的方式把電影微服務註冊到Eureka中
1.修改application.yml配置檔案
2.在啟動類上加入註解@EnableEurekaClient
3.加入相關依賴。
啟動Eureka,使用者服務和電影服務。
程式碼展示:
Eureka
application.yml配置檔案
server:
port: 8761 #Eurake
eureka:
client:
healthcheck:
enabled: true
register-with-eureka: false
fetch-registry: false
service-url:
defaultZone: http://user:[email protected]:8761/eureka #把Eureka註冊到那個Eureka
security:
basic:
enabled: true
user:
name: user #login username
password: pass123
POM.xml
<project xmlns="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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>microservice-discovery-eureka</artifactId>
<packaging>jar</packaging>
<parent>
<groupId>com.dynamic.cloud</groupId>
<artifactId>microservice-spring-cloud</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath/>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
</dependencies>
</project>
使用者微服務:
application.yml
server:
port: 7900
spring:
jpa:
generate-ddl: false
show-sql: true
hibernate:
ddl-auto: none
datasource:
platform: h2
schema: classpath:schema.sql #建表語句
data: classpath:data.sql #資料
application:
name: microservice-provider-user #使用小寫
logging:
level:
root: INFO
org.hibernate: INFO
org.hibernate.type.descriptor.sql.BasicBinder: TRACE
org.hibernate.type.descriptor.sql.BasicExtractor: TRACE
com.dynamic: DEBUG
eureka:
client:
serviceUrl:
defaultZone: http://user:[email protected]:8761/eureka
instance:
prefer-ip-address: true
instance-id: ${spring.application.name}:${spring.application.instance_id:${server.port}}
啟動類package com.dynamic.cloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableEurekaClient
public class MicroserviceSimpleProviderUserApplication {
public static void main(String[] args) {
SpringApplication.run(MicroserviceSimpleProviderUserApplication.class, args);
}
}
Controller
package com.dynamic.cloud.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import com.dynamic.cloud.entity.User;
import com.dynamic.cloud.repository.UserRepository;
import com.netflix.appinfo.InstanceInfo;
import com.netflix.discovery.EurekaClient;
import org.springframework.cloud.client.discovery.DiscoveryClient;
@RestController
public class UserController
{
@Autowired
private UserRepository userRepository;
@GetMapping("/simple/{id}")
public User findById(@PathVariable Long id)
{
return this.userRepository.findById(id);
}
@Autowired
private EurekaClient eurekaClient;
@GetMapping("/eureka-instance")
public String serviceUrl() {
InstanceInfo instance = eurekaClient.getNextServerFromEureka("MICROSERVICE-PROVIDER-USER", false);
return instance.getHomePageUrl();
}
@Autowired
private DiscoveryClient discoveryClient;
@GetMapping("/instance-info")
public ServiceInstance showInfo()
{
ServiceInstance localServiceInstance = this.discoveryClient.getLocalServiceInstance();
return localServiceInstance;
}
}
電影微服務類比使用者微服務,進行修改。
application.yml
server:
port: 7901
user:
userServicePath: http://localhost:7900/simple/
spring:
application:
name: microservice-comsumer-movie
eureka:
client:
serviceUrl:
defaultZone: http://user:[email protected]:8761/eureka
instance:
prefer-ip-address: true
instance-id: ${spring.application.name}:${spring.application.instance_id:${server.port}}
POM.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>microservice-comsumer-movie</artifactId>
<packaging>jar</packaging>
<parent>
<groupId>com.dynamic.cloud</groupId>
<artifactId>microservice-spring-cloud</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-actuator</artifactId>
</dependency>
</dependencies>
</project>
啟動類加上註解:@EnableEurekaClient
相關推薦
【SpringCloud】(五):服務註冊到Eureka Server
上篇文章我們建立了Eureka Server例項。本篇文章我們把使用者服務和電影服務註冊到Eureka Server上。(服務消費者和服務提供者) 首先我們以註冊使用者服務為整個講述核心 註冊文件,SpringCloud官網,我們定位到How to Includ
【SpringCloud】(一):SpringCloud入門程式
什麼是SpringCloud,和SpringBoot的關係 官網:http://projects.spring.io/spring-cloud/ ---------------------------------------------------------------
Spring Cloud(二):服務註冊與發現 Eureka【Finchley 版】
LEDE .com Go eureka clean 英文逗號 開始 效果 sam Spring Cloud(二):服務註冊與發現 Eureka【Finchley 版】 發表於 2018-04-15 | 更新於 2018-05-07 | 上一篇主要介紹了相關理論,這一
【ElasticSearch】(五)“Result window is too large & 深度分頁”的利弊權衡
如題,在使用elastic search的dsl查詢過程中,遇到了如下問題: { "error": { "root_cause": [{ "type": "query_phase_execution_exception", "reason": "Re
【ElasticSearch】(五)“Result window is too large & 深度分頁”的利弊權衡
如題,在使用elastic search的dsl查詢過程中,遇到了如下問題: { "error": { "root_cause": [{ "type": "query_phase_execution_exception", "reason": "R
【VC++】(一):MFC在Picture control控制元件中顯示Bitmap
今天在《VC++指紋模式識別系統及演算法概述》一書中,看到有一段程式碼——在Picture Control中顯示Bitmap。把它的程式碼和顯示結果摘出來,作為今天的小小學習成果,鼓勵一下自己。程式碼
【YOLO】(二):Yolo_mark使用教程
一、獲取原始碼 首先,從github上拉取程式碼。 git clone [email protected]:AlexeyAB/Yolo_mark.git 二、編譯可執行檔案 然後用visual studio開啟yolo_mark.sln,配置正確的openc
【Tensorflow】(十): tf.train.string_input_producer
tf.train.string_input_producer( string_tensor, num_epochs=None, shuffle=True, seed=None, capacity=32, shared_n
【原創】(五)Linux程序排程-CFS排程器
# 背景 - `Read the fucking source code!` --By 魯迅 - `A picture is worth a thousand words.` --By 高爾基 說明: 1. Kernel版本:4.14 2. ARM64處理器,Contex-A53,雙核 3. 使用工具:S
Choerodon 的微服務之路(三):服務註冊與發現
本文是 Choerodon 的微服務之路系列推文第三篇。在上一篇《Choerodon的微服務之路(二):微服務閘道器》中,介紹了Choerodon 在搭建微服務閘道器時考慮的一些問題以及兩種常見的微服務閘道器模式,並且通過程式碼介紹了Choerodon 的閘道器是如何實現的。本篇文章將介紹Choer
spring-cloud微服務之路(三):服務註冊和發現之Eureka、Consul
在上一篇spring-cloud微服務之路(二):Spring Boot 我們介紹瞭如何快速的使用 Spring Boot 搭建一個微服務專案,這一篇我們演示如何分別使用 Spring Cloud Eureka 和 Spring Cloud Consul 完成
基於docker部署的微服務架構(一):服務註冊中心
前言 微服務架構解決方案使用 spring cloud ,由於spring cloud目前版本迭代非常快,bug也有不少,這裡以目前最新的版本 Camden.SR2 為例。 spring cloud netflix套件 spring cloud net
Spring Cloud(一):服務註冊中心Eureka
Spring Cloud 基於 Netflix 的幾個開源專案進行了封裝,提供包括服務註冊與發現(Eureka),智慧路由(Zuul),熔斷器(Hystrix),客戶端負載均衡(Ribbon)等在內的核心元件。 在微服務系統中,服務少則十幾、幾十個,多則上百、幾百個(據悉 Netflix 的雲平臺上運行了50
SpringCloud進擊 | 一淺出:服務註冊與發現(Eureka)【Finchley版本】
1.前言 Spring Cloud 已經幫我們實現了服務註冊中心,我們只需要很簡單的幾個步驟就可以完成。關於理論知識,我想大家都已經有不同程度上的瞭解和認識,這裡,我們最後再進行總結。本系列 Spring Cloud 介紹基於 Spring Boot 2.0.5 版本和 Spring C
【SpringCloud】(十五):Zuul的基本應用,反向代理和負載均衡
Router and Filter: Zuul。 Zuul is a JVM based router and server side load balancer by Netflix。 Zu
【Win 10 應用開發】UI Composition 劄記(五):燈光
傳播 目標 spa 速度 review sta sset ext 集合 UI Composition 除了能夠為 UI 元素建立三維空間外,還有相當重要的一個部件——燈光。宇宙萬物的精彩繽紛,皆源於光明,光,使我們看到各種東西,除了黑洞之外的世界都是
Spring Cloud(四):服務容錯保護 Hystrix【Finchley 版】
回調 alt 差異 ner 隔離 簡化 保護 不可用 無法 Spring Cloud(四):服務容錯保護 Hystrix【Finchley 版】 發表於 2018-04-15 | 更新於 2018-05-07 | 分布式系統中經常會出現某個基礎服務不可用造成整個系統
Spring Cloud(三):服務提供與調用 Eureka【Finchley 版】
fan default fun cer 觀察 微服務 divide 動態 erl Spring Cloud(三):服務提供與調用 Eureka【Finchley 版】 發表於 2018-04-15 | 更新於 2018-05-07 | 上一篇文章我們介紹了 Eure
【linux】Valgrind工具集詳解(五):命令列詳解
一、使用方法 usage: valgrind [options] prog-and-args 使用方法:valgrind [引數選項] 程式和引數 二、選擇工具 tool-selection option, with default in [ ]: 工具選擇選項,預設值在[]
python爬蟲(五):實戰 【2. 爬創客實驗室(requests + bs4)】
目標:爬取創科實驗室網站中講座的資訊, 輸出表:講座標題、報告人、單位、報告時間、講座內容、報告人簡介 技術:requests + bs4 檢視爬蟲協議: http://127.0.0.1/lab/robots.txt (創科實驗室是我自己寫的網址,不反爬蟲)