介紹
SpringCloud是基于springBoot的一整套實作微服務的架構.Eureka為springCloud架構中首選推薦的服務治理元件.從分布式或者微服務的角度來講,将一個大的項目劃分成許多小的項目,首當其沖的第一個問題就是子項目之間如何通訊的問題.Eureka是解決微服務架構中服務執行個體維護的一種技術解決方案,說白了就是管理消費者和生産者注冊,一下會通關兩個服務來說明。
一,eureka中server的簡單搭建
建立eurekaServer 服務
建立eureka的maven項目
點選下一步 點選next
等待maven加載完成如下面圖
加入springcloud eureka相關依賴
<?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">
<parent>
<artifactId>micro-serivce-demo</artifactId>
<groupId>com.huangan</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>eureka</artifactId>
<name>eureka</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!-- eurkaServer 依賴 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
<version>1.4.3.RELEASE</version>
</dependency>
<!-- 安全依賴 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
</dependencies>
<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.7.1</version>
</plugin>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.0.0</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
通關application.yml進行eureka配置
server:
port: 9100
#因為目前的eureka 是單機的,是以我們需要做一些配置
eureka:
client:
register-with-eureka: false
fetch-registry: false
service-url:
#defaultZone: http://localhost:9100/eureka/
defaultZone: http://user:[email protected]:9100/eureka/
security:
basic:
enabled: true #開啟安全配置,也就是需要密碼,如果不需要設定為 false 即可,注意這個參數必須放在 application.yml中不允許放在boostrost
user:
password: 123 #設定賬戶名與密碼
name: user
聲明(預設配置eureka配置時可以吧defaultZone 寫成自己eureka服務的路徑,因為如果不寫入他會自己去找服務的注冊,如果沒有就會異常輸出,如果不配置賬号密碼,服務啟動時會在背景生成密碼,
如果不想配置密碼,security:basic:enabled: false,吧defaultZone 改為http://localhost:9100/eureka/即可,後面需要自己擷取背景輸出的密碼)
建立eureka啟動來
package com.huangan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
/**
* Hello world!
*
*/
@SpringBootApplication
@EnableEurekaServer
public class EurekaApp
{
public static void main( String[] args )
{
SpringApplication.run(EurekaApp.class);;
}
}
啟動服務,通路eureka位址:http://localhost:9100/eureka/
通關上面路徑通路輸入賬号密碼,就會顯示eureka的管理頁面,Application下面就是注冊的服務,下面就是服務的注冊于發現。
二,消息生産者proviede-rorder服務注冊
下面的代碼細節不會多講,以上在文章https://blog.csdn.net/weixin_45863786/article/details/109521592 詳細建立說明過
導入eureka的依賴
<!-- eurka用戶端 依賴 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>1.4.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
application.yml的配置
server:
port: 7900 #程式啟動服務的端口号
spring:
application:
name: provider-user #通路應用的名稱
eureka:
client:
service-url:
defaultZone: http://user:[email protected]:9100/eureka
編寫啟動類
package com.huangan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
/**
* Hello world!
*
*/
@SpringBootApplication
@EnableEurekaClient
public class UserApp
{
public static void main( String[] args )
{
SpringApplication.run(UserApp.class);
}
}
運作服務,通關eureka服務管理界面檢視服務是否被注冊
通過上面,以及發現provider-user服務以及注冊了,下面就是通過consumer-order服務進行eureka注冊且使用RestTemplate 擷取資訊
三,消息消費者consumer-order服務注冊
、導入eureka的依賴
<!-- eurka用戶端 依賴 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>1.4.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
配置application.yml檔案
server:
port: 8900
spring:
application:
name: consumer-order
url:
value: http://localhost:7900/user/ #自己寫了一個消息生産者位址
eureka:
client:
service-url:
defaultZone: http://user:[email protected]:9100/eureka
編寫控制器代碼
第一種使用生産者位址擷取接口資料
@RestController
public class OrderController {
@Autowired
private RestTemplate restTemplate; //spring 提供通路rest接口的模闆對象
@Value("${url.value}")
private String url;
@GetMapping("/order/{id}")
public User getUser(@PathVariable Long id){
User user= restTemplate.getForObject(url+"/"+id,User.class);
System.out.println("進入資料:"+user.toString());
return user;
}
}
第二種使用EurekaClient 擷取生産者位址
@RestController
public class OrderController {
@Autowired
private RestTemplate restTemplate; //spring 提供通路rest接口的模闆對象
@Autowired
private EurekaClient eurekaClient;
@GetMapping("/order/{id}")
public User getUser(@PathVariable Long id){
InstanceInfo instanceInfo = eurekaClient.getNextServerFromEureka("PROVIDER-USER",false);
String userUrl = instanceInfo.getHomePageUrl();
User user= restTemplate.getForObject(userUrl+"/user/"+id,User.class);
System.out.println("進入資料:"+user.toString());
return user;
}
}
啟動服務通關eureka頁面進行查詢是否已注冊
下一步通路consumer-order進口路徑檢視是否擷取消費者資料
簡單的實作springcloud 的eureka服務注冊已完成,下一篇會講到ribbon 多服務之間負載均衡整合配置