spring cloud分为好几个模块,现在我先学习neflix模块,这个模块是spring cloud的核心模块,首先使用maven将eureka client和eureka server的相关架包下载下来:
eureka server:
<?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>com.example</groupId>
<artifactId>springcloud</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>springcloud</name>
<description>springcloudtest</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
<!-- spring boot test-->
<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>
eureka client:
<?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>eurekaclient</groupId> <artifactId>eurekaclient.com</artifactId> <version>1.0-SNAPSHOT</version> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-netflix</artifactId> <version>1.4.6.BUILD-SNAPSHOT</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> </dependencies><repositories> <repository> <id>spring-snapshots</id> <name>Spring Snapshots</name> <url>https://repo.spring.io/libs-snapshot</url> <snapshots> <enabled>true</enabled> </snapshots> </repository> </repositories> </project>
将这两个maven同时下载下来之后我们开始对eureka server进行部署 :
首先,我们先在resouce下面生成一个application.yml文件,因为是spring boot,所以一切都是自动配置的,我们配置文件如下:
server:
port: 8790
eureka:
instance:
hostname: localhost
client:
registerWithEureka: false
fetchRegistry: false
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
这里面,其实eureka server也是一个eureka client,这里面defaultZone表示他发送的注册的地址,我这里defaultZone的地址写的是它自己的地址,如果这个地址写的是其他 eureka server地址那么就会形成一个双***的情况,同时如果registerWithEureka和fetchRegistry为false的时候表示不去注册,注意这样就形成不了双***情况。这里的server.port表示这个程序申请的端口,然后我们创建一个启动类,
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@EnableEurekaServer
@SpringBootApplication
public class SpringcloudApplication {
public static void main(String[] args) {
SpringApplication.run(SpringcloudApplication.class, args);
}
}
@EnableEurekaServer和@SpringBootApplication两个注释不能或缺,一个是eureka server启动注解一个是spring boot启动注解,等启动完成我们可以访问localhost:8790。得到如下图:
这就表示我们的eureka server配置成功了。
接下来是eureka client的配置文件:
server:
port: 8060
servletPath: /
eureka:
client:
service-url:
defaultZone: http://localhost:8790/eureka/
enabled: true
instance:
healthCheckUrlPath: ${server.servletPath}/health
statusPageUrlPath: ${server.servletPath}/info
homePageUrl: ${server.servletPath}/
healthcheck:
enabled: true
spring:
application:
name: eureka-hello
这里eureka.enabled必须是true,否则就不会去想eureka server注册中心发送注册信息了,eureka.client.service-url.defaultZone写eureka server的注册地址。
同样创建一个启动类:
package com.eurekaclient.app;
import com.netflix.appinfo.InstanceInfo;
import com.netflix.discovery.EurekaClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
/**
* Created by 用户 on 2018/7/6.
*/
@RestController
@SpringBootApplication
@EnableEurekaClient
public class EurekaClientApplication {
@Value("service.port")
private String port;
@Autowired
private EurekaClient discoveryClient;
public static void main(String[] args){
new SpringApplicationBuilder(EurekaClientApplication.class).web(true).run(args);
}
@RequestMapping("/hi")
public String hello(@RequestParam String name){
return "hello world,"+name+","+"I am from port:"+port;
}
@RequestMapping("/info")
public String info(){
return "hello, I am eureka client, i am from prot:"+port;
}
@RequestMapping("/health")
public String health(){
return "i am health,I am from port:"+port;
}
@RequestMapping("/instance")
public String instance(){
InstanceInfo instanceInfo = discoveryClient.getNextServerFromEureka("EUREKA-HELLO",false);
return instanceInfo.getHomePageUrl();
}
}
@RestController表示这是一个resful的controller的注解 @SpringBootApplication表示启动springboot @EnableEurekaClient表示开启eurekaclient类。完成启动后我们可以在eureka server的页面上看到:
这里的EUREKA-HELLO就是上面配置文件中对应的spring.application.name,好了到这里我们就完成了eurekaclient和eurekaserver的配置了。