以图实现
服务端相互注册:https://blog.csdn.net/qq53167889/article/details/106240092
client8001 client8002 client8003创建
1.pom 通用
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>springcloud</artifactId>
<groupId>com.psf.springcould</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>springcloud-test8001</artifactId>
<dependencies>
<dependency>
<groupId>com.psf.springcloud</groupId>
<artifactId>springcloud-api-commons</artifactId>
<version>${project.version}</version>
</dependency>
<!--eureka client-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-jdbc -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-devtools -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-test -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
2.application.yml server:port: 8001 改成相应的端口号 8001 8002 8003 其他不需要改变
server:
port: 8001
spring:
application:
name: springcloud-payment-service #服务名称
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: org.gjt.mm.mysql.Driver
url: jdbc:mysql://localhost:3306/springcloud2020?useUnicode=true&characterEncoding=utf-8&useSSL=false
username: root
password: root
#以下为新增
druid:
# 指明是否在从池中取出连接前进行检验,如果检验失败, 则从池中去除连接并尝试取出另一个,
#注意: 设置为true后如果要生效,validationQuery参数必须设置为非空字符串
test-on-borrow: false
# 指明连接是否被空闲连接回收器(如果有)进行检验.如果检测失败,则连接将被从池中去除.
#注意: 设置为true后如果要生效,validationQuery参数必须设置为非空字符串
test-while-idle: true
# 指明是否在归还到池中前进行检验,注意: 设置为true后如果要生效,
#validationQuery参数必须设置为非空字符串
test-on-return: false
# SQL查询,用来验证从连接池取出的连接,在将连接返回给调用者之前.
#如果指定,则查询必须是一个SQL SELECT并且必须返回至少一行记录
validation-query: select 1
#eureka client配置
eureka:
client:
register-with-eureka: true
fetchRegistry: true
service-url:
defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/
instance:
instance-id: payment8001 #主机名 更改8001 8002 8003
prefer-ip-address: true #显示ip
mybatis:
mapperLocations: classpath:mapper/*.xml
type-aliases-package: com.psf.springcloud.entity
3.controller 写了简单的测试
@RestController
@Slf4j
public class PaymentController {
@Resource
private PaymentImplService paymentImplService;
@Value("${server.port}")
private String serverPort;
@PostMapping(value = "/payment/insert")
public Reslt insert(@RequestBody Payment payment){
int insert = paymentImplService.insert(payment);
if (insert>0){
return new Reslt(200,"添加成功,端口="+serverPort,insert);
}
else {
return new Reslt(404,"添加失败",insert);
}
}
@GetMapping(value = "/payment/getBySerial/{serial}")
public Reslt getBySerial(@PathVariable("serial") String serial){
Payment payment = paymentImplService.getByPaySerial(serial);
if (payment!= null){
return new Reslt(200,"查询成功,端口="+serverPort,payment);
}
else {
return new Reslt(404,"查询失败",null);
}
}
}
80消费者创建
1.pom
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>springcloud</artifactId>
<groupId>com.psf.springcould</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>springcloud-order80</artifactId>
<dependencies>
<!--eureka client-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
<dependency>
<groupId>com.psf.springcloud</groupId>
<artifactId>springcloud-api-commons</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-devtools -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-test -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
2.yml
server:
port: 80
spring:
application:
name: springcloud-order80 #服务名称
#eureka client配置
eureka:
client:
register-with-eureka: true
fetchRegistry: true
service-url:
defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/
3.controller
@RestController
@Slf4j
public class OrderController {
//public static final String url="http://localhost:8001";
public static final String url="http://springcloud-payment-service";
@Resource
private RestTemplate restTemplate;
@GetMapping(value = "/order/payment/insert")
public Reslt<Payment> insert(Payment payment){
return restTemplate.postForObject(url+"/payment/insert",payment,Reslt.class);
}
@GetMapping(value = "/order/payment/getBySerial/{serial}")
public Reslt<Payment> getBySerial(@PathVariable("serial") String serial){
return restTemplate.getForObject(url+"/payment/getBySerial/"+serial,Reslt.class);
}
}
4.config
@Configuration
public class ApplicationContextConfig {
@Bean
@LoadBalanced //赋予RestTemplate负载均衡的能力 必须加
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
}
最后启动顺序
server7001 7002 →client 8001 8002 →消费者order 80
配置成功,现在用以消费者进行测试
一直刷新 端口每次都在变化,这就实现了默认的轮询负载机制
附一 服务发现Discovery
对于注册进eureka里面的微服务,可以通过服务发现来获得该服务的信息
修改8001 端口的controller
@Resource
private DiscoveryClient discoveryClient;
@GetMapping(value = "/payment/discovery")
public Object discovery(){
List<String> services = discoveryClient.getServices();
for (String element : services) {
log.info("***** element:"+element);
}
List<ServiceInstance> instances = discoveryClient.getInstances("SPRINGCLOUD-PAYMENT-SERVICE");
for (ServiceInstance instance : instances) {
log.info(instance.getServiceId()+"\t"+instance.getHost()+"\t"+instance.getPort()+"\t"+instance.getUri());
}
return this.discoveryClient;
}
启动类加 @EnableDiscoveryClient 运行
日志显示
附二 Eureka自我保护机制
7001端口 yml
eureka:
instance:
hostname: eureka7001.com #eureka服务端的实例名字
client:
register-with-eureka: false #表识不向注册中心注册自己
fetch-registry: false #表示自己就是注册中心,职责是维护服务实例,并不需要去检索服务
service-url:
defaultZone: http://eureka7002.com:7002/eureka/ #设置与eureka server交互的地址查询服务和注册服务都需要依赖这个地址
#########################下面是添加#############################
server:
enable-self-preservation: false #禁用自我保护机制
eviction-interval-timer-in-ms: 2000
8001 端口 yml
eureka:
client:
register-with-eureka: true
fetchRegistry: true
service-url:
defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/
instance:
instance-id: payment8001 #主机名
prefer-ip-address: true #显示ip
#########################下面是添加#############################
#客户端向服务端发送心跳时间间隔,默认30秒
lease-renewal-interval-in-seconds: 1
#服务端收到最后一次时间上限,默认90秒,超过剔除服务
lease-expiration-duration-in-seconds: 2
测试结果
如果关闭8001,会被立即删除