1. eureka-server
1.1 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>
<groupId>com.eureka.server</groupId>
<artifactId>eureka_server</artifactId>
<version>1.0.1-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<!-- 開啓基本的鑑權 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</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>Dalston.RC1</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>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
</project>
1.2 ====== application.yml=======
#server:
# port: 8761java
# 是否要開啓基本的鑑權
security:
basic:
enabled: true
user:
name: admin
password: 123456web
management:
security:
enabled: falseredis
spring:
application:
name: eureka
profiles:
active: native
cloud:
config:
server:
native:
search-locations: file:///D:/Users/xuzhi268/zhongchou/config_profilesspring
eureka:
instance:
hostname: eureka
lease-renewal-interval-in-seconds: 10000 #心跳鏈接時間(單位毫秒,默認是60*1000)
client:
register-with-eureka: false # 禁用eureka做爲客戶端註冊本身
fetch-registry: false
service-url:
default-zone: http://admin:123456@localhost:8761/eureka/,http://admin:123456@localhost:8766/eureka/ #多個用逗號隔開apache
1.3 ======= eureka-server 啓動類 ===========
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;bootstrap
@EnableEurekaServer
@EnableConfigServer
@SpringBootApplication
public class EurekaServerApplication {app
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
2 ========== ucenter 用戶中心 ==========
2.1 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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.cloud.eureka.client</groupId>
<artifactId>ucenter</artifactId>
<packaging>war</packaging>
<version>1.0.1-SNAPSHOT</version>
<name>ucenter Maven Webapp</name>
<url>http://maven.apache.org</url>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
<relativePath/>
</parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Camden.SR7</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</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.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
<build>
<finalName>ucenter</finalName>
</build>
</project>
2.2 application.yml 和 bootstrap.yml
eureka:
client:
serviceUrl:
#註冊中心的地址
defaultZone:http://admin:123456@localhost:8761/eureka/,http://admin:123456@localhost:8766/eureka/
server:
#當前服務端口號
port: 8762
spring:
application:
#當前應用名稱
name: ucenterdom
#配置中心地址
#禁用配置中心權限驗證
management:
security:
enabled: falsemaven
spring:
cloud:
config:
uri: http://admin:123456@localhost:8761/eureka/,http://admin:123456@localhost:8766/eureka/
2.3 啓動類
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.web.bind.annotation.RestController;spring-boot
@SpringBootApplication
@EnableEurekaClient
@RestController
public class UcenterApplicationRunner {
public static void main(String[] args) {
SpringApplication.run(UcenterApplicationRunner.class, args);
}
}
2.4 服務類
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.cloud.eureka.client.ucenter.biz.UserInfoService;
import com.cloud.eureka.client.ucenter.domain.UserDto;
import com.cloud.eureka.client.ucenter.network.request.QueryUserByIdRequest;
import com.cloud.eureka.client.ucenter.network.response.QueryUserByIdResult;
@RestController
@RefreshScope
public class UcenterInfoService {
private final Logger logger = LoggerFactory.getLogger(UcenterInfoService.class); @Autowired private DiscoveryClient discoveryClient; @Value(value = "${redis.host}") private String redisHost; @Value(value = "${redis.port}") private String redisPort; @RequestMapping(value = "/hello", method = RequestMethod.GET) public String index() { logger.info("redis host " + redisHost + ":" + redisPort); ServiceInstance instance = discoveryClient.getLocalServiceInstance(); logger.info("<=-=-=-= ucenter server access index() " + this.getClass().getSimpleName() + " " + Thread.currentThread().getName()); logger.info("/hello, host:" + instance.getHost() + ", service_id:" + instance.getServiceId()); return "Hello World"; } @Autowired private UserInfoService userInfoService; @RequestMapping(value = "/queryUserInfo", method = RequestMethod.GET) public UserDto queryUserInfo (@RequestParam("userId") Long userId) { logger.info("redis host " + redisHost + ":" + redisPort); logger.info("<=-=-=-= ucenter server access queryUserInfo " + this.getClass().getSimpleName() + " " + Thread.currentThread().getName()); logger.info("<<=-=-=-=>>恭喜用戶 " + userId + " 你鏈接成功<<=-=-=-=>>"); return userInfoService.queryUserInfo(); } @RequestMapping(value = "queryUserList", method = RequestMethod.POST) public List<UserDto> queryUserList() { logger.info("redis host " + redisHost + ":" + redisPort); logger.info("<=-=-=-= ucenter server access queryUserList " + this.getClass().getSimpleName() + " " + Thread.currentThread().getName()); List<UserDto> ulist = userInfoService.queryUserList(); return ulist; } @RequestMapping(value = "/queryUserById", method = RequestMethod.POST) public QueryUserByIdResult<UserDto> queryUserUserId(QueryUserByIdRequest request) { logger.info("redis host " + redisHost + ":" + redisPort); logger.info("<=-=-=-= ucenter server access queryUserInfo " + this.getClass().getSimpleName() + " " + Thread.currentThread().getName()); QueryUserByIdResult<UserDto> result = new QueryUserByIdResult<UserDto>(); UserDto dto = userInfoService.queryUserInfo(); result.setModel(dto); result.setRespCode("000"); result.setRespMsg("查詢成功"); return result; } }