一、服务提供者
1、控制器
package com.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.RestController;
import java.util.UUID;
/**
* @author dc
* @date 2020/7/26 - 15:30
*/
@RestController
@Slf4j
public class MyController {
@Value("${server.port}")
private String serverPort;
@GetMapping("/provider/consul")
public String doFirst() {
return "springcloud with consul:" + serverPort + "\t" + UUID.randomUUID().toString();
}
}
2、启动类
package com.springcloud;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
/**
* @author dc
* @date 2020/7/26 - 15:29
*/
@SpringBootApplication
@Slf4j
@EnableDiscoveryClient //开启服务发现客户端
public class ProviderConsul8086Main {
public static void main(String[] args) {
SpringApplication.run(ProviderConsul8086Main.class, args);
}
}
3、配置类
#指定服务端口号
server.port=8086
#指定服务名称
spring.application.name=payment-providerconsul8086
#指定consul的主机地址
spring.cloud.consul.host=localhost
#指定consul的端口号
spring.cloud.consul.port=8500
#指定consul服务发现服务名字
spring.cloud.consul.discovery.service-name=${spring.application.name}
二、服务消费者
1、控制器
package com.springcloud.controller;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import javax.annotation.Resource;
/**
* @author dc
* @date 2020/7/26 - 15:50
*/
@RestController
@Slf4j
public class MyController {
//指明调用服务的地址
public static final String INVOKE_URL = "http://payment-providerconsul8086";
//从spring容器中注入一个bean
@Resource
private RestTemplate restTemplate;
@GetMapping("/consumer/consul")
public String doFirst() {
return restTemplate.getForObject(INVOKE_URL + "/provider/consul", String.class);
}
}
2、启动类
package com.springcloud;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
/**
* @author dc
* @date 2020/7/26 - 15:49
*/
@SpringBootApplication
@Slf4j
@EnableDiscoveryClient //开启服务发现的客户端
public class ConsumerConsul81Main {
public static void main(String[] args) {
SpringApplication.run(ConsumerConsul81Main.class, args);
}
}
3、主配置类
#指定服务的端口号
server.port=81
#指定服务的名称
spring.application.name=payment-consumerconsul81
#指定consul注册中心的端口号
spring.cloud.consul.port=8500
#指定consul注册中心的主机
spring.cloud.consul.host=localhost
#指定consul服务发现的服务名称
spring.cloud.consul.discovery.service-name=${spring.application.name}
4、配置类
package com.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;
/**
* @author dc
* @date 2020/7/26 - 15:52
*/
@Configuration
public class ApplicationContextConfig {
@Bean //在spring容器中生成一个bean
@LoadBalanced //开启bean的负载均衡功能
public RestTemplate getRestTemplate() {
return new RestTemplate();
}
}
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.springcloud</groupId>
<version>1.0.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>payment-providerconsul8086</artifactId>
<dependencies>
<!--springboot框架web项目起步依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--springboot框架actuator起步依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!--springboot框架debtools依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<!--springboot框架consul服务发现依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
<version>2.2.3.RELEASE</version>
</dependency>
<!--lombok依赖-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
</project>