版本的选择
查看SpringCloud和SpringBoot之间版本的依赖关系:
首先连接下各个组件:
技术要求:
java8+maven+git、github+Nginx+RabbitMQ+SpringBoot2.0
接下来我们会搭建一个订单-支付模块微服务。
1、New Project
maven项目: maven-archetype-site
maven选自己本地的版本
2、字符编码
设置:Editor->File Encodings 里全部设置utf-8,native-to-ascii 打钩
3、注解生效激活
4、java编译版本选8
5、File Type过滤
6、安装插件
1、在pom.xml中 添加 pom
2、删除src文件夹
3、编写pom文件
<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">
<modelVersion>4.0.0modelVersion>
<groupId>com.atguigu.springcloudgroupId>
<artifactId>cloud2020artifactId>
<version>1.0-SNAPSHOTversion>
<packaging>pompackaging>
<properties>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<maven.compiler.source>1.8maven.compiler.source>
<maven.compiler.target>1.8maven.compiler.target>
<junit.version>4.12junit.version>
<log4j.version>1.2.17log4j.version>
<lombok.version>1.16.18lombok.version>
<mysql.version>8.0.18mysql.version>
<druid.verison>1.1.16druid.verison>
<mybatis.spring.boot.verison>1.3.0mybatis.spring.boot.verison>
properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-dependenciesartifactId>
<version>2.2.2.RELEASEversion>
<type>pomtype>
<scope>importscope>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-dependenciesartifactId>
<version>Hoxton.SR1version>
<type>pomtype>
<scope>importscope>
dependency>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-alibaba-dependenciesartifactId>
<version>2.2.0.RELEASEversion>
<type>pomtype>
<scope>importscope>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>${mysql.version}version>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>druid-spring-boot-starterartifactId>
<version>${druid.verison}version>
dependency>
<dependency>
<groupId>org.mybatis.spring.bootgroupId>
<artifactId>mybatis-spring-boot-starterartifactId>
<version>${mybatis.spring.boot.verison}version>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<version>${lombok.version}version>
dependency>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>${junit.version}version>
dependency>
<dependency>
<groupId>log4jgroupId>
<artifactId>log4jartifactId>
<version>${log4j.version}version>
dependency>
dependencies>
dependencyManagement>
project>
复习:Maven中的DependencyManagement和Dependencies
<dependencyManagement>
<dependencies>
Maven使用dependencyManagement元素来提供了一种管理依赖版本号方式。
通常会在一个组织或者项目的最顶层中看到dependencyManagement元素。
使用pom.xml中的dependencyManagement元素的项目,然后它就会使用这个dependencyManagement元素中指定的版本号。
maven中跳过单元测试
发布到maven仓库
父工程创建完成执行mvn:install将父工程发布到仓库方便子工程继承
<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>
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>
<optional>trueoptional>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
dependencies>
server:
port: 8001
spring:
application:
name: cloud-payment-service
datasource:
# 当前数据源操作类型
type: com.alibaba.druid.pool.DruidDataSource
# mysql驱动类
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/cloud?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=GMT%2B8
username: root
password: root
mybatis:
mapper-locations: classpath*:mapper/*.xml
type-aliases-package: com.atguigu.springcloud.entities
@SpringBootApplication
public class PaymentMain8001 {
public static void main(String[] args) {
SpringApplication.run(PaymentMain8001.class, args);
}
}
1、建表sql
CREATE TABLE `payment`(
`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'ID',
`serial` varchar(200) DEFAULT '',
PRIMARY KEY(`id`)
)ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8
2、entities
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Payment implements Serializable {
private Long id;
private String serial;
}
@Data
@AllArgsConstructor
@NoArgsConstructor
public class CommonResult<T> {
// 404 not_found
private Integer code;
private String message;
private T data;
public CommonResult(Integer code, String message){
this(code, message, null);
}
}
3、dao
@Mapper
public interface PaymentDao {
public int create(Payment payment);
public Payment getPaymentById(@Param("id") Long id);
}
建文件夹 mapper
建mapper文件
<mapper namespace="com.atguigu.springcloud.dao.PaymentDao">
<resultMap id="BaseResultMap" type="com.atguigu.springcloud.entities.Payment">
<id column="id" property="id" jdbcType="BIGINT" />
<id column="serial" property="serial" jdbcType="VARCHAR"/>
resultMap>
<insert id="create" parameterType="Payment" useGeneratedKeys="true" keyProperty="id">
insert into payment(serial) values(#{serial});
insert>
<select id="getPaymentById" parameterType="Long" resultMap="BaseResultMap">
select * from payment where id=#{id};
select>
mapper>
4、service
public interface PaymentService {
public int create(Payment payment);
public Payment getPaymentById(@Param("id") Long id);
}
@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);
}
}
5、controller
@RestController
@Slf4j
public class PaymentController {
@Resource
private PaymentService paymentService;
@PostMapping(value = "/payment/create")
public CommonResult create(@RequestBody Payment payment){
int result = paymentService.create(payment);
log.info("PaymentController create 插入结果 " + result);
if(result > 0){
return new CommonResult(200, "插入数据库成功", result);
}else{
return new CommonResult(444, "插入数据库失败");
}
}
@GetMapping(value = "/payment/get/{id}")
public CommonResult getPaymentById(@PathVariable("id") Long id){
Payment payment = paymentService.getPaymentById(id);
log.info("PaymentController getPaymentById 查询结果 " + payment);
if(payment != null){
return new CommonResult(200, "查询成功", payment);
}else{
return new CommonResult(444, "没有对应记录,查询ID:" + id);
}
}
}
http://localhost:8001/payment/get/1
http://localhost:8001/payment/create?serial=aaa
<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>
server:
port: 80
@SpringBootApplication
public class OrderMain80 {
public static void main(String[] args) {
SpringApplication.run(OrderMain80.class, args);
}
}
entities
复制entities包下的实体类
CommonResult
Payment
ApplicationContextConfig 配置
@Configuration
public class ApplicationContextConfig {
@Bean
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
}
OrderController
@RestController
@Slf4j
@RequestMapping("/consumer")
public class OrderController {
private static final String PAYMENT_URL = "http://localhost:8001";
@Resource
private RestTemplate restTemplate;
@GetMapping("/payment/create")
public CommonResult<Payment> create(Payment payment) {
log.info("method create " + payment);
return restTemplate.postForObject(PAYMENT_URL + "/payment/create", payment, CommonResult.class);
}
@GetMapping("/payment/get/{id}")
public CommonResult<Payment> getPayment(@PathVariable("id") Long id) {
return restTemplate.getForObject(PAYMENT_URL + "/payment/get/" + id, CommonResult.class);
}
}
http://localhost/consumer/payment/create?serial=abbb
http://localhost/consumer/payment/get/1
<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.3.10version>
dependency>
dependencies>
使用maven,将commone模块打包(install)
在其他项目中引入这个jar包
<dependency>
<groupId>com.atguigu.springcloudgroupId>
<artifactId>cloud-api-commonsartifactId>
<version>${project.version}version>
dependency>
服务治理
SpringCloud封装了Netflix公司开发的Eureka模块来实现服务治理
在传统的rpc远程调用框架中,管理每个服务与服务之间依赖关系比较复杂,所以需要使用服务治理,管理服务于服务之间依赖关系,可以实现服务调用、负载均衡、容错等,实现服务发现与注册。
服务注册与发现
Eureka采用了CS的设计架构,Eureka Server作为服务注册功能的服务器,它是服务注册中心,而系统中的其他微服务,使用Eureka的客户端连接到Eureka Server并维持心跳连接。这样系统的维护人员就可以通过Eureka Server来监控系统中各个微服务是否正常运行。
在服务注册与发现中有一个注册中心,档服务器启动的时候,会把当前自己服务器的信息 比如服务器地址、通信地址、等以别名方式注册到注册中心上。另一方(消费者 | 服务提供者),以该别名的方式去注册中心上获取到实际的服务器通讯地址,然后在实现本地RPC调用、RPC远程调用框架核心设计思想:在于注册中心。因为使用注册中心管理每个服务于服务之间的一个依赖关系(服务治理概念)。在任何RPC远程框架中,都会有一个注册中心存放服务地址相关信息。
Eureka包含了两个组件:
cloud-eureka-server7001
<dependencies>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-netflix-eureka-serverartifactId>
dependency>
<dependency>
<groupId>com.atguigu.springcloudgroupId>
<artifactId>cloud-api-commonsartifactId>
<version>${project.version}version>
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>
server:
port: 7001
eureka:
instance:
hostname: localhost #eureka服务端的实例名称
client:
#false表示不向注册中心注册自己
register-with-eureka: false
#false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
fetch-registry: false
service-url:
#设置与Eureka Server交互的地址查询服务和注册服务都需要依赖这个地址
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
@SpringBootApplication
@EnableEurekaServer
public class EurekaMain7001 {
public static void main(String[] args){
SpringApplication.run(EurekaMain7001.class,args);
}
}
访问 http://localhost:7001 就可以看到如下页面,可以看到当前没有服务注册进来
1、配置pom
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
dependency>
2、写application.yml
spring:
application:
name: cloud-payment-service
eureka:
client:
# 不注册自己
register-with-eureka: true
# 表示自己是注册中心,不用检索服务
fetch-registry: true
service-url:
defaultZone: http://localhost:7001/eureka
3、开启eureka服务
@SpringBootApplication
@EnableEurekaClient
public class PaymentMain8001 {
public static void main(String[] args) {
SpringApplication.run(PaymentMain8001.class, args);
}
}
1、配置pom
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
dependency>
2、写application.yml
spring:
application:
name: cloud-order-service
eureka:
client:
# 不注册自己
register-with-eureka: true
# 表示自己是注册中心,不用检索服务
fetch-registry: true
service-url:
defaultZone: http://localhost:7001/eureka
3、开启eureka服务
@SpringBootApplication
@EnableEurekaClient
public class OrderMain80 {
public static void main(String[] args) {
SpringApplication.run(OrderMain80.class, args);
}
}
访问 http://localhost:7001 可以看到程序被注册进euraka
访问 http://localhost/consumer/payment/get/1
服务注册:将服务信息注册到注册中心
服务发现:从注册中心上获取到服务信息
其实质就是:key-value形式的。Key:服务的名字 value:服务调用地址
执行步骤:
1:先启动eureka注册中心
2:启动服务提供者(我们这里的服务提供者就是payment支付服务)
3:服务提供者在启动后会把自身的信息(如服务地址,以别名方式注册到)注册到eureka中
4:消费者(我们这里是order服务)在需要调用接口的时候,使用服务别名去注册中心获取到实际的RPC远程调用地址
5:消费者获取到调用地址后,底层实际是利用HttpClient技术实现远程调用的
6:消费者获得服务地址后会缓存在本地的JVM内存中,默认每隔30秒更新移除服务调用地址。
问题:微服务RPC远程调用最核心的是什么?
高可用。试想下,如果你的注册中心有且仅有一个 only One.那么,如果注册中心故障了,那就呵呵了,会导致整个服务的不可用。后果可是很严重的。所以,解决办法就是:搭建Eureka注册中心集群。实现负载均衡+故障容错(其实从上图中也可以看出,eureka server是多个,provider也是多个)
块 cloud-eureka-server7002
新建 cloud-eureka-server7002
复制 cloud-eureka-server7001的pom.xml
复制 cloud-eureka-server7001的application.yml
复制 Main.java 启动类
C:\Windows\System32\drivers\etc
127.0.0.1 eureka7001.com
127.0.0.1 eureka7002.com
server:
port: 7001
eureka:
instance:
hostname: eureka7001.com #eureka服务端的实例名称
client:
#false表示不向注册中心注册自己
register-with-eureka: false
#false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
fetch-registry: false
service-url:
#设置与Eureka Server交互的地址查询服务和注册服务都需要依赖这个地址
defaultZone: http://eureka7002.com:7002/eureka/
server:
port: 7002
eureka:
instance:
hostname: eureka7002.com #eureka服务端的实例名称
client:
#false表示不向注册中心注册自己
register-with-eureka: false
#false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
fetch-registry: false
service-url:
#设置与Eureka Server交互的地址查询服务和注册服务都需要依赖这个地址
defaultZone: http://eureka7001.com:7001/eureka/
同时看到Eureka图标,且7001指着7002,7002指着7001,说明Eureka集群搭建成功。
修改application.yml
eureka:
client:
# 不注册自己
register-with-eureka: true
# 表示自己是注册中心,不用检索服务
fetch-registry: true
service-url:
defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka
@Value("${server.port}")
private String serverPort;
@PostMapping(value = "/payment/create")
public CommonResult create(@RequestBody Payment payment){
log.info("method create " + payment);
int result = paymentService.create(payment);
log.info("PaymentController create 插入结果 " + result);
if(result > 0){
return new CommonResult(200, "插入数据库成功,serverPort:" + serverPort, result);
}else{
return new CommonResult(444, "插入数据库失败");
}
}
将payment8001模块复制 到 payment8002
开启@LoadBalanced注解,使RestTemplate开启负载均衡(可以指定负载均衡算法,默认轮询)
@Configuration
public class ApplicationContextConfig {
@Bean
@LoadBalanced
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
}
OrderController中接口访问,用服务器名的形式
public class OrderController {
//private static final String PAYMENT_URL="http://localhost:8001";
//通过在eureka上注册过的微服务名称调用
private static final String PAYMENT_URL = "http://cloud-payment-service";
@Resource
private RestTemplate restTemplate;
@GetMapping("/payment/create")
public CommonResult<Payment> create(Payment payment) {
log.info("method create " + payment);
return restTemplate.postForObject(PAYMENT_URL + "/payment/create", payment, CommonResult.class);
}
访问 http://localhost/consumer/payment/get/1 多次执行后会轮询调用
{“code”:200,“message”:“查询成功,serverPort:8001”,“data”:{“id”:1,“serial”:“aaa”}}
{“code”:200,“message”:“查询成功,serverPort:8002”,“data”:{“id”:1,“serial”:“aaa”}}
为什么有自我保护机制?
为了防止EurekaClient可以正常运行,但是与EurekaServer网络不同的情况下,将EurekaClient服务剔除。
自我保护模式
正常情况下,如果EurekaServer在一定时间内没有接收到某个微服务实例的心跳,就会注销该实例。
而自我保护模式就是某时刻某个微服务不可用了,Eureka不会立即进行清理,依旧会对该微服务的信息进行保存,属于CAP里面的AP分支(下文补充)。当EurekaServer节点在短时间内丢失过多客户端时(网络故障),节点就会进入自我保护模式。
自我保护模式配置
默认自我保护模式是开启的,可以通过配置进行禁用:
Eureka Server 端操作添加的配置:
#7001、7002 服务端配置
eureka:
server:
#关闭自我保护机制,保证不可用服务被及时剔除
enable-self-preservation: false
#清理无效节点的时间间隔,默认60000毫秒,即60秒 (此处时间间隔设置为2s)
eviction-interval-timer-in-ms: 2000
Eureka Client 端操作添加的配置:
#8001、8002服务中配置
eureka:
instance:
# Eureka客户端向服务端发送心跳的时间间隔,单位为妙(默认是30s)
lease-renewal-interval-in-seconds: 1
# Eureka服务端在收到最后一次心跳后的等待时间上限,单位为秒(默认90s),超时将移除服务
lease-expiration-duration-in-seconds: 2
Zookeeper是一个分布式协调工具,可以实现注册中心功能,所以可以取代Eureka服务器,作为服务注册与发现中心。
为了方便我们接下来的测试,通过 Docker 方式在远程服务器上以单机形式安装 Zookeeper,并启动。
cloud-provider-payment8004
<dependencies>
<dependency>
<groupId>com.atguigu.springcloudgroupId>
<artifactId>cloud-api-commonsartifactId>
<version>${project.version}version>
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.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.14version>
<exclusions>
<exclusion>
<groupId>log4jgroupId>
<artifactId>log4jartifactId>
exclusion>
<exclusion>
<groupId>org.slf4jgroupId>
<artifactId>slf4j-log4j12artifactId>
exclusion>
exclusions>
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>
server:
port: 8004
spring:
application:
name: cloud-provider-payment
cloud:
zookeeper:
connect-string: localhost:2181
@SpringBootApplication
@EnableDiscoveryClient
public class PaymentMain8004 {
public static void main(String[] args){
SpringApplication.run(PaymentMain8004.class,args);
}
}
@RestController
@Slf4j
public class PaymentController {
@Value("${server.port}")
private String serverPort;
@GetMapping("/payment/zk")
public String paymentzk(){
return "springcloud with zookeeper"+serverPort+"\t"+ UUID.randomUUID().toString();
}
}
http://localhost:8004/payment/zk
修改pom依赖,排除3.5.3版本的,并引入3.4.14版本的
<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.14version>
dependency>
排除zookeeper自带的log架包
<dependency>
<groupId>org.apache.zookeepergroupId>
<artifactId>zookeeperartifactId>
<version>3.4.14version>
<exclusions>
<exclusion>
<groupId>log4jgroupId>
<artifactId>log4jartifactId>
exclusion>
<exclusion>
<groupId>org.slf4jgroupId>
<artifactId>slf4j-log4j12artifactId>
exclusion>
exclusions>
dependency>
cloud-consumerzk-order80
<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.14version>
<exclusions>
<exclusion>
<groupId>log4jgroupId>
<artifactId>log4jartifactId>
exclusion>
<exclusion>
<groupId>org.slf4jgroupId>
<artifactId>slf4j-log4j12artifactId>
exclusion>
exclusions>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
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>
server:
port: 80
spring:
application:
name: cloud-consumer-order
cloud:
#注册到zookeeper地址
zookeeper:
connect-string: localhost:2181
@SpringBootApplication
@EnableDiscoveryClient
public class OrderZKMain80 {
public static void main(String[] args) {
SpringApplication.run(OrderZKMain80.class,args);
}
}
@RestController
@Slf4j
public class OrderZKController {
private static final String INVOKE_URL = "http://cloud-provider-payment";
@Resource
private RestTemplate restTemplate;
@GetMapping(value = "/consumer/payment/zk")
public String paymentInfo() {
String result = restTemplate.getForObject(INVOKE_URL+"/payment/zk",String.class);
return result;
}
}
http://localhost/consumer/payment/zk