SpringCloud搭建
使用详解 Eureka是Spring Cloud Netflix微服务套件中的一部分,Eureka包含了服务器端和客户端 组件。服务器端,也被称作是服务注册中心,用于提供服务的注册与发现。Eureka支持高 可用的配置,当集群中有分片出现故障时,Eureka就会转入自动保护模式,它允许分片故 障期间继续提供服务的发现和注册,当故障分片恢复正常时,集群中其他分片会把他们的状 态再次同步回来。客户端组件包含服务消费者与服务生产者。在应用程序运行时,Eureka 客户端向注册中心注册自身提供的服务并周期性的发送心跳来更新它的服务租约。同时也可 以从服务端查询当前注册的服务信息并把他们缓存到本地并周期性的刷新服务状态。
搭建父工程cloud-parent
导入依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
<relativePath />
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<!--cloud-->
<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>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
搭建服务注册中心cloud-eureka
1、导入依赖
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
****2、application.yml****
server:
port: 8761
eureka:
instance:
hostname: localhost
client:
registerWithEureka: false
fetchRegistry: false
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
spring:
application:
name: eurka-server
(提示)格式稍有不正确,自行更改
3.在Application中使用@EnableEurekaServer
@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
3.搭建客户端服务eureka-client
1、导入依赖包
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>2.0.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
2、application.yml
server:
port: 8762
spring:
application:
name: eureka-client
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
3、在启用类上加入@EnableEurekaClient注解
@SpringBootApplication
@EnableEurekaClient
public class EurekaClientApplication {
public static void main(
String[]args{SpringApplication.run(EurekaClientApplication.class,args);
}
}
4、@RestController注解的对外服务接口将可通过注册发布的方式对外提供服务
@RestController(“user”)
public class UserController {
@GetMapping(“getUser”)
public User getUser(String userName,String userPass){
if(userName.equals(“hlq”) && userPass.equals(“hlq”)) {
User user = new User();
user.setUserName(“hlq”);
user.setUserPass(“hlq”);
user.setUserAge(“24”);
user.setUserId(“1”);
return user;
}else {
return null;
}
}
}
5、客户端服务测试