目录
2、EurekaClient端的--服务提供者Service端:
PS:RestTemplate: 是有一种优雅的HTTP请求方式---参考二
单机版注册中心:
1、Eureka服务端:
pom
<!--eureka-server-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
yml:
server:
port: 7001
eureka:
instance:
hostname: localhost #eureka服务端的实例名字
client:
allow-redirects: false # 是否向注册中心注册本服务:true为是。注册中心不需要将自己注册进去
fetch-registry: false # false表示自己是注册中心,职责是维护服务实例,并不需要去检索服务
service-url:
#设置与eureka server交互的地址查询服务和注册服务都需要依赖这个地址,多个时用逗号隔开
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
主启动类 :
@EnableEurekaServer
@SpringBootApplication
@EnableEurekaServer //服务端:注册中心
public class EurekaApplication7001 {
public static void main(String[] args) {
SpringApplication.run(EurekaApplication7001.class,args);
}
}
2、EurekaClient端的--服务提供者端:
<!--eureka-client-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
server:
port: 8001
spring:
application:
name: cloud-payment-service # 服务名称
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: org.gjt.mm.mysql.Driver
url: jdbc:mysql://localhost:3306/db2019?useUnicode=true&characterEncoding=utf-8&useSSL=false
username: root
password: 123456789
eureka:
client:
register-with-eureka: true
fetchRegistry: true
service-url:
defaultZone: http://localhost:7001/eureka
主启动类添加注解:@EnableEurekaClient
3、EurekaClient端的--服务调用者端:
80Controller层
@Resource
public RestTemplate restTemplate;
public static final String PAY_URL="http://localhost:8001";
@GetMapping("/consumer/payment/get/{id}")</

本文详细介绍Eureka作为服务注册与发现组件,在微服务架构中如何实现服务注册、服务发现及集群搭建。涵盖Eureka服务端与客户端配置,以及通过RestTemplate进行服务调用的方法。
最低0.47元/天 解锁文章
1239

被折叠的 条评论
为什么被折叠?



