一.单机版eureka的搭建
- 使用restTemple进行消息的传递
- 搭建负载均衡(进行轮训测试)
1.服务端的构建
1.1导入依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<!--图形化界面所需要的依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
1.2编写application.yml文件
------单机版eureka注册的地址是自己
server:
port: 7001
eureka:
instance:
hostname: localhost
client:
#不向eureka注册自己
register-with-eureka: false
#自己是服务端,不需要被检测
fetch-registry: false
service-url:
defaultZone: http://localhost:7001/eureka/ 单机版
#defaultZone: http://localhost:7002/eureka/ #集群配置
server:
#eureka的自我保护机制
enable-self-preservation: true
#服务名称
spring:
application:
name: eureka-7001
1.3主启动类添加注解
添加eurekaserver服务端的标识
2.服务的提供者
2.1导入依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.example</groupId>
<artifactId>cloud-commons</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.3.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
<version>2.3.0.RELEASE</version>
</dependency>
2.2编写application.yml
server:
port: 8001
#注册到eureka
eureka:
client:
register-with-eureka: true
fetch-registry: true
service-url:
#defaultZone: http://localhost:7001/eureka,http://localhost:7002/eureka
defaultZone: http://localhost:7001/eureka 单机版
2.3配置主启动类
2.4浏览器访问 localhost:7001
会出现服务注册进去的信息
2.Eureka集群的搭建
2.1 eurekaServer端的修改
修改yml文件
- 构建相同的eurekaServer服务端
- 两个服务相互注册
server:
port: 7002
eureka:
instance:
hostname: localhost
client:
register-with-eureka: false #不注册自己
fetch-registry: false #自己是服务,不被检索
service-url:
defaultZone: http://localhost:7001/eureka/
spring:
application:
name: eureka-7002
2.2 服务的提供者端的修改
yml文件的修改
注册到两台eureka中
server:
port: 8001
#注册到eureka
eureka:
client:
register-with-eureka: true
fetch-registry: true
service-url:
defaultZone: http://localhost:7001/eureka,http://localhost:7002/eureka
#defaultZone: http://localhost:7001/eureka 单机版
3.eureka注册中心的使用案例(实现如下模式)
1.搭建提供者集群(具体业务代码就是一个简单的crud)
重新构建一个一样的服务提供者就行,端口修改一下就行
@RestController
@RequestMapping("/payment")
@Slf4j
public class PaymentController {
@Resource
private PaymentService paymentService;
@Value("${server.port}")
private String serverPort;
@Resource
private DiscoveryClient discoveryClient;
@PostMapping("/create")
public CommentResult creat(@RequestBody Payment payment){
int creat = paymentService.creat(payment);
log.info("*******插入结果"+creat+"笑脸");
if (creat>0){
return new CommentResult(200,"插入成功"+serverPort,creat);
}
return new CommentResult(404,"插入失败",null);
}
@GetMapping("/get/{id}")
public CommentResult getById(@PathVariable("id") Long id){
Payment paymentById = paymentService.getPaymentById(id);
log.info("查询成功:"+paymentById);
if (paymentById !=null){
return new CommentResult(200,"查询成功"+serverPort,paymentById);
}
return new CommentResult(404,"查询失败",null);
}
}
2.搭建消费者80
2.1导入依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.example</groupId>
<artifactId>cloud-commons</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.3.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
<version>2.3.0.RELEASE</version>
</dependency>
2.2注册到eureka集群
server:
port: 80
spring:
application:
name: cloud-consumer
eureka:
client:
register-with-eureka: true
fetch-registry: true
service-url:
defaultZone: http://localhost:7001/eureka,http://localhost:7002/eureka
2.3构建restTemple进行跨服务访问
2.4 Controller层进行调用
@RestController
@RequestMapping("/consumer")
public class ConsumerController {
#通过注册到eureka的服务名称进行访问
private static final String URL="http://CLOUD-PAYMENT";
@Resource
private RestTemplate restTemplate;
@GetMapping("/payment/create")
public CommentResult<Payment> create(Payment payment){
return restTemplate.postForObject(URL+"/payment/create",payment, CommentResult.class);
}
@GetMapping("/payment/get/{id}")
public CommentResult<Payment> get(@PathVariable("id") Long id){
return restTemplate.getForObject(URL+"/payment/get/"+id, CommentResult.class);
}
}
3.服务的提供者进行负载均衡搭建
只需要在消费者方开启负载均衡
进行测试只需要在服务的提供端显示各自的端口号就可以看见效果
@Value("${server.port}")
private String serverPort;
@PostMapping("/create")
public CommentResult creat(@RequestBody Payment payment){
int creat = paymentService.creat(payment);
log.info("*******插入结果"+creat+"笑脸");
if (creat>0){
#添加服务的端口号进行负载均衡效果查看
return new CommentResult(200,"插入成功"+serverPort,creat);
}
return new CommentResult(404,"插入失败",null);
}
4.actuator图形化界面的修改
#注册到eureka
eureka:
client:
register-with-eureka: true
fetch-registry: true
service-url:
defaultZone: http://localhost:7001/eureka,http://localhost:7002/eureka
#defaultZone: http://localhost:7001/eureka 单机版
#修改注册到eureka服务上的服务名和ip地址的显示
instance:
instance-id: payment8081
prefer-ip-address: true
8001端口服务的发现discovery 获取注册到eureka服务的信息
1.配置启动类注解
2.编写cotroller
//注入discovery
@Resource
private DiscoveryClient discoveryClient;
@GetMapping("/discovery")
public Object getDiscovery(){
//获取服务的名称
List<String> services = discoveryClient.getServices();
for (String service : services) {
log.info("*******"+service);
}
//获取服务的主机,端口,地址
List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-PAYMENT");
for (ServiceInstance instance : instances) {
String host = instance.getHost();
int port = instance.getPort();
URI uri = instance.getUri();
log.info(host+"\t"+port+"\t"+uri);
}
return discoveryClient;
}
3.得到的信息如下
5.eureka的自我保护机制如何关闭(不建议关闭)
eureka:
instance:
hostname: localhost
client:
#不向eureka注册自己
register-with-eureka: false
#自己是服务端,不需要被检测
fetch-registry: false
service-url:
#defaultZone: http://localhost:7001/eureka/ 单机版
defaultZone: http://localhost:7002/eureka/ #集群配置
server:
#eureka的自我保护机制----修改为false就关闭了
enable-self-preservation: false