做一個sample,微服務間應用調用,首先創建父工程將幾個微服務聚合maven版本管理。 推薦幾個gitee上的精緻demo,主要github太卡了。
參考sample
https://gitee.com/forezp/SpringCloudLearning -- 方誌朋 隨書教程
https://gitee.com/didispace/SpringCloud-Learning -- 程序員dd
https://gitee.com/exclusiver/springcloud2020/tree/master -- 跟敲教程代碼
https://cloud.spring.io/spring-cloud-static/Dalston.SR5/multi/multi_spring-cloud.html Spring cloud 官方文檔 強推!
SpringCloud Springboot版本關係
https://spring.io/projects/spring-cloud 附錄 springcloud和springboot版本關聯。這裏選用2.0.6.Release的springboot,對應就是Finchley。
1. 創建父工程
<?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>
<groupId>org.example</groupId>
<artifactId>SpringCloudDemo</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>eureka-server-01</module>
<module>client-order</module>
<module>client-stock</module>
<module>eureka-server-02</module>
</modules>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Finchley.SR2</spring-cloud.version>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.6.RELEASE</version>
<relativePath/>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2. 創建eureka server 01單機模式
new module eureka-server-01
主要參考 How to run a eureka server 創建 EurekaServerApplication.java 啓動類, 這個文件不能直接放java文件夾下不然會報
** WARNING ** : Your ApplicationContext is unlikely to start due to a @ComponentScan of the default package.
警告:你的應用上下文可能沒有啓動,因爲你將註解添加到了默認的package上面了
因爲會掃當前main方法的包,啓動類沒有包就報錯,新建package裏放進去即可。先開始Standalone Mode 單機模式
package org.xl.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author xl
* @description: gzy的項目
* @date 2022/2/13 6:35 PM
*/
@SpringBootApplication
@EnableEurekaServer
@RestController
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
配置文件 /resource/application.yml
server:
port: 7001
eureka:
instance:
hostname: server7001 #eureka服務端的實例名稱
client:
register-with-eureka: false #false表示不向註冊中心註冊自己
fetch-registry: false #false表示自己端就是註冊中心
service-url:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
spring:
application:
name: cloud-eureka-service
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">
<parent>
<artifactId>SpringCloudDemo</artifactId>
<groupId>org.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>eureka-server-01</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<!--後續版本由spring-cloud-starter-eureka-server改成spring-cloud-starter-netflix-eureka-server-->
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
</project>
啓動工程後訪問 localhost:7001 若出現控制檯則說明啓動成功。
2. 創建eureka server 02 集羣模式
High Availability, Zones and Regions The Eureka server does not have a backend store, but the service instances in the registry all have to send heartbeats to keep their registrations up to date (so this can be done in memory). Clients also have an in-memory cache of eureka registrations (so they don’t have to go to the registry for every single request to a service). By default every Eureka server is also a Eureka client and requires (at least one) service URL to locate a peer. If you don’t provide it the service will run and work, but it will shower your logs with a lot of noise about not being able to register with the peer.
就是說Eureka沒有後臺存儲,eureka服務端同時也是客戶端,可以互相註冊,遵從AP原則。
創建module,eureka-server-02 pom.xml文件和EurekaServerApplication同上
新增/resource/application.yml
server:
port: 7002
eureka:
instance:
hostname: server7002 #eureka服務端的實例名稱
client:
register-with-eureka: false #false表示不向註冊中心註冊自己
fetch-registry: false #false表示自己端就是註冊中心
service-url:
defaultZone: http://server7001/eureka/
spring:
application:
name: cloud-eureka-service
修改 server-01的application.yml文件
server:
port: 7001
eureka:
instance:
hostname: server7001 #eureka服務端的實例名稱
client:
register-with-eureka: false #false表示不向註冊中心註冊自己
fetch-registry: false #false表示自己端就是註冊中心
service-url:
defaultZone: http://server7002/eureka/
spring:
application:
name: cloud-eureka-service
eureka.client.service-url.defaultZone 配置爲對方,形成互補。如果大於兩個呢?官網文檔也提供了配置方法,就是將所有域都配置進去
運行,可看到 Got 1 instances from neighboring DS node
2022-02-13 23:54:41.901 INFO 2566 --- [ Thread-13] c.n.e.r.PeerAwareInstanceRegistryImpl : Got 1 instances from neighboring DS node
2022-02-13 23:54:41.901 INFO 2566 --- [ Thread-13] c.n.e.r.PeerAwareInstanceRegistryImpl : Renew threshold is: 1
2022-02-13 23:54:41.901 INFO 2566 --- [ Thread-13] c.n.e.r.PeerAwareInstanceRegistryImpl : Changing status to UP
2022-02-13 23:54:41.913 INFO 2566 --- [ Thread-13] e.s.EurekaServerInitializerConfiguration : Started Eureka Server
2022-02-13 23:54:41.954 INFO 2566 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 7001 (http) with context path ''
2022-02-13 23:54:41.955 INFO 2566 --- [ main] .s.c.n.e.s.EurekaAutoServiceRegistration : Updating port to 7001
2022-02-13 23:54:41.960 INFO 2566 --- [ main] org.xl.demo.EurekaServerApplication : Started EurekaServerApplication in 22.837 seconds (JVM running for 23.974)
2022-02-13 23:55:41.907 INFO 2566 --- [a-EvictionTimer] c.n.e.registry.AbstractInstanceRegistry : Running the evict task with compensationTime 0ms
附錄,eureka 配置
https://docs.spring.io/spring-cloud-netflix/docs/3.0.4/reference/html/appendix.html