sporingboot:2.0.5
springcloud:Finchley.RELEASEjava
<?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> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.5.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.definesys</groupId> <artifactId>my_cloud</artifactId> <version>0.0.1-SNAPSHOT</version> <name>my_cloud</name> <packaging>pom</packaging> <description>Demo project for Spring Boot</description> <modules> <module>eurekaServer</module> <module>sayHello</module> </modules> <properties> <java.version>1.8</java.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <spring-cloud.version>Finchley.RELEASE</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <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> <repositories> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/milestone</url> </repository> </repositories> </project>
建立兩個model工程,建立model一個做爲eureka server,一個是client也就是具體的服務。node
<?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> <parent> <groupId>com.definesys</groupId> <artifactId>my_cloud</artifactId> <version>0.0.1-SNAPSHOT</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.definesys</groupId> <artifactId>eureka-server</artifactId> <version>0.0.1-SNAPSHOT</version> <name>eureka-server</name> <description>Demo project for Spring Boot</description> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
配置文件:web
server.port=1111 eureka.instance.hostname=localhost #這兩個是代表本身是eureka Server #是否向服務註冊中心註冊本身 eureka.client.register-with-eureka=false #是否檢索服務 eureka.client.fetch-registry=false #關閉自我保護機制(生產環境建議開啓) #eureka.server.enable-self-preservation=true #服務註冊中心的配置內容,指定服務註冊中心的位置 eureka.client.service-url.defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ spring.application.name: eurka-server
在啓動類上添加@EnableEurekaServerspring
@SpringBootApplication @EnableEurekaServer public class EurekaServerApplication { public static void main(String[] args) { SpringApplication.run(EurekaServerApplication.class, args); } }
啓動eureka server,在瀏覽器地址欄輸入http://localhost:1111,能夠看到erueka註冊中心頁面apache
<?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> <parent> <groupId>com.definesys</groupId> <artifactId>my_cloud</artifactId> <version>0.0.1-SNAPSHOT</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.definesys</groupId> <artifactId>say-hello</artifactId> <version>0.0.1-SNAPSHOT</version> <name>say-hello</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
配置文件:瀏覽器
server.port=2222 #服務名稱,服務與服務之間相互調用通常都是根據這個name spring.application.name=say-hello #註冊中心地址 eureka.client.service-url.defaultZone=http://localhost:1111/eureka/
建立一個Controller緩存
@RestController public class SayHelloController { @Value("${server.port}") private String serverPort; @GetMapping("/sayHello") public String sayHelloCtr(@RequestParam("helloName")String helloName){ return "Hello "+helloName+",個人端口是: "+serverPort; } }
在啓動類上添加@EnableEurekaClient代表本身是一個eureka client安全
@SpringBootApplication @EnableEurekaClient public class SayHelloApplication { public static void main(String[] args) { SpringApplication.run(SayHelloApplication.class, args); } }
啓動服務訪問http://localhost:1111,會發現服務已經註冊到註冊中心中去了。
服務器
(1)enable-self-preservation: true # 自我保護模式,當出現出現網絡分區、eureka在短期內丟失過多客戶端時,會進入自我保護模式,即一個服務長時間沒有發送心跳,eureka也不會將其刪除,默認爲true (2)eviction-interval-timer-in-ms: 60000 #eureka server清理無效節點的時間間隔,默認60000毫秒,即60秒 (3)renewal-percent-threshold: 0.85 #閾值因子,默認是0.85,若是閾值比最小值大,則自我保護模式開啓 (4)peer-eureka-nodes-update-interval-ms: 600000 # 集羣裏eureka節點的變化信息更新的時間間隔,單位爲毫秒,默認爲10 * 60 * 1000
(1)register-with-eureka: false #是否註冊到eureka (2)registry-fetch-interval-seconds: 30 # 從eureka服務器註冊表中獲取註冊信息的時間間隔(s),默認爲30秒 (3)fetch-registry: false # 實例是否在eureka服務器上註冊本身的信息以供其餘服務發現,默認爲true 若是是作高可用的發現服務那就要改爲true (4)instance-info-replication-interval-seconds: 30 # 複製實例變化信息到eureka服務器所須要的時間間隔(s),默認爲30秒 (5)initial-instance-info-replication-interval-seconds: 40 # 最初複製實例信息到eureka服務器所需的時間(s),默認爲40秒 (6)eureka-service-url-poll-interval-seconds: 300 #詢問Eureka服務url信息變化的時間間隔(s),默認爲300秒 (7)eureka-server-connect-timeout-seconds: 5 # eureka須要超時鏈接以前須要等待的時間,默認爲5秒 (8)eureka-server-total-connections: 200 #eureka客戶端容許全部eureka服務器鏈接的總數目,默認是200 (9)heartbeat-executor-thread-pool-size: 2 #心跳執行程序線程池的大小,默認爲2 (10)cache-refresh-executor-thread-pool-size: 2 # 執行程序緩存刷新線程池的大小,默認爲2