系统配置信息
- springboot版本:2.1.6.RELEASE
- jdk:1.8
- 系统:Windows10
工程结构
- 父工程 halo-cloud-parent
- 子工程<注册中心> halo-cloud-server
- 子工程<服务消费者> halo-cloud-consumer
- 子工程<服务提供者> halo-cloud-provider
halo-coud-parent
- 依赖引入
<!--打包方式-->
<packaging>pom</packaging>
<modules>
<!--moudle-->
<module>halo-cloud-server</module>
</modules>
<!--web依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-commons</artifactId>
</dependency>
<dependencyManagement>
<!--引入springcloud依赖的-->
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Greenwich.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
halo-cloud-server
- 依赖
<!--将其对应的父工程换为parent即可-->
<parent>
<groupId>com.cloud</groupId>
<artifactId>halo</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<!--eureka server 依赖坐标-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
application.yml
server: # 服务端口
port: 9090
spring:
application: # 应用名字,eureka 会根据它作为服务id
name: EurekaServer
eureka:
instance:
hostname: localhost
client:
service-url: # eureka server 的地址, 咱们单实例模式就写自己好了
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka
register-with-eureka: false# 不向eureka server 注册自己
fetch-registry: false# 不向eureka server 获取服务列表
修改启动类,加上注解:@EnableEurekaServer或者
启动
遇到的问题:
- 第一次启动启动的时候,会出现一下的错误,不要慌!
打开IDEA最右边maven,找到server,clean、install,重新启动即可。
halo-cloud-provider
- pom.xml
<parent>
<groupId>com.cloud</groupId>
<artifactId>parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
- application.yml
server:
port: 7070
spring:
application:
name: spring-cloud-provider
eureka:
client:
service-url:
defaultZone: http://localhost:9090/eureka
fetch-registry: true
register-with-eureka: true
启动类添加注解:@EnableEurekaClient
halo-cloud-consumer
- pom.xml
<parent>
<groupId>com.cloud</groupId>
<artifactId>parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
- application.yml
server:
port: 8080
spring:
application:
name: spring-cloud-consumer
eureka:
client:
service-url:
defaultZone: http://localhost:9090/eureka
fetch-registry: true
register-with-eureka: true
启动类添加注解:@EnableEurekaClient
我们可以看到服务提供者个消费者都注册到了注册中心。
消费者调用服务
服务提供者 halo-cloud-provider
修改内容
- 新增 ProviderController
@RestController
@RequestMapping("/provider")
public class ProviderController {
@Value("${server.port}")
private Integer port;
@GetMapping("/helloProvider")
public Integer helloProvider(){
return port;
}
}
测试:
服务消费者 halo-cloud-consumer
修改内容
- 新增 RestTemplateConfiguration
- 新增 ConsumerController
@Configuration
public class RestTemplateConfiguration {
@Bean
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
}
@RestController
@RequestMapping("/consumer")
public class ConsumerController {
@Autowired
private RestTemplate restTemplate;
@Autowired
private DiscoveryClient discoveryClient;
@GetMapping("/helloConsumer")
public Integer getTodayStatistic() {
// 使用discoveryClient 类能够与eureka server 交互, getInstances 获取注册到eureka server
// 的"spring-cloud-order-service-provider" 实例列表
List<ServiceInstance> instances = discoveryClient.getInstances("spring-cloud-provider");
// 获取第一个服务信息
ServiceInstance instanceInfo = instances.get(0);
//获取ip
String ip = instanceInfo.getHost();
//获取port
int port = instanceInfo.getPort();
String url = "http://" + ip + ":" + port + "/provider/helloProvider/";
return restTemplate.getForObject(url, Integer.class);
}
}
测试: