服务提供者应用
两个模块 (prot:8021,8021)
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>springcloud19</artifactId> <groupId>com.hztech</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>provider-consul-payment8022</artifactId> <properties> <maven.compiler.source>19</maven.compiler.source> <maven.compiler.target>19</maven.compiler.target> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <!-- consul --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-consul-discovery</artifactId> </dependency> <!-- 公共的模块--> <dependency> <groupId>com.hztech</groupId> <artifactId>common-api</artifactId> <version>1.0-SNAPSHOT</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.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> </dependency> <!--mysql-connector-java--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <!--jdbc--> <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.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>
application.yml
spring:
cloud: consul: port: 8500 host: machine136 discovery: health-check-timeout: 10s #检测超时时间 health-check-critical-timeout: 30s #设置超时30秒之后,注销服务,这里数字要大于心跳检测的数字 prefer-ip-address: true instance-group: default #设置实例所在组 heartbeat: enabled: true ttl: 10 #这里开启心跳检测,设置10s提交一次心跳,用于consul与服务不在同一个网段的情况,支持存活检测,由于是启动10秒之后提交心跳,同时我这里consul和服务不在同一个网段,所以在项目启动成功之后,在consul上面需要等待十秒之后才能看到检测成功
main
@SpringBootApplication @EnableDiscoveryClient public class PaymentConsulMain8022 { public static void main(String[] args) { SpringApplication.run( PaymentConsulMain8022.class, args); } }
消费端模块
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>springcloud19</artifactId> <groupId>com.hztech</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>consumer-consul-order80</artifactId> <properties> <maven.compiler.source>19</maven.compiler.source> <maven.compiler.target>19</maven.compiler.target> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <!-- consul --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-consul-discovery</artifactId> </dependency> <!-- 引入公用模块--> <dependency> <groupId>com.hztech</groupId> <artifactId>common-api</artifactId> <version>1.0-SNAPSHOT</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.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</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> </project>
application.yml
server: port: 87 spring: application: name: order-consul-payment cloud: consul: port: 8500 host: machine136 discovery: health-check-timeout: 10s #检测超时时间 health-check-critical-timeout: 30s #设置超时30秒之后,注销服务,这里数字要大于心跳检测的数字 prefer-ip-address: true instance-group: default #设置实例所在组 heartbeat: enabled: true ttl: 10 #这里开启心跳检测,设置10s提交一次心跳,用于consul与服务不在同一个网段的情况,支持存活检测,由于是启动10秒之后提交心跳,同时我这里consul和服务不在同一个网段,所以在项目启动成功之后,在consul上面需要等待十秒之后才能看到检测成功
main
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class) @EnableDiscoveryClient public class OrderConsulMain80 { public static void main(String[] args) { SpringApplication.run( OrderConsulMain80.class, args); } }
order 91 通过 restTemplate 完成
@Configuration public class AppContextConfig { @Bean @LoadBalanced public RestTemplate getRestTmp() { return new RestTemplate(); } }
order 91 controller.cs
@RestController @RequestMapping("/consumer") public class OrderController { private static final Logger logger = LoggerFactory.getLogger(OrderController.class); //远程调用的地址,改成提供者在Eureka 上的名称,无需写端口号 //在RestTemplatebean上添加@LoadBalanced注解,可以有原先的ip:port转成微服务名调用。**** public static final String PAYMENT_URL="http://provider-consul-payment"; @Autowired private RestTemplate restTemplate; @RequestMapping("/payment/create") public CommonResult<Payment> create(Payment payment) throws IllegalAccessException { //logger.info(payment.toString()); MultiValueMap<String,Object> postpara=new LinkedMultiValueMap<>(); //调用公共类方法 完成对象到 MultiValueMap类型的转换 postpara= ObjConvert.objectToMultiValueMap(payment); // logger.info("-----after objtomap :"+postpara.toString()); CommonResult result = restTemplate.postForObject(PAYMENT_URL + "/payment/create", postpara, CommonResult.class); return result ; } @RequestMapping("/payment/get/{id}") public CommonResult<Payment> getPaymentById(@PathVariable("id") Long id) { return restTemplate.getForObject(PAYMENT_URL+"/payment/get/"+id ,CommonResult.class); } }
测试
启动 provider 8021,8022 ,order 91
访问 consumer
docker consul 安装 在 Linux
consul Linux Docker 安装 - hztech - 博客园 (cnblogs.com)
标签:restTemplate,SpringCloud,Consul,boot,springframework,spring,org,consul,starter From: https://www.cnblogs.com/hztech/p/17117289.html