eclipse上springCloud分布式微服务搭建-⼲货⼀、创建maven⽗⼯程(pom)
<?xml version="1.0"encoding="UTF-8"?>
<project xmlns="/POM/4.0.0"xmlns:xsi="/2001/XMLSchema-
instance"xsi:schemaLocation="/POM/4.0.0/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注解,所以服务端的l⽂件中需要添加依赖:
<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.cloudflix.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.ConnectException: Connection refused: connect
或者comflix.ansport.TransportException: Cannotexecute request on any known server
原因如下:
在默认设置下,Eureka服务注册中⼼也会将⾃⼰作为客户端来尝试注册它⾃⼰,所以我们需要禁⽤它的客户端注册⾏为。禁⽌⽅式如下:在l配置⽂件中增加以下内容
registerWithEureka:false
fetchRegistry:false
如下所⽰:
server:
port: 7070
eureka:
client:
registerWithEureka:false
fetchRegistry:false
serviceUrl:
defaultZone: localhost:7070/eureka/
instance:
hostname: localhost
重新启动即可。
浏览器访问localhost:7070就可以看到
四、客户端配置
1、l配置:
server:
port: 8081
eureka:
client:
registerWithEureka:false
fetchRegistry:false
serviceUrl:
defaultZone: 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.cloudflix.eureka.EnableEurekaClient; import org.springframework.cloudflix.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";
}
}
spring到底是干啥的注意:
光是注册了服务还不⾏,这⾥可以再配⼀个⽹关,让服务调⽤有统⼀的⼊⼝.⽹关⽤于请求转发和负载均衡,类似于Nginx,
五、⽹关配置:
1、 l配置:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zuul</artifactId>
</dependency>
2、 l配置
server:
port: 8082
eureka:
client:
registerWithEureka:false
fetchRegistry:false
serviceUrl:
defaultZone: 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.cloudflix.eureka.EnableEurekaClient; import org.springframework.cloudflix.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/FilterRegistr 原因分析:不org.t.embedded.FilterRegistrationBean,查看源码发现:在1.5.6版本中FilterRegistrationBean的包路径已经改为
org.springframework.boot.springframework.boot.web.servlet.FilterRegistrationBean,
所以引发错误。
解决办法:既然是引⽤了以前的包路径,说明是l⽂件的问题。检查发现spring-cloud-dependencies 的版本没有更新,改为Dalston.SR3即可解决问题。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。