技术 | 版本 |
---|---|
Spring Cloud版本 | Hoxton.SR1 |
Spring Boot版本 | 2.2.2RELEASE |
Cloud Alibaba版本 | 2.1.0.RELEASE |
官方地址
git地址: https://github.com/spring-projects/spring-boot/releases/
Spring Boot官网: https://spring.io/projects/spring-boot
git地址: https://github.com/spring-projects/spring-cloud
Spring Boot官网: https://spring.io/projects/spring-cloud
SpringCloud的版本
Spring Cloud 采用了英国伦敦地铁站的名称来命名,并由地铁站名称字母A-Z依次类推的形式来发布迭代版本SpringCloud是一个由许多子项目组成的综合项目,各子项目有不同的发布节奏。为了管理SpringCloud与各子项目的版本依赖关系,发布了一个清单,其中包括了某个SpringCloud版本对应的子项目版本。为了避免SpringCloud版本号与子项目版本号混淆,SpringCloud版本采用了名称而非版本号的命名,这些版本的名字采用了伦敦地铁站的名字,根据字母表的顺序来对应版本时间顺序。例如Angel是第一个版本, Brixton是第二个版本。当SpringCloud的发布内容积累到临界点或者一个重大BUG被解决后,会发布一个"service releases"版本,简称SRX版本,比如Greenwich.SR2就是SpringCloud发布的Greenwich版本的第2个SRX版本。
网站: https://spring.io/projects/spring-cloud#overview
更详细版本的查看地址: https://start.spring.io/actuator/info
{
"git":{
"branch":"64b2829d7b37506fa4e7e5361462ac3ca91c1692",
"commit":{
"id":"64b2829",
"time":"2023-02-13T11:11:29Z"
}
},
"build":{
"version":"0.0.1-SNAPSHOT",
"artifact":"start-site",
"versions":{
"spring-boot":"3.0.2",
"initializr":"0.20.0-SNAPSHOT"
},
"name":"start.spring.io website",
"time":"2023-02-13T11:12:37.946Z",
"group":"io.spring.start"
},
"bom-ranges":{
"codecentric-spring-boot-admin":{
"2.4.3":"Spring Boot >=2.3.0.M1 and <2.5.0-M1",
"2.5.6":"Spring Boot >=2.5.0.M1 and <2.6.0-M1",
"2.6.8":"Spring Boot >=2.6.0.M1 and <2.7.0-M1",
"2.7.4":"Spring Boot >=2.7.0.M1 and <3.0.0-M1",
"3.0.0-M4":"Spring Boot >=3.0.0-M1 and <3.1.0-M1"
},
"solace-spring-boot":{
"1.1.0":"Spring Boot >=2.3.0.M1 and <2.6.0-M1",
"1.2.2":"Spring Boot >=2.6.0.M1 and <3.0.0-M1"
},
"solace-spring-cloud":{
"1.1.1":"Spring Boot >=2.3.0.M1 and <2.4.0-M1",
"2.1.0":"Spring Boot >=2.4.0.M1 and <2.6.0-M1",
"2.3.2":"Spring Boot >=2.6.0.M1 and <3.0.0-M1"
},
"spring-cloud":{
"Hoxton.SR12":"Spring Boot >=2.2.0.RELEASE and <2.4.0.M1",
"2020.0.6":"Spring Boot >=2.4.0.M1 and <2.6.0-M1",
"2021.0.0-M1":"Spring Boot >=2.6.0-M1 and <2.6.0-M3",
"2021.0.0-M3":"Spring Boot >=2.6.0-M3 and <2.6.0-RC1",
"2021.0.0-RC1":"Spring Boot >=2.6.0-RC1 and <2.6.1",
"2021.0.5":"Spring Boot >=2.6.1 and <3.0.0-M1",
"2022.0.0-M1":"Spring Boot >=3.0.0-M1 and <3.0.0-M2",
"2022.0.0-M2":"Spring Boot >=3.0.0-M2 and <3.0.0-M3",
"2022.0.0-M3":"Spring Boot >=3.0.0-M3 and <3.0.0-M4",
"2022.0.0-M4":"Spring Boot >=3.0.0-M4 and <3.0.0-M5",
"2022.0.0-M5":"Spring Boot >=3.0.0-M5 and <3.0.0-RC1",
"2022.0.0-RC1":"Spring Boot >=3.0.0-RC1 and <3.0.0-RC2",
"2022.0.0-RC2":"Spring Boot >=3.0.0-RC2 and <3.0.0",
"2022.0.1":"Spring Boot >=3.0.0 and <3.1.0-M1"
},
"spring-cloud-azure":{
"4.6.0":"Spring Boot >=2.5.0.M1 and <3.0.0-M1",
"5.0.0":"Spring Boot >=3.0.0-M1 and <3.1.0-M1"
},
"spring-cloud-gcp":{
"2.0.11":"Spring Boot >=2.4.0-M1 and <2.6.0-M1",
"3.4.3":"Spring Boot >=2.6.0-M1 and <3.0.0-M1",
"4.0.0":"Spring Boot >=3.0.0-M1 and <3.1.0-M1"
},
"spring-cloud-services":{
"2.3.0.RELEASE":"Spring Boot >=2.3.0.RELEASE and <2.4.0-M1",
"2.4.1":"Spring Boot >=2.4.0-M1 and <2.5.0-M1",
"3.3.0":"Spring Boot >=2.5.0-M1 and <2.6.0-M1",
"3.4.0":"Spring Boot >=2.6.0-M1 and <2.7.0-M1",
"3.5.0":"Spring Boot >=2.7.0-M1 and <3.0.0-M1",
"4.0.0":"Spring Boot >=3.0.0 and <3.1.0-M1"
},
"spring-shell":{
"2.1.6":"Spring Boot >=2.7.0 and <3.0.0-M1",
"3.0.0":"Spring Boot >=3.0.0 and <3.1.0-M1"
},
"vaadin":{
"14.9.6":"Spring Boot >=2.1.0.RELEASE and <2.6.0-M1",
"23.2.15":"Spring Boot >=2.6.0-M1 and <2.7.0-M1",
"23.3.5":"Spring Boot >=2.7.0-M1 and <2.8.0-M1"
},
"wavefront":{
"2.0.2":"Spring Boot >=2.1.0.RELEASE and <2.4.0-M1",
"2.1.1":"Spring Boot >=2.4.0-M1 and <2.5.0-M1",
"2.2.2":"Spring Boot >=2.5.0-M1 and <2.7.0-M1",
"2.3.4":"Spring Boot >=2.7.0-M1 and <3.0.0-M1",
"3.0.1":"Spring Boot >=3.0.0-M1 and <3.1.0-M1"
}
},
"dependency-ranges":{
"okta":{
"1.4.0":"Spring Boot >=2.2.0.RELEASE and <2.4.0-M1",
"1.5.1":"Spring Boot >=2.4.0-M1 and <2.4.1",
"2.0.1":"Spring Boot >=2.4.1 and <2.5.0-M1",
"2.1.6":"Spring Boot >=2.5.0-M1 and <3.0.0-M1",
"3.0.2":"Spring Boot >=3.0.0-M1 and <3.1.0-M1",
"managed":"Spring Boot >=3.1.0-M1"
},
"mybatis":{
"2.1.4":"Spring Boot >=2.1.0.RELEASE and <2.5.0-M1",
"2.2.2":"Spring Boot >=2.5.0-M1 and <2.7.0-M1",
"2.3.0":"Spring Boot >=2.7.0-M1 and <3.0.0-M1",
"3.0.0":"Spring Boot >=3.0.0-M1"
},
"camel":{
"3.5.0":"Spring Boot >=2.3.0.M1 and <2.4.0-M1",
"3.10.0":"Spring Boot >=2.4.0.M1 and <2.5.0-M1",
"3.13.0":"Spring Boot >=2.5.0.M1 and <2.6.0-M1",
"3.17.0":"Spring Boot >=2.6.0.M1 and <2.7.0-M1",
"3.20.2":"Spring Boot >=2.7.0.M1 and <3.0.0-M1",
"4.0.0-M1":"Spring Boot >=3.0.0-M1 and <3.1.0-M1"
},
"picocli":{
"4.7.0":"Spring Boot >=2.5.0.RELEASE and <3.1.0-M1"
},
"open-service-broker":{
"3.2.0":"Spring Boot >=2.3.0.M1 and <2.4.0-M1",
"3.3.1":"Spring Boot >=2.4.0-M1 and <2.5.0-M1",
"3.4.1":"Spring Boot >=2.5.0-M1 and <2.6.0-M1",
"3.5.0":"Spring Boot >=2.6.0-M1 and <2.7.0-M1"
}
}
}
官网:https://cloud.spring.io/spring-cloud-static/Hoxton.SR1/reference/htmlsingle/
官网:https://docs.spring.io/spring-boot/docs/2.2.2.RELEASE/reference/htmlsingle/
Spring Cloud中文文档: https://www.bookstack.cn/read/spring-cloud-docs/docs-index.md
升级后:
<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>springcloudartifactId>
<groupId>com.yydsgroupId>
<version>1.0-SNAPSHOTversion>
parent>
<modelVersion>4.0.0modelVersion>
<artifactId>cloud-provider-payment8001artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-actuatorartifactId>
dependency>
<dependency>
<groupId>org.mybatis.spring.bootgroupId>
<artifactId>mybatis-spring-boot-starterartifactId>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>druid-spring-boot-starterartifactId>
<version>1.1.22version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-jdbcartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-devtoolsartifactId>
<scope>runtimescope>
<optional>trueoptional>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
dependency>
dependencies>
project>
server:
port: 8001
spring:
application:
name: cloud-payment-service
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/springcloud?useUnicode=true&characterEncoding=utf-8&useSSL=false
username: root
password: root
mybatis:
mapper-locations: classpath:mapper/*.xml
type-aliases-package: com.yyds.springcloud.entities
package com.yyds.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class PaymentMain8001 {
public static void main(String[] args) {
SpringApplication.run(PaymentMain8001.class, args);
System.out.println("*******************PaymentMain8001启动成功********************");
}
}
package com.yyds.springcloud.dao;
import com.yyds.springcloud.entities.Payment;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
@Mapper
public interface PaymentDao {
int create(Payment payment);
Payment getPaymentById(@Param("id") Long id);
}
DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.yyds.springcloud.dao.PaymentDao">
<resultMap id="BaseResultMap" type="payment">
<result column="id" property="id"/>
<result column="serial" property="serial"/>
resultMap>
<insert id="create" parameterType="Payment" useGeneratedKeys="true" keyProperty="id">
insert into payment (serial) value (#{serial});
insert>
<select id="getPaymentById" parameterType="Long" resultMap="BaseResultMap">
select * from payment where id = #{id}
select>
mapper>
package com.yyds.springcloud.service;
import com.yyds.springcloud.entities.Payment;
import org.apache.ibatis.annotations.Param;
public interface PaymentService {
int create(Payment payment);
Payment getPaymentById(@Param("id") Long id);
}
package com.yyds.springcloud.service.impl;
import com.yyds.springcloud.dao.PaymentDao;
import com.yyds.springcloud.entities.Payment;
import com.yyds.springcloud.service.PaymentService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
@Service
public class PaymentServiceImpl implements PaymentService {
@Resource
private PaymentDao paymentDao;
@Override
public int create(Payment payment) {
return paymentDao.create(payment);
}
@Override
public Payment getPaymentById(Long id) {
return paymentDao.getPaymentById(id);
}
}
package com.yyds.springcloud.controller;
import com.yyds.springcloud.entities.CommonResult;
import com.yyds.springcloud.entities.Payment;
import com.yyds.springcloud.service.PaymentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
@RestController
@RequestMapping("/payment")
@Slf4j
public class PaymentController {
@Resource
private PaymentService paymentService;
@PostMapping("/create")
public CommonResult<Payment> create(@RequestBody Payment payment){
int result = paymentService.create(payment);
if(result > 0){
return new CommonResult(200,"插入数据库成功",result);
}else {
return new CommonResult(444,"插入数据库失败",null);
}
}
@GetMapping("/get/{id}")
public CommonResult<Payment> getPaymentById(@PathVariable("id") Long id){
Payment payment = paymentService.getPaymentById(id);
log.info("*****查询结果:{}",payment);
if (payment != null) {
return new CommonResult(200,"查询成功",payment);
}else {
return new CommonResult(200,"没有对应id",null);
}
}
}
package com.yyds.springcloud.entities;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class CommonResult<T>{
private Integer code;
private String message;
private T data;
public CommonResult(Integer code, String message) {
this(code,message,null);
}
}
package com.yyds.springcloud.entities;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Payment implements Serializable {
private Long id;
private String serial;
}
RestTemplate提供了多种便捷访问远程Http服务的方法, 是一种简单便捷的访问restful服务模板类,是Spring提供的用于访问Rest服务的客户端模板工具集。
https://docs.spring.io/spring-framework/docs/5.2.2.RELEASE/javadoc-api/org/springframework/web/client/RestTemplate.html
使用restTemplate访问restful接口非常的简单粗暴无脑。(url, requestMap, ResponseBean.class)这三个参数分别代表
REST请求地址、请求参数、HTTP响应转换被转换成的对象类型。
<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>springcloudartifactId>
<groupId>com.yydsgroupId>
<version>1.0-SNAPSHOTversion>
parent>
<modelVersion>4.0.0modelVersion>
<artifactId>cloud-consumer-order80artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-actuatorartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-devtoolsartifactId>
<scope>runtimescope>
<optional>trueoptional>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<optional>trueoptional>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
dependencies>
project>
package com.yyds.springcloud.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
@Configuration
public class ApplicationContextConfig {
@Bean
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
}
package com.yyds.springcloud.controller;
import com.yyds.springcloud.entities.CommonResult;
import com.yyds.springcloud.entities.Payment;
import lombok.extern.slf4j.Slf4j;
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;
@RestController
@RequestMapping("/consumer/payment")
@Slf4j
public class OrderController {
public static final String PAYMENT_URL = "http://localhost:8001";
@Resource
private RestTemplate restTemplate;
@GetMapping("/create")
public CommonResult<Payment> create(Payment payment) {
return restTemplate.postForObject(PAYMENT_URL +"/payment/create",payment,CommonResult.class);
}
@GetMapping("/get/{id}")
public CommonResult<Payment> getPayment(@PathVariable("id") Long id) {
return restTemplate.getForObject(PAYMENT_URL+"/payment/get/"+id,CommonResult.class);
}
}
测试
# 服务之间的调用
http://localhost/consumer/payment/get/10
{"code":200,"message":"查询成功","data":{"id":10,"serial":"11111"}}
http://localhost/consumer/payment/create?serial=6666
{"code":200,"message":"插入数据库成功","data":1}
工程重构
<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>springcloudartifactId>
<groupId>com.yydsgroupId>
<version>1.0-SNAPSHOTversion>
parent>
<modelVersion>4.0.0modelVersion>
<artifactId>cloud-api-commonsartifactId>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-devtoolsartifactId>
<scope>runtimescope>
<optional>trueoptional>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<optional>trueoptional>
dependency>
<dependency>
<groupId>cn.hutoolgroupId>
<artifactId>hutool-allartifactId>
<version>5.1.0version>
dependency>
dependencies>
project>
# 安装cloud-api-commons到本地
mvn clean
mvn install
在两个微服务模块删除实体类包,然后在两个模块的pom文件中,添加下面配置
<dependency>
<groupId>com.yydsgroupId>
<artifactId>cloud-api-commonsartifactId>
<version>1.0-SNAPSHOTversion>
dependency>
Spring Cloud 封装了 Netflix 公司开发的 Eureka 模块来实现服务治理
在传统的rpc远程调用框架中,管理每个服务与服务之间依赖关系比较复杂,管理比较复杂,所以需要使用服务治理,管理服务于服务之间依赖关系,可以实现服务调用、负载均衡、容错等,实现服务发现与注册。
Eureka采用了CS的设计架构,Eureka Server 作为服务注册功能的服务器,它是服务注册中心。而系统中的其他微服务,使用 Eureka的客户端连接到 Eureka Server并维持心跳连接。这样系统的维护人员就可以通过 Eureka Server 来监控系统中各个微服务是否正常运行。
在服务注册与发现中,有一个注册中心。当服务器启动的时候,会把当前自己服务器的信息 比如 服务地址通讯地址等以别名方式注册到注册中心上。另一方(消费者|服务提供者),以该别名的方式去注册中心上获取到实际的服务通讯地址,然后再实现本地RPC调用RPC远程调用框架核心设计思想:在于注册中心,因为使用注册中心管理每个服务与服务之间的一个依赖关系(服务治理概念)。在任何rpc远程框架中,都会有一个注册中心(存放服务地址相关信息(接口地址))
Eureka Server提供服务注册服务
各个微服务节点通过配置启动后,会在EurekaServer中进行注册,这样EurekaServer中的服务注册表中将会存储所有可用服务节点的信息,服务节点的信息可以在界面中直观看到。
EurekaClient通过注册中心进行访问
是一个Java客户端,用于简化Eureka Server的交互,客户端同时也具备一个内置的、使用轮询(round-robin)负载算法的负载均衡器。在应用启动后,将会向Eureka Server发送心跳(默认周期为30秒)。如果Eureka Server在多个心跳周期内没有接收到某个节点的心跳,EurekaServer将会从服务注册表中把这个服务节点移除(默认90秒)
模块名: cloud-eureka-server7001
<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>springcloudartifactId>
<groupId>com.yydsgroupId>
<version>1.0-SNAPSHOTversion>
parent>
<modelVersion>4.0.0modelVersion>
<artifactId>cloud-eureka-server7001artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-netflix-eureka-serverartifactId>
dependency>
<dependency>
<groupId>com.yydsgroupId>
<artifactId>cloud-api-commonsartifactId>
<version>1.0-SNAPSHOTversion>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-actuatorartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-devtoolsartifactId>
<scope>runtimescope>
<optional>trueoptional>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
dependency>
dependencies>
project>
server:
port: 7001
eureka:
instance:
hostname: localhost #eureka服务端的实例名称
client:
register-with-eureka: false #false表示不向注册中心注册自己。
fetch-registry: false #false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
service-url:
defaultZone: http://localhost:7001/eureka
package com.yyds.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
// 添加此注解
@EnableEurekaServer
public class EurekaMain7001 {
public static void main(String[] args) {
SpringApplication.run(EurekaMain7001.class, args);
System.out.println("************EurekaMain7001启动成功******************");
}
}
启动eureka测试
http://localhost:7001/
EurekaClient端cloud-provider-payment8001将注册进EurekaServer成为服务提供者provider。
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
dependency>
# 添加下面配置信息
eureka:
client:
service-url:
defaultZone: http://localhost:7001/eureka/
register-with-eureka: true
fetch-registry: true
package com.yyds.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
// 添加此注解
@EnableEurekaClient
public class PaymentMain8001 {
public static void main(String[] args) {
SpringApplication.run(PaymentMain8001.class, args);
System.out.println("*******************PaymentMain8001启动成功********************");
}
}
重新启动,刷新Eureka页面
EurekaClient端cloud-consumer-order80将注册进EurekaServer成为服务消费者consumer。
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
dependency>
server:
port: 80
spring:
application:
name: cloud-order-service
# 添加下面配置信息
eureka:
client:
service-url:
defaultZone: http://localhost:7001/eureka/
register-with-eureka: true
fetch-registry: true
package com.yyds.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableEurekaClient
public class OrderMain80 {
public static void main(String[] args) {
SpringApplication.run(OrderMain80.class, args);
System.out.println("***************OrderMain80启动成功********************");
}
}
重新启动,刷新Eureka页面
如果你的注册中心只有一个only one, 它出故障了会导致整个为服务环境不可用,所以需要搭建Eureka注册中心集群 ,实现负载均衡+故障容错。
参考cloud-eureka-server7001新建模块cloud-eureka-server7002
在C:\Windows\System32\drivers\etc
路径下的hosts文件中添加
127.0.0.1 eureka7001.com
127.0.0.1 eureka7002.com
server:
port: 7001
eureka:
instance:
hostname: eureka7001.com #eureka服务端的实例名称
client:
register-with-eureka: false #false表示不向注册中心注册自己。
fetch-registry: false #false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
service-url:
#集群指向其它eureka
defaultZone: http://eureka7002.com:7002/eureka/
server:
port: 7002
eureka:
instance:
hostname: eureka7002.com #eureka服务端的实例名称
client:
register-with-eureka: false #false表示不向注册中心注册自己。
fetch-registry: false #false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
service-url:
#集群指向其它eureka
defaultZone: http://eureka7001.com:7001/eureka/
server:
port: 8001
spring:
application:
name: cloud-payment-service
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/springcloud?useUnicode=true&characterEncoding=utf-8&useSSL=false
username: root
password: root
mybatis:
mapper-locations: classpath:mapper/*.xml
type-aliases-package: com.yyds.springcloud.entities
eureka:
client:
service-url:
#defaultZone: http://localhost:7001/eureka
defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka # 集群版本
register-with-eureka: true
fetch-registry: true
server:
port: 80
spring:
application:
name: cloud-order-service
# 添加下面配置信息
eureka:
client:
service-url:
#defaultZone: http://localhost:7001/eureka
defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka # 集群版本
register-with-eureka: true # 是否从EurekaServer抓取已有的注册信息,默认为true。单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡
fetch-registry: true
先要启动EurekaServer,7001/7002服务
再要启动服务提供者provider,8001
再要启动消费者,80
测试端口号: http://localhost/consumer/payment/get/1
参考cloud-provider-payment8001新建cloud-provider-payment8002模块
并且在两个模块的controller中均添加server.port属性,方便进行测试
package com.yyds.springcloud.controller;
import com.yyds.springcloud.entities.CommonResult;
import com.yyds.springcloud.entities.Payment;
import com.yyds.springcloud.service.PaymentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
@RestController
@RequestMapping("/payment")
@Slf4j
public class PaymentController {
@Resource
private PaymentService paymentService;
@Value("${server.port}")
private String serverPort;
@PostMapping("/create")
public CommonResult<Payment> create(@RequestBody Payment payment){
int result = paymentService.create(payment);
if(result > 0){
return new CommonResult(200,"插入数据库成功,serverPort+" + serverPort ,result);
}else {
return new CommonResult(444,"插入数据库失败",null);
}
}
@GetMapping("/get/{id}")
public CommonResult<Payment> getPaymentById(@PathVariable("id") Long id){
Payment payment = paymentService.getPaymentById(id);
log.info("*****查询结果:{}",payment);
if (payment != null) {
return new CommonResult(200,"查询成功,serverPort+" + serverPort,payment);
}else {
return new CommonResult(200,"没有对应id",null);
}
}
}
可以从Eureka中看到下面页面
访问http://localhost/consumer/payment/get/1
此时发现,都在访问8001服务
{"code":200,"message":"查询成功,serverPort+8001","data":{"id":1,"serial":"00001a"}}
修改订单微服务接口
package com.yyds.springcloud.controller;
import com.yyds.springcloud.entities.CommonResult;
import com.yyds.springcloud.entities.Payment;
import lombok.extern.slf4j.Slf4j;
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;
@RestController
@RequestMapping("/consumer/payment")
@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("/create")
public CommonResult<Payment> create(Payment payment) {
return restTemplate.postForObject(PAYMENT_URL +"/payment/create",payment,CommonResult.class);
}
@GetMapping("/get/{id}")
public CommonResult<Payment> getPayment(@PathVariable("id") Long id) {
return restTemplate.getForObject(PAYMENT_URL+"/payment/get/"+id,CommonResult.class);
}
}
此时访问http://localhost/consumer/payment/get/1,会报如下异常java.net.UnknownHostException: CLOUD-PAYMENT-SERVICE
。
需要修改配置类
package com.yyds.springcloud.config;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
@Configuration
public class ApplicationContextConfig {
@Bean
@LoadBalanced // 添加此注解
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
}
此时,会进行负载均衡
{"code":200,"message":"查询成功,serverPort+8002","data":{"id":1,"serial":"00001a"}}
{"code":200,"message":"查询成功,serverPort+8001","data":{"id":1,"serial":"00001a"}}
主机名称:服务名称修改
eureka:
client:
service-url:
#defaultZone: http://localhost:7001/eureka
defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka # 集群版本
register-with-eureka: true
fetch-registry: true
# 添加此配置
instance:
instance-id: payment8001
eureka:
client:
service-url:
#defaultZone: http://localhost:7001/eureka
defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka # 集群版本
register-with-eureka: true
fetch-registry: true
# 添加此配置
instance:
instance-id: payment8002
访问信息有IP信息提示
eureka:
client:
service-url:
#defaultZone: http://localhost:7001/eureka
defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka # 集群版本
register-with-eureka: true
fetch-registry: true
instance:
instance-id: payment8001
prefer-ip-address: true # 显示ip
eureka:
client:
service-url:
#defaultZone: http://localhost:7001/eureka
defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka # 集群版本
register-with-eureka: true
fetch-registry: true
instance:
instance-id: payment8002
prefer-ip-address: true # 显示ip
对于注册进eureka里面的微服务,可以通过服务发现来获得该服务的信息。
package com.yyds.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
// 添加此注解
@EnableEurekaClient
// 添加此注解
@EnableDiscoveryClient
public class PaymentMain8001 {
public static void main(String[] args) {
SpringApplication.run(PaymentMain8001.class, args);
System.out.println("*******************PaymentMain8001启动成功********************");
}
}
@Resource
private DiscoveryClient discoveryClient;
@GetMapping("/discovery")
public Object discovery(){
// 服务清单列表
List<String> services = discoveryClient.getServices();
for (String service : services) {
log.info("******************service:"+service);
}
List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-PAYMENT-SERVICE");
for (ServiceInstance instance : instances) {
String host = instance.getHost();
String serviceId = instance.getServiceId();
URI uri = instance.getUri();
int port = instance.getPort();
log.info(serviceId + "\t" + host + "\t"+ port+ "\t" + uri );
}
return this.discoveryClient;
}
测试http://localhost:8001/payment/discovery
{"services":["cloud-payment-service","cloud-order-service"],"order":0}
******************service:cloud-payment-service
******************service:cloud-order-service
CLOUD-PAYMENT-SERVICE 192.168.42.2 8002 http://192.168.42.2:8002
CLOUD-PAYMENT-SERVICE 192.168.42.2 8001 http://192.168.42.2:8001
默认情况下,如果EurekaServer在一定时间内没有接收到某个微服务实例的心跳,EurekaServer将会注销该实例(默认90秒)。但是当网络分区故障发生(延时、卡顿、拥挤)时,微服务与EurekaServer之间无法正常通信,以上行为可能变得非常危险了——因为微服务本身其实是健康的,此时本不应该注销这个微服务。Eureka通过“自我保护模式”来解决这个问题——当EurekaServer节点在短时间内丢失过多客户端时(可能发生了网络分区故障),那么这个节点就会进入自我保护模式。
在自我保护模式中,Eureka Server会保护服务注册表中的信息,不再注销任何服务实例。它的设计哲学就是宁可保留错误的服务注册信息,也不盲目注销任何可能健康的服务实例
。
eureka:
instance:
hostname: eureka7001.com #eureka服务端的实例名称
client:
register-with-eureka: false #false表示不向注册中心注册自己。
fetch-registry: false #false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
service-url:
#集群指向其它eureka
defaultZone: http://eureka7002.com:7002/eureka/
#单机就是7001自己
# defaultZone: http://localhost:7001/eureka
server:
#关闭自我保护机制,保证不可用服务被及时踢除
enable-self-preservation: false
eviction-interval-timer-in-ms: 2000
server:
port: 8001
###服务名称(服务注册到eureka名称)
spring:
application:
name: cloud-provider-payment
eureka:
client: #服务提供者provider注册进eureka服务列表内
service-url:
register-with-eureka: true
fetch-registry: true
# cluster version
#defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka
# singleton version
defaultZone: http://eureka7001.com:7001/eureka
#心跳检测与续约时间
#开发时设置小些,保证服务关闭后注册中心能即使剔除服务
instance:
#Eureka客户端向服务端发送心跳的时间间隔,单位为秒(默认是30秒)
lease-renewal-interval-in-seconds: 1
#Eureka服务端在收到最后一次心跳后等待时间上限,单位为秒(默认是90秒),超时将剔除服务
lease-expiration-duration-in-seconds: 2
关闭8001后,Eureka就会把其剔除。
2.4.4 Eureka停更
https://github.com/Netflix/eureka/wiki
zookeeper是一个分布式协调工具,可以实现注册中心功能
zookeeper服务器取代Eureka服务器,zk作为服务注册中心
<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>springcloudartifactId>
<groupId>com.yydsgroupId>
<version>1.0-SNAPSHOTversion>
parent>
<modelVersion>4.0.0modelVersion>
<artifactId>cloud-provider-payment8004artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-zookeeper-discoveryartifactId>
<exclusions>
<exclusion>
<groupId>org.apache.zookeepergroupId>
<artifactId>zookeeperartifactId>
exclusion>
exclusions>
dependency>
<dependency>
<groupId>org.apache.zookeepergroupId>
<artifactId>zookeeperartifactId>
<version>3.4.6version>
<exclusions>
<exclusion>
<groupId>org.slf4jgroupId>
<artifactId>slf4j-apiartifactId>
exclusion>
<exclusion>
<groupId>org.slf4jgroupId>
<artifactId>slf4j-log4j12artifactId>
exclusion>
<exclusion>
<groupId>log4jgroupId>
<artifactId>log4jartifactId>
exclusion>
exclusions>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-actuatorartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-devtoolsartifactId>
<scope>runtimescope>
<optional>trueoptional>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
dependency>
dependencies>
project>
server:
port: 8004
spring:
application:
name: cloud-payment-service
cloud:
zookeeper:
connect-string: 192.168.42.101:2181,192.168.42.102:2181,192.168.42.103:2181
package com.yyds.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
//该注解用于向使用consul或者zookeeper作为注册中心时注册服务
@EnableDiscoveryClient
public class PaymentMain8004 {
public static void main(String[] args) {
SpringApplication.run(PaymentMain8004.class, args);
System.out.println("*******************PaymentMain8004启动成功********************");
}
}
package com.yyds.springcloud.controller;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/payment")
@Slf4j
public class PaymentController {
@Value("${server.port}")
private String serverPort;
@GetMapping("/zk")
public String paymentZk(){
return "spring cloud with zk:" + serverPort + "\t" + System.currentTimeMillis();
}
}
http://localhost:8004/payment/zk
spring cloud with zk:8004 1676560104626
# 在zk中进行查看,发现已经注册上了
[zk: localhost:2181(CONNECTED) 3] ls /services
[cloud-payment-service]
[zk: localhost:2181(CONNECTED) 6] get /services/cloud-payment-service/25db342b-6426-47f5-9bc6-6c084a245723
{
"name":"cloud-payment-service",
"id":"25db342b-6426-47f5-9bc6-6c084a245723",
"address":"DESKTOP-6CM5A7S",
"port":8004,
"sslPort":null,
"payload":{
"@class":"org.springframework.cloud.zookeeper.discovery.ZookeeperInstance",
"id":"application-1",
"name":"cloud-payment-service",
"metadata":{
}
},
"registrationTimeUTC":1676560096304,
"serviceType":"DYNAMIC",
"uriSpec":{
"parts":[
{
"value":"scheme",
"variable":true
},
{
"value":"://",
"variable":false
},
{
"value":"address",
"variable":true
},
{
"value":":",
"variable":false
},
{
"value":"port",
"variable":true
}
]
}
}
注意:当杀掉注册进去的服务,zk会把此服务信息删除。
<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>springcloudartifactId>
<groupId>com.yydsgroupId>
<version>1.0-SNAPSHOTversion>
parent>
<modelVersion>4.0.0modelVersion>
<artifactId>cloud-consumerzk-order80artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-zookeeper-discoveryartifactId>
<exclusions>
<exclusion>
<groupId>org.apache.zookeepergroupId>
<artifactId>zookeeperartifactId>
exclusion>
exclusions>
dependency>
<dependency>
<groupId>org.apache.zookeepergroupId>
<artifactId>zookeeperartifactId>
<version>3.4.6version>
<exclusions>
<exclusion>
<groupId>org.slf4jgroupId>
<artifactId>slf4j-apiartifactId>
exclusion>
<exclusion>
<groupId>org.slf4jgroupId>
<artifactId>slf4j-log4j12artifactId>
exclusion>
<exclusion>
<groupId>log4jgroupId>
<artifactId>log4jartifactId>
exclusion>
exclusions>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-actuatorartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-devtoolsartifactId>
<scope>runtimescope>
<optional>trueoptional>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<optional>trueoptional>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
dependencies>
project>
server:
port: 80
spring:
application:
name: cloud-consumer-order
cloud:
zookeeper:
connect-string: 192.168.42.101:2181,192.168.42.102:2181,192.168.42.103:2181
package com.yyds.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ZkOrderMain80 {
public static void main(String[] args) {
SpringApplication.run(ZkOrderMain80.class, args);
System.out.println("***************ZkOrderMain80启动成功********************");
}
}
package com.yyds.springcloud.controller;
import lombok.extern.slf4j.Slf4j;
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;
@RestController
@RequestMapping("/consumer/zk")
@Slf4j
public class OrderController {
public static final String PAYMENT_URL = "http://cloud-payment-service";
@Resource
private RestTemplate restTemplate;
@GetMapping()
public String paymentInfo() {
return restTemplate.getForObject(PAYMENT_URL +"/payment/zk",String.class);
}
}
package com.yyds.springcloud.config;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
@Configuration
public class ApplicationContextConfig {
@Bean
@LoadBalanced
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
}
http://localhost/consumer/zk
spring cloud with zk:8004 1676562116384
[zk: localhost:2181(CONNECTED) 28] ls /services
[cloud-payment-service, cloud-consumer-order]
[zk: localhost:2181(CONNECTED) 30] get /services/cloud-consumer-order/8d13f36d-0697-4e98-a7f5-8d16169b4588
{
"name":"cloud-consumer-order",
"id":"8d13f36d-0697-4e98-a7f5-8d16169b4588",
"address":"DESKTOP-6CM5A7S",
"port":80,
"sslPort":null,
"payload":{
"@class":"org.springframework.cloud.zookeeper.discovery.ZookeeperInstance",
"id":"application-1",
"name":"cloud-consumer-order",
"metadata":{
}
},
"registrationTimeUTC":1676561841585,
"serviceType":"DYNAMIC",
"uriSpec":{
"parts":[
{
"value":"scheme",
"variable":true
},
{
"value":"://",
"variable":false
},
{
"value":"address",
"variable":true
},
{
"value":":",
"variable":false
},
{
"value":"port",
"variable":true
}
]
}
}
Consul 是一套开源的分布式服务发现和配置管理系统,由 HashiCorp 公司用 Go 语言开发。
提供了微服务系统中的服务治理、配置中心、控制总线等功能。这些功能中的每一个都可以根据需要单独使用,也可以一起使用以构建全方位的服务网格,总之Consul提供了一种完整的服务网格解决方案。
官网地址:https://www.consul.io/intro/index.html
下载地址: https://www.consul.io/downloads.html
中文文档:https://www.springcloud.cc/spring-cloud-consul.html
安装比较简单,直接解压,只有一个exe文件
consul --version
# 启动
consul agent -dev
通过以下地址可以访问Consul的首页:http://localhost:8500
<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>springcloudartifactId>
<groupId>com.yydsgroupId>
<version>1.0-SNAPSHOTversion>
parent>
<modelVersion>4.0.0modelVersion>
<artifactId>cloud-providerconsul-payment8006artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-consul-discoveryartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-actuatorartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-devtoolsartifactId>
<scope>runtimescope>
<optional>trueoptional>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
dependency>
dependencies>
project>
server:
port: 8006
spring:
application:
name: cloud-provider-payment
cloud:
consul:
host: localhost
port: 8500
discovery:
service-name: ${spring.application.name}
heartbeat:
enabled: true
package com.yyds.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
//该注解用于向使用consul或者zookeeper作为注册中心时注册服务
@EnableDiscoveryClient
public class ConsulPaymentMain8006 {
public static void main(String[] args) {
SpringApplication.run(ConsulPaymentMain8006.class, args);
System.out.println("*******************ConsulPaymentMain8006启动成功********************");
}
}
package com.yyds.springcloud.controller;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/payment")
@Slf4j
public class PaymentController {
@Value("${server.port}")
private String serverPort;
@GetMapping("/consul")
public String paymentZk(){
return "spring cloud with consul:" + serverPort + "\t" + System.currentTimeMillis();
}
}
http://localhost:8006/payment/consul
spring cloud with consul:8006 1676563822227
<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>springcloudartifactId>
<groupId>com.yydsgroupId>
<version>1.0-SNAPSHOTversion>
parent>
<modelVersion>4.0.0modelVersion>
<artifactId>cloud-consumerconsul-order80artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-consul-discoveryartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-actuatorartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-devtoolsartifactId>
<scope>runtimescope>
<optional>trueoptional>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<optional>trueoptional>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
dependencies>
project>
server:
port: 80
spring:
application:
name: cloud-consumer-order
cloud:
consul:
host: localhost
port: 8500
discovery:
service-name: ${spring.application.name}
heartbeat:
enabled: true
package com.yyds.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ConsulOrderMain80 {
public static void main(String[] args) {
SpringApplication.run(ConsulOrderMain80.class, args);
System.out.println("***************ConsulOrderMain80启动成功********************");
}
}
package com.yyds.springcloud.controller;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import javax.annotation.Resource;
@RestController
@RequestMapping("/consumer/consul")
@Slf4j
public class OrderConsulController {
public static final String PAYMENT_URL = "http://cloud-provider-payment";
@Resource
private RestTemplate restTemplate;
@GetMapping()
public String paymentInfo() {
return restTemplate.getForObject(PAYMENT_URL +"/payment/consul",String.class);
}
}
http://localhost/consumer/consul
spring cloud with consul:8006 1676564785406