一、创建maven父工程(pom)
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.0http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.snowlink</groupId>
<artifactId>springCloud</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.5.RELEASE</version>
</parent>
<modules>
<module>springCloud-eureka-server</module>
<module>springCloud-eureka-client</module>
</modules>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.SR3</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
</project>
二、创建两个子工程
两个子工程都是springBoot的项目,一个是服务端(server),一个是客户端(client)
三、服务端配置
1、 由于服务端在启动类上需要加载@EnableEurekaServer注解,所以服务端的pom.xml文件中需要添加依赖:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
2、 服务端启动类
package org.springCloud.eureka.server;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
publicclass App
{
publicstaticvoid main( String[] args )
{
SpringApplication.run(App.class, args);
System.out.println( "Hello World!" );
}
}
3、特别注意:
1、com.sun.jersey.api.client.ClientHandlerException:java.net.ConnectException: Connection refused: connect
或者com.netflix.discovery.shared.transport.TransportException: Cannotexecute request on any known server
原因如下:
在默认设置下,Eureka服务注册中心也会将自己作为客户端来尝试注册它自己,所以我们需要禁用它的客户端注册行为。
禁止方式如下:在application.yml配置文件中增加以下内容
registerWithEureka:false
fetchRegistry:false
如下所示:
server:
port: 7070
eureka:
client:
registerWithEureka:false
fetchRegistry:false
serviceUrl:
defaultZone: http://localhost:7070/eureka/
instance:
hostname: localhost
重新启动即可。
浏览器访问localhost:7070就可以看到
四、客户端配置
1、application.yml配置:
server:
port: 8081
eureka:
client:
registerWithEureka:false
fetchRegistry:false
serviceUrl:
defaultZone: http://localhost:8080/eureka/
spring:
application:
name: service(服务名称)
2、入口类:
package org.springCloud.eureka.client;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* Hello world!
*
*/
@SpringBootApplication
@EnableDiscoveryClient
@RestController
publicclass App
{
publicstaticvoid main( String[] args )
{
SpringApplication.run(App.class, args);
System.out.println( "Hello World!" );
}
@GetMapping("/service")
public String service() {
return"service";
}
}
注意:
光是注册了服务还不行,这里可以再配一个网关,让服务调用有统一的入口.
网关用于请求转发和负载均衡,类似于Nginx,
五、网关配置:
1、 pom.xml配置:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zuul</artifactId>
</dependency>
2、 application.yml配置
server:
port: 8082
eureka:
client:
registerWithEureka:false
fetchRegistry:false
serviceUrl:
defaultZone: http://localhost:8080/eureka/
spring:
application:
name: gateway
zuul:
routes:
service: service(用于发布的服务,可以是多个)
3、启动类配置:
package org.gateway;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
/**
* Hello world!
*
*/
@SpringBootApplication
@EnableZuulProxy
@EnableEurekaClient
publicclass App
{
publicstaticvoid main( String[] args )
{
SpringApplication.run(App.class, args);
}
}
注意(异常):
java.lang.NoClassDefFoundError:org/springframework/boot/context/embedded/FilterRegistrationBean
原因分析:找不org.springframework.boot.context.embedded.FilterRegistrationBean,查看源码发现:在1.5.6版本中FilterRegistrationBean的包路径已经改为org.springframework.boot.web.servlet.org.springframework.boot.web.servlet.FilterRegistrationBean,所以引发错误。
解决办法:既然是引用了以前的包路径,说明是pom.xml文件的问题。检查发现spring-cloud-dependencies 的版本没有更新,改为Dalston.SR3即可解决问题。