目录
Eureka是什么?
创建EurekaServer
1.创建Eureka工程
2.启动类
3.配置文件
4.启动
5.启动成功
payment商家注册:
2.修改pom.xml
3.payment配置文件:
4.启动类
5.启动payment工程
消费者张三:order
1.order pom.xml(同上);
2.配置文件
3.启动类
4.controller
5.启动:
6. 成功
Eureka是什么?
张三想买东西,李四想卖东西,王五在中间拉近张三和李四,并在中间提供一些别的服务(比如李四提供的服务需要找王五注册,张三才能找王五去联系李四),王五叫Eureka。(话糙理不糙,官方话就不说了,我也刚学,不知道太多)。
"Eureka"来源于古希腊词汇,意为“发现了”。在软件领域,Eureka是Netflix在线影片公司开源的一个服务注册和发现组件,和其他的Netflix公司的服务组件(例如负载均衡,熔断器,网关等)一起,被Spring Cloud社区整合为Spring Cloud Netflix模块。
注意:Eureka2.x已经停更,解决方案推荐使用Nacos作为替换方案
话不多说,直接上手,前提条件https://blog.csdn.net/lifeisworship/article/details/123144410https://blog.csdn.net/lifeisworship/article/details/123144410
上一篇提供了:
1.实体类:a_cloud_common
2.服务提供者李四:b_cloud_payment port:9001
3.服务消费者张三:c_cloud_order port:9002
下面是中间人王五:d_cloud_Eureka port:9004
创建EurekaServer
步骤:
1,创建Eureka工程;
2.启动类添加注解:@EnableEurekaServer
3.yml配置文件配置;
4.启动 如下:
1.创建Eureka工程
2.启动类
加@EnableEurekaServer 声明当前应用为Eureka Server
@EnableEurekaServer
@SpringBootApplication
public class DCloudEurekaApplication {
public static void main(String[] args) {
SpringApplication.run(DCloudEurekaApplication.class, args);
}
}
3.配置文件
改名为application.yml
server:
port: 9004
spring:
application:
name: eureka-server
eureka:
client:
service-url:
# eureka 服务地址,如果是集群的话;需要指定其它集群eureka地址
defaultZone: http://127.0.0.1:9004/eureka
# 不注册自己
register-with-eureka: false
# 不拉取服务
fetch-registry: false
4.启动
5.启动成功
payment商家注册:
服务提供者:1.引入Eureka;2.配置文件:3.启动类添加注解
2.修改pom.xml
1.8
2021.0.1
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
org.springframework.cloud
spring-cloud-dependencies
${spring-cloud.version}
pom
import
3.payment配置文件:
server:
port: 9001
spring:
application:
name: cloud-payment-service #指定应用名称,将来会作为服务的id使用
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:9004/eureka
fetch-registry: true
register-with-eureka: true
4.启动类
@SpringBootApplication
@EnableDiscoveryClient
public class BCloudPaymentApplication {
public static void main(String[] args) {
SpringApplication.run(BCloudPaymentApplication.class, args);
}
}
5.启动payment工程
server:
port: 9001
spring:
application:
name: cloud-payment-service #指定应用名称,将来会作为服务的id使用
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:9004/eureka
fetch-registry: true
register-with-eureka: true
4.启动类
@SpringBootApplication
@EnableDiscoveryClient
public class BCloudPaymentApplication {
public static void main(String[] args) {
SpringApplication.run(BCloudPaymentApplication.class, args);
}
}
5.启动payment工程
(这时,Eureka已经启动)
刷新Eureka页面
配置成功
消费者张三:order
基本步骤:
1,引入Eureka(同上);
2.配置文件:不配置(register-with-eureka: false fetch-registry: false):
3.启动类添加注解(使用DiscoveryClient,解决了url地址硬编码到了代码中,不方便后期维护的问题)
1.order pom.xml(同上);
1.8
2021.0.1
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
org.springframework.cloud
spring-cloud-dependencies
${spring-cloud.version}
pom
import
2.配置文件
server:
port: 9002
spring:
application:
name: cloud-order-service
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:9004/eureka
3.启动类
@SpringBootApplication
@EnableDiscoveryClient
public class CCloudOrderApplication {
public static void main(String[] args) {
SpringApplication.run(CCloudOrderApplication.class, args);
}
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
4.controller
package com.wjg.controller;
import com.wjg.Payment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import javax.annotation.Resource;
import java.util.List;
@RestController
@RequestMapping("/order")
public class OrderController {
@Autowired
private RestTemplate restTemplate;
@Resource
private DiscoveryClient discoveryClient;
@GetMapping("/payment/{id}")
public ResponseEntity getPaymentById(@PathVariable("id") Integer id) {
String url = "http://localhost:9001/payment/" + id;
List serviceInstances = discoveryClient.getInstances("cloud-payment-service");
ServiceInstance serviceInstance = serviceInstances.get(0);
url = "http://" + serviceInstance.getHost() + ":" + serviceInstance.getPort() + "/payment/" + id;
Payment payment = restTemplate.getForObject(url, Payment.class);
return ResponseEntity.ok(payment);
}
}
5.启动:
package com.wjg.controller;
import com.wjg.Payment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import javax.annotation.Resource;
import java.util.List;
@RestController
@RequestMapping("/order")
public class OrderController {
@Autowired
private RestTemplate restTemplate;
@Resource
private DiscoveryClient discoveryClient;
@GetMapping("/payment/{id}")
public ResponseEntity getPaymentById(@PathVariable("id") Integer id) {
String url = "http://localhost:9001/payment/" + id;
List serviceInstances = discoveryClient.getInstances("cloud-payment-service");
ServiceInstance serviceInstance = serviceInstances.get(0);
url = "http://" + serviceInstance.getHost() + ":" + serviceInstance.getPort() + "/payment/" + id;
Payment payment = restTemplate.getForObject(url, Payment.class);
return ResponseEntity.ok(payment);
}
}



