1.下载安装
下载地址:https://www.consul.io/downloads.html
打开启动consul
启动成功后,进入localhost:8500
2.将支付服务注册到consul
建立module
导入pom.xml
<dependencies><!-- 引入自己定义的api通用包,可以使用Payment支付Entity --><dependency><groupId>com.liuscoding</groupId><artifactId>cloud-api-commons</artifactId><version>${project.version}</version></dependency><!--SpringCloud consul-server --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-consul-discovery</artifactId></dependency><!-- SpringBoot整合Web组件 --><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><!--日常通用jar包配置--><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><dependency><groupId>cn.hutool</groupId><artifactId>hutool-all</artifactId><version>RELEASE</version><scope>test</scope></dependency><dependency><groupId>cn.hutool</groupId><artifactId>hutool-all</artifactId><version>RELEASE</version><scope>test</scope></dependency></dependencies>
修改yml文件
###consul服务端口号
server:port: 8006spring:application:name: consul-provider-payment####consul注册中心地址cloud:consul:host: localhostport: 8500discovery:#hostname: 127.0.0.1service-name: ${spring.application.name}
添加启动类和业务代码
@SpringBootApplication
@EnableDiscoveryClient//该注解用于向使用consul或者zookeeper作为注册中心时注册服务
public class PaymentMain8006 {public static void main(String[] args) {SpringApplication.run(PaymentMain8006.class,args);}
}@RestController
@Slf4j
@RequestMapping("/payment")
public class PaymentController {@Value("${server.port}")private String serverPort;@RequestMapping(value = "/consul")public String paymentzk(){return "springcloud with consul: "+serverPort+"\t"+ UUID.randomUUID().toString();}
}
启动项目查看测试结果,服务被成功注册
3.将消费服务注册到consul
建立module
导入pom.xml
<?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>cloud2020</artifactId><groupId>com.kebo.cloud</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>cloud-consumerconsul-order80</artifactId><dependencies><!--SpringCloud consul-server --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-consul-discovery</artifactId></dependency><!-- SpringBoot整合Web组件 --><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><!--日常通用jar包配置--><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></project>
修改yml文件
###consul服务端口号
server:port: 80spring:application:name: cloud-consumer-order####consul注册中心地址cloud:consul:host: localhostport: 8500discovery:#hostname: 127.0.0.1service-name: ${spring.application.name}
添加启动类和业务代码
@SpringBootApplication
@EnableDiscoveryClient
public class OrderMain80 {public static void main(String[] args) {SpringApplication.run(OrderMain80.class, args);}
}@Configuration
public class ApplicationContextConfig {@Bean@LoadBalanced//使用@LoadBalanced注解赋予RestTemplate负载均衡的能力public RestTemplate getRestTemplate(){return new RestTemplate();}
}@RestController
@Slf4j
public class OrderController {public static final String PAYMENT_URL = "http://consul-provider-payment";@Autowiredprivate RestTemplate restTemplate;@GetMapping(value = "/consumer/payment/consul")public String paymentInfo() {String result = restTemplate.getForObject(PAYMENT_URL + "/payment/consul", String.class);return result;}
}
启动项目查看测试结果