Spring-Cloud整合插件
eureka注册中心
1. 创建父maven项目,并添加依赖
<!-- 父模块依赖 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
</parent>
<!-- 编码和JDK版本 -->
<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>
<!-- web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 日志 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</dependency>
</dependencies>
<!-- spring-cloud版本 -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Greenwich.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
2. 新建eureka-server模块(注册中心服务器)
I. 添加 eureka 服务端依赖
<!-- web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- eureka 服务端依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
II. 创建启动类并添加注解
package com.ddz;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer//标记为eureka的服务端
public class MyEurekaServer {
public static void main(String[] args) {
SpringApplication.run(MyEurekaServer.class, args);
}
}
III. 在resources目录下添加 application.yml 配置文件
需要注意的是层级关系(缩进)
server:
port: 7001
spring:
application:
name: eureka-server
eureka:
client:
service-url:
defaultZone: http://localhost:7001/eureka/
fetch-registry: false #将eureka不注册到注册中心上, 运行环境不需要
register-with-eureka: false
server: #设置微服务没有发送心跳时自动剔除
enable-self-preservation: false #自动保护关闭,在发布环境中改为默认true
eviction-interval-timer-in-ms: 4000 #剔除的时间
instance:
prefer-ip-address: true # 展示Ip地址和端口号
instance-id: ${spring.cloud.client.ip-address}:${server.port} #Ip地址和端口号
IV. 在浏览器中访问网页链接: http://localhost:7001/
3. mysc-common 公共模块,存放entity和其他公共的文件
4. mysc-order-server 订单模块,用于查看用户的订单
I.添加 eureka客户端依赖
<!-- eureka客户端 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
II. 创建 application.yml 配置文件
server:
port: 9001
spring:
application:
name: order-server
eureka:
client:
service-url:
defaultZone: http://localhost:7001/eureka/ #,http://localhost:7002/eureka/ #添加到的eureka注册中心的地址
instance:
prefer-ip-address: true #在eureka地址页面中显示微服务的ip地址和端口
instance-id: ${spring.cloud.client.ip-address}:${server.port} #设置发送心跳时间间隔
lease-renewal-interval-in-seconds: 5 #设置没有发送心跳 微服务在eureka中保存的最大时间
lease-expiration-duration-in-seconds: 10
III. 在启动类添加注解
package com.ddz;
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;
@SpringBootApplication
@EnableDiscoveryClient//标记为eureka的客户端
public class MyOrderServer {
public static void main(String[] args) {
SpringApplication.run(MyOrderServer.class, args);
}
}
5. mysc-user-server 用户模块,远程访问订单模块并查看用户的订单
I. 添加依赖
<!-- eureka客户端 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
II. 创建 application.yml 配置文件
server:
port: 8001
spring:
application:
name: user-server
eureka:
client:
service-url:
defaultZone: http://localhost:7001/eureka/ #,http://localhost:7002/eureka/ #添加到的eureka注册中心的地址
instance:
prefer-ip-address: true #在eureka地址页面中显示微服务的ip地址和端口
instance-id: ${spring.cloud.client.ip-address}:${server.port} #设置发送心跳时间间隔
lease-renewal-interval-in-seconds: 5 #设置没有发送心跳 微服务在eureka中保存的最大时间
lease-expiration-duration-in-seconds: 10
III. 在启动类添加注解
package com.ddz;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient//标记为eureka的客户端
public class MyUserServer {
public static void main(String[] args) {
SpringApplication.run(MyUserServer.class, args);
}
}
IV. UserServiceImpl 用户接口实现类
package com.ddz.service.impl;
import com.ddz.entity.Order;
import com.ddz.service.UserService;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import javax.annotation.Resource;
import java.util.List;
@Service
public class UserServiceImpl implements UserService {
@Resource
private RestTemplate restTemplate;
@Resource
private DiscoveryClient discoveryClient;
@Override
public List<Order> findUserOrder(Integer uid) {
//从eureka注册中心中找注册过的微服务的ip地址和端口号
List<ServiceInstance> instances = discoveryClient.getInstances("order-server");
//获取第一个
ServiceInstance serviceInstance = instances.get(0);
String url = "http://" + serviceInstance.getHost() + ":" + serviceInstance.getPort() + "/findOrderByUser/" + uid;
System.out.println(url);
List<Order> orders = restTemplate.getForObject(url, List.class);
return orders;
}
}
V. 在启动类添加注解
@SpringBootApplication
@EnableEurekaClient//标记为eureka的客户端
public class MyUserServer {
public static void main(String[] args) {
SpringApplication.run(MyUserServer.class, args);
}
@Bean
public RestTemplate createRestTemplate() {
return new RestTemplate();
}
}
6. 在网页就可以访问用户模块,从而从订单模块中获取到订单的信息
链接: [link](http://localhost:8001/findUserOrder?uid=1)
Consul 替换 Eureka
consul与Eureka的区别
(1)一致性
Consul强一致性(CP)
Eureka保证高可用和最终一致性(AP)
(2)开发语言和使用
eureka就是个servlet程序,跑在servlet容器中
Consul则是go编写而成,安装启动即可
1. 下在Consul文件
地址:https://www.consul.io/downloads
2. 启动 Consul 服务
I. 找到下载好的 consul 文件目录下
II. 在 DOS 页面启动服务
consul agent -dev -client=0.0.0.0
III. 在需要注册的模块下的 application.yml 文件添加配置和依赖
server:
port: 9001 #端口号
spring:
application:
name: order-server #项目名称,在注册中心显示的名字
cloud:
consul:
host: 127.0.0.1 #表示Consul的Server的请求地址
port: 8500 #表示Consul的Server的端口
discovery: #服务注册与发现的相关配置
register: true
instdance-id: ${spring.application.name}-${server.port} #实例的唯一id(推荐必填),spring cloud官网文档的推荐,为了保证生成一个唯一的id ,也可以换成${spring.application.name}:${spring.cloud.client.ipAddress}
service-name: ${spring.application.name}
port: ${server.port} #
prefer-ip-address: true #开启ip地址注册
ip-address: ${spring.cloud.client.ip-address} #当前微服务的请求ip
<!-- consul注册中心 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
- spring-boot-starter-actuator 健康检查依赖于此包。
- spring-cloud-starter-consul-discovery Spring Cloud Consul 的支持。
IV. 在启动类上添加注册中心通用注解
package com.ddz;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient//标记为通用注册中心的客户端
public class MyOrderServer {
public static void main(String[] args) {
SpringApplication.run(MyOrderServer.class, args);
}
}