当前位置:网站首页>ss-4.2 多个eureka集群案例
ss-4.2 多个eureka集群案例
2022-08-03 05:09:00 【lhorse003】
cloud-eureka-server7001
2个服务 2个提供器 1个消费者 http://127.0.0.1:8001/actuator/health //查看当前服务状态 http://127.0.0.1:8001/payment/discovery 发现服务
│ pom.xml
│
├─src
│ ├─main
│ │ ├─java
│ │ │ └─com
│ │ │ └─cmk
│ │ │ └─springCloud
│ │ │ EurekaMain7001.java //jetbrains://idea/navigate/reference?project=zy2020&path=com/cmk/springCloud/EurekaMain7001.java:12:1
│ │ │
│ │ └─resources
│ │ application.yml
pom.xml
<dependencies>
<!--eureka-server-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.cmk.springcoloud</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>${project.version}</version>
</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>
<!--一般为通用配置-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
EurekaMain7001.java
@SpringBootApplication
@EnableEurekaServer
public class EurekaMain7001 {
public static void main(String[] args) {
SpringApplication.run(EurekaMain7001.class,args);
}
}
application.yml
server:
port: 7001
eureka:
instance:
hostname: eureka7001.com #eureka服务端的实例名称
client:
register-with-eureka: false #false表示不向注册中心注册自己
fetch-registry: false #false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
service-url:
#设置与Eureka server交互的地址查询服务和注册服务都需要依赖这个地址
defaultZone: http://eureka7002.com:7002/eureka/
cloud-eureka-server7002
│ pom.xml
│
├─src
│ ├─main
│ │ ├─java
│ │ │ └─com
│ │ │ └─cmk
│ │ │ └─springCloud
│ │ │ EurekaMain7002.java //jetbrains://idea/navigate/reference?project=zy2020&path=com/cmk/springCloud/EurekaMain7002.java:14:1
│ │ │
│ │ └─resources
│ │ application.yml
pom.xml
<dependencies>
<!--eureka-server-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.cmk.springcoloud</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>${project.version}</version>
</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>
<!--一般为通用配置-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
EurekaMain7002.java
@SpringBootApplication
@EnableEurekaServer
public class EurekaMain7002 {
public static void main(String[] args) {
SpringApplication.run(EurekaMain7002.class,args);
}
}
application.yml
server:
port: 7002
eureka:
instance:
hostname: eureka7002.com #eureka服务端的实例名称
client:
register-with-eureka: false #false表示不向注册中心注册自己
fetch-registry: false #false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
service-url:
#设置与Eureka server交互的地址查询服务和注册服务都需要依赖这个地址
defaultZone: http://eureka7001.com:7001/eureka/
cloud-provider-payment8001
│ pom.xml
│
├─src
│ ├─main
│ │ ├─java
│ │ │ └─com
│ │ │ └─cmk
│ │ │ └─springCloud
│ │ │ │ PaymentMain8001.java //jetbrains://idea/navigate/reference?project=zy2020&path=com/cmk/springCloud/PaymentMain8001.java:14:1
│ │ │ │
│ │ │ ├─controller
│ │ │ │ PaymentController.java
│ │ │ │
│ │ │ ├─dao
│ │ │ │ PaymentDao.java
│ │ │ │
│ │ │ └─service
│ │ │ │ PaymentService.java
│ │ │ │
│ │ │ └─impl
│ │ │ PaymentServiceImpl.java
│ │ │
│ │ └─resources
│ │ │ application.yml
│ │ │
│ │ └─mapper
│ │ PaymentMapper.xml
pom.xml
<dependencies>
<!--eureka-client-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.cmk.springcoloud</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>${project.version}</version>
</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>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
</dependencies>
PaymentMain8001.java
@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
public class PaymentMain8001 {
public static void main(String[] args) {
SpringApplication.run(PaymentMain8001.class,args);
}
}
PaymentController.java
@RestController
@Slf4j
public class PaymentController {
@Value("${server.port}")
private String serverPort;
@Resource
private DiscoveryClient discoveryClient;
@Resource
private PaymentService paymentService;
@PostMapping("/payment/create")
public CommonResult create(@RequestBody Payment payment){
int result = paymentService.create(payment);
log.info("****插入结果"+result);
if (result>0){
return new CommonResult(200,"插入数据库成功,服务端口:"+serverPort,result);
}else{
return new CommonResult(400,"插入数据库失败",null);
}
}
@GetMapping("/payment/get/{id}")
public CommonResult getPaymentById(@PathVariable("id") long id){
Payment result = paymentService.getPaymentById(id);
log.info("****查询结果"+result+"456");
if (result !=null){
return new CommonResult(200,"查询数据库成功,服务端口:"+serverPort,result);
}else{
return new CommonResult(400,"查询数据库失败",null);
}
}
@GetMapping("/payment/discovery")
public Object discovery(){
List<String> services = discoveryClient.getServices();
for (String element : services) {
log.info("*****:"+element);
}
List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-PAYMENT-SERVICE");
for (ServiceInstance instance : instances) {
log.info(instance.getServiceId()+"\t"+instance.getHost()+"\t"+instance.getPort()+"\t"+instance.getUri());
}
return this.discoveryClient;
}
}
PaymentDao.java
@Mapper
public interface PaymentDao {
int create(Payment payment);
Payment getPaymentById(@Param("id") long id);
}
service
PaymentService.java
@Mapper
public interface PaymentService {
int create(Payment payment);
Payment getPaymentById(@Param("id") long id);
}
impl > PaymentServiceImpl.java
@Service
public class PaymentServiceImpl implements PaymentService {
@Resource
private PaymentDao paymentDao;
public int create(Payment payment){
return paymentDao.create(payment);
}
public Payment getPaymentById(@Param("id") long id){
return paymentDao.getPaymentById(id);
}
}
resources
application.yml
server:
port: 8001
spring:
application:
name: cloud-payment-service
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC
username: root
password: 123456
mybatis:
mapper-locations: classpath:mapper/*.xml
type-aliases-package: com.cmk.springCloud.entities
eureka:
client:
#表示是否将自己注册进 EurekaServer默认为true
register-with-eureka: true
#是否从EurekaServer抓取已有的注册信息,默认为true。单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡
fetch-registry: true
service-url:
# defaultZone: http://localhost:7001/eureka
defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka #集群版
instance:
instance-id: payment8001
prefer-ip-address: true #访问路径可以显示IP地址
mapper > PaymentMapper.xml
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.cmk.springCloud.service.PaymentService">
<insert id="create" parameterType="com.cmk.springCloud.entities.Payment">
insert into payment(serial) values (#{serial});
</insert>
<select id="getPaymentById" parameterType="long" resultMap="BaseResultMap">
select * from payment where id=#{id}
</select>
<resultMap id="BaseResultMap" type="com.cmk.springCloud.entities.Payment">
<id column="id" property="id" jdbcType="BIGINT"/>
<id column="serial" property="serial" jdbcType="VARCHAR"/>
</resultMap>
</mapper>
cloud-provider-payment8002
│ pom.xml
│
├─src
│ ├─main
│ │ ├─java
│ │ │ └─com
│ │ │ └─cmk
│ │ │ └─springCloud
│ │ │ │ PaymentMain8002.java //jetbrains://idea/navigate/reference?project=zy2020&path=com/cmk/springCloud/PaymentMain8002.java:13:1
│ │ │ │
│ │ │ ├─controller
│ │ │ │ PaymentController.java
│ │ │ │
│ │ │ ├─dao
│ │ │ │ PaymentDao.java
│ │ │ │
│ │ │ └─service
│ │ │ │ PaymentService.java
│ │ │ │
│ │ │ └─impl
│ │ │ PaymentServiceImpl.java
│ │ │
│ │ └─resources
│ │ │ application.yml
│ │ │
│ │ └─mapper
│ │ PaymentMapper.xml
pom.xml
<dependencies>
<!--eureka-client-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.cmk.springcoloud</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>${project.version}</version>
</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>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
</dependencies>
PaymentMain8002.java
@SpringBootApplication
@EnableEurekaClient
public class PaymentMain8002 {
public static void main(String[] args) {
SpringApplication.run(PaymentMain8002.class,args);
}
}
PaymentController.java
@RestController
@Slf4j
public class PaymentController {
@Value("${server.port}")
private String serverPort;
@Resource
private PaymentService paymentService;
@PostMapping("/payment/create")
public CommonResult create(@RequestBody Payment payment){
int result = paymentService.create(payment);
log.info("****插入结果"+result);
if (result>0){
return new CommonResult(200,"插入数据库成功,服务端口:"+serverPort,result);
}else{
return new CommonResult(400,"插入数据库失败",null);
}
}
@GetMapping("/payment/get/{id}")
public CommonResult getPaymentById(@PathVariable("id") long id){
Payment result = paymentService.getPaymentById(id);
log.info("****查询结果"+result+"456");
if (result !=null){
return new CommonResult(200,"查询数据库成功,服务端口:"+serverPort,result);
}else{
return new CommonResult(400,"查询数据库失败",null);
}
}
}
PaymentDao.java
@Mapper
public interface PaymentDao {
int create(Payment payment);
Payment getPaymentById(@Param("id") long id);
}
service
PaymentService.java
@Mapper
public interface PaymentService {
int create(Payment payment);
Payment getPaymentById(@Param("id") long id);
}
impl > PaymentServiceImpl.java
@Service
public class PaymentServiceImpl implements PaymentService {
@Resource
private PaymentDao paymentDao;
public int create(Payment payment){
return paymentDao.create(payment);
}
public Payment getPaymentById(@Param("id") long id){
return paymentDao.getPaymentById(id);
}
}
resources
application.yml
server:
port: 8002
spring:
application:
name: cloud-payment-service
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC
username: root
password: 123456
mybatis:
mapper-locations: classpath:mapper/*.xml
type-aliases-package: com.cmk.springCloud.entities
eureka:
client:
#表示是否将自己注册进 EurekaServer默认为true
register-with-eureka: true
#是否从EurekaServer抓取已有的注册信息,默认为true。单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡
fetch-registry: true
service-url:
# defaultZone: http://localhost:7001/eureka
defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka #集群版
instance:
instance-id: payment8002
prefer-ip-address: true #访问路径可以显示IP地址
mapper > PaymentMapper.xml
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.cmk.springCloud.service.PaymentService">
<insert id="create" parameterType="com.cmk.springCloud.entities.Payment">
insert into payment(serial) values (#{serial});
</insert>
<select id="getPaymentById" parameterType="long" resultMap="BaseResultMap">
select * from payment where id=#{id}
</select>
<resultMap id="BaseResultMap" type="com.cmk.springCloud.entities.Payment">
<id column="id" property="id" jdbcType="BIGINT"/>
<id column="serial" property="serial" jdbcType="VARCHAR"/>
</resultMap>
</mapper>
com-consumer-order80
│ pom.xml
│
├─src
│ ├─main
│ │ ├─java
│ │ │ └─com
│ │ │ └─cmk
│ │ │ └─consumerOrder80
│ │ │ │ OrderMain80.java //jetbrains://idea/navigate/reference?project=zy2020&path=com/cmk/consumerOrder80/OrderMain80.java:13:1
│ │ │ │
│ │ │ ├─config
│ │ │ │ ApplicationContextConfig.java
│ │ │ │
│ │ │ └─controller
│ │ │ OrderController.java
│ │ │
│ │ └─resources
│ │ application.yml
pom.xml
<dependencies>
<!--eureka-client-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.cmk.springcoloud</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>${project.version}</version>
</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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
</dependencies>
OrderMain80.java
@SpringBootApplication
@EnableEurekaClient
public class OrderMain80 {
public static void main(String[] args) {
SpringApplication.run(OrderMain80.class,args);
}
}
ApplicationContextConfig.java
@Configuration
public class ApplicationContextConfig {
@Bean
@LoadBalanced //使用@Loaibalanced注解赋予RestTemplate负载均衡的能力
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
}
OrderController.java
@RestController
@Slf4j
public class OrderController {
// public static final String PAYMENT_URL = "http://localhost:8001";
public static final String PAYMENT_URL = "http://CLOUD-PAYMENT-SERVICE";
@Resource
private RestTemplate restTemplate;
@GetMapping("/consumer/payment/create")
public CommonResult<Payment> create(Payment payment){
System.out.println(payment);
return restTemplate.postForObject(PAYMENT_URL+"/payment/create",payment,CommonResult.class);
}
@GetMapping("consumer/payment/get/{id}")
public CommonResult getPayment(@PathVariable("id") long id){
return restTemplate.getForObject(PAYMENT_URL+"/payment/get/"+id,CommonResult.class);
}
}
application.yml
server:
port: 80
spring:
application:
name: cloud-order-service
eureka:
client:
register-with-eureka: true
fetch-registry: true
service-url:
# defaultZone: http://localhost:7001/eureka
defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka
边栏推荐
- Fluorescent marker peptides FITC/AMC/FAM/Rhodamine TAMRA/Cy3 / Cy5 / Cy7 - Peptide
- DFS's complement to pruning
- 1079 延迟的回文数 (20 分)
- IO process thread -> thread -> day5
- 设计模式——组合模式、享元模式(Integer缓存)(结构型模式)
- Concepts and Methods of Exploratory Testing
- Object类与常用API
- Install IIS services (Internet Information Services (Internet Information Services, abbreviated IIS, Internet Information Services)
- idea uses @Autowired annotation to explain the reasons and solutions
- Gradle的安装配置
猜你喜欢
【Harmony OS】【ARK UI】轻量级数据存储
Concepts and Methods of Exploratory Testing
[Harmony OS] [ARK UI] ETS context basic operations
探索性测试的概念及方法
Talking about GIS Data (6) - Projected Coordinate System
typescript46-函数之间的类型兼容性
三丁基-巯基膦烷「tBuBrettPhos Pd(allyl)」OTf),1798782-17-8
WinForm的控件二次开发
PotPlayer实现上班摸鱼电视自由
MySql数据库
随机推荐
unity2D横板游戏教程6-敌人AI以及受击动画
三丁基-巯基膦烷「tBuBrettPhos Pd(allyl)」OTf),1798782-17-8
Practical application of WebSocket
1089 狼人杀-简单版 (20 分)
[Harmony OS] [ArkUI] ets development graphics and animation drawing
阿里云对象存储oss私有桶生成链接
高可用 两地三中心
【HMS core】【Ads Kit】华为广告——海外应用在国内测试正式广告无法展示
User password encryption tool
Online password generator tool recommendation
Super handy drawing tool is recommended
Build your own web page on the Raspberry Pi (2)
typescript41-class类的私有修饰符
2022暑假牛客多校联赛第一场
DFS's complement to pruning
测试人员的价值体现在哪里
1. 两数之和
Shell conditional statement judgment
CAD有生僻字如何打出来、如何提交软件相关问题或建议?
typescript44-对象之间的类兼容器