SpringCloud EureKa服務集羣

1、pom文件(由於我服務裏用到redis,之前在 SpringBoot整合redis及遇到的版本問題 裏講過,springBoot2.1.x版本跟redis兼容有問題,所以這裏使用2.0.6)

<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.6.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.luzx.EurekaClient</groupId>
	<artifactId>EurekaClient</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>EurekaClient</name> 
	<description>Demo project for Spring Boot</description>

	<properties>
		<java.version>1.8</java.version>
		<spring-cloud.version>Finchley.SR2</spring-cloud.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
		</dependency>

		<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>
		
		<!-- 熱部署 -->
		<dependency>
        	<groupId>org.springframework.boot</groupId>
        	<artifactId>spring-boot-devtools</artifactId>
        	<optional>true</optional>
    	</dependency>
		
		<!-- redis緩存 -->
        <dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-redis</artifactId>
		</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>

2、配置文件

#springCloud
#是否註冊自身到eureka服務器
eureka.client.registerWithEureka=true
#是否從eureka服務器獲取註冊信息
eureka.client.fetchRegistry=true
#註冊地址
eureka.client.serviceUrl.defaultZone=http://xx.xx.xx.xx:9980/eureka/
# 每間隔1s,向服務端發送一次心跳,證明自己依然存活
eureka.client.lease-renewal-interval-in-seconds=1
#告訴服務端,如果我10s之內沒有給你發心跳,就代表我“死”了,將我踢出掉
eureka.client.lease-expiration-duration-in-seconds=10
#網卡禁用
#spring.cloud.inetutils.ignored-interfaces[0]=eth0


# Redis數據庫索引(默認爲0)
spring.redis.database=0
# Redis服務器地址
#spring.redis.host=
# Redis服務器連接端口
#spring.redis.port=7000
#集羣服務器
spring.redis.cluster.nodes=
# Redis服務器連接密碼(默認爲空)集羣密碼
spring.redis.password=
# 連接池最大連接數(使用負值表示沒有限制)
spring.redis.jedis.pool.max-active=8
# 連接池最大阻塞等待時間(使用負值表示沒有限制)
spring.redis.jedis.pool.max-wait=-1
# 連接池中的最大空閒連接
spring.redis.jedis.pool.max-idle=8
# 連接池中的最小空閒連接
spring.redis.jedis.pool.min-idle=0
# 連接超時時間(毫秒)
spring.redis.timeout=10000


#日誌
logging.level.com.luzx.EurekaClient=DEBUG

#允許覆蓋
spring.main.allow-bean-definition-overriding=true

spring.application.name=EurekaDotaServiceClient

#hostname
eureka.instance.hostname= ${spring.cloud.client.ipaddress}
#使用ip地址註冊
eureka.instance.prefer-ip-address=true
eureka.instance.ip-address=xx.xx.xx.xx


#端口
server.port=8081
#server.port=8082

3、啓動類添加註釋@EnableEurekaClient或者@EnableDiscoveryClient

@SpringBootApplication
@EnableEurekaClient
public class EurekaClientApplication {

	public static void main(String[] args) {
		SpringApplication.run(EurekaClientApplication.class, args);
	}

}

4、跟往常一樣正常編寫Controller層接口就行

package com.luzx.EurekaClient.EurekaClient.controller;

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.luzx.EurekaClient.EurekaClient.service.DotaService;

@Controller
public class DotaController {

	@Autowired
	private DotaService dotaService;
	@Value("${server.port}")
	private String port;
	

	
	@RequestMapping("/getHeroMsgById")
	@ResponseBody
	public Map<String,Object> getHeroMsgById(HttpServletRequest request){
		Map<String,Object> result = new HashMap<String,Object>();
		result.put("status", false);
		result.put("port", port);
		try {
			Optional<String> heroId = Optional.ofNullable(request.getParameter("heroId"));
			heroId.ifPresent((value) -> {
				result.put("status", true);
				result.put("result", dotaService.getHeroMsgById(value));
			});
		}
		catch(Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	
	
}

5、修改配置文件的端口號,啓動多個服務,實現集羣

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章