正好今天有时间,就把提供方和消费方的搭建也一并写了
友情链接
详细版Idea2019.2+maven+jdk1.8搭建springcloud,springboot入门(一)注册中心Eureka 类似(一)中的步骤创建新的模块
创建完项目的目录应该在同一级
下面导入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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.me</groupId>
<artifactId>EruekaService</artifactId>
<version>1.0-SNAPSHOT</version>
<name>EruekaService</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.7.RELEASE</version>
</parent>
<dependencies>
<!--引入springcloud的euekea client依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
<!--指定下载源和使用springcloud的版本-->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Edgware.SR5</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>
删除掉自己生成的demo代码
自己配置资源目录和application.yml文件(不明白请参考(一)中的步骤
自己配置启动类
利用注解@@EnableDiscoveryClient标注自己是客户端
写一个自定义的controller来提供服务
配置application.yml文件
server:
port: 8701 # 端口自己决定
# 指定当前eureka客户端的注册地址,也就是eureka服务的提供方,当前配置的服务的注册服务方
eureka:
client:
service-url:
defaultZone: http://${eureka.instance.hostname}:8697/eureka
register-with-eureka: true #自身 不在向eureka注册
fetch-registry: true #启动时禁用client的注册
instance:
hostname: localhost
#指定应用名称
spring:
application:
name: eureka-service
注意注册的端口号要向你自己搭建服务的端口进行注册
然后运行启动类
然后检查一下注册成功
通过地址栏来调用服务
说明成功了!
那么如何来实现消费方调用呢?下面我们就来搭建服务消费方
同样创建一个maven module
导入pom依赖文件,需要注意的是新增了一个ribbon的启动器
<?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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.yun</groupId>
<artifactId>EruekaCustomer</artifactId>
<version>1.0-SNAPSHOT</version>
<name>EruekaCustomer</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.7.RELEASE</version>
</parent>
<dependencies>
<!--引入springcloud的euekea client依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
</dependency>
</dependencies>
<!--指定下载源和使用springcloud的版本-->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Edgware.SR5</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>
配置application.yml文件
server:
port: 8702 # 服务消费方
# 指定当前eureka客户端的注册地址,
server:
port: 8702 # 服务消费方
# 指定当前eureka客户端的注册地址,
eureka:
client:
service-url:
defaultZone: http://localhost:8697/eureka
#当前服务名称
spring:
application:
name: eureka-consumer
自己写好启动类
自己写调用服务的controller
package com.yun.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
@RestController
@RequestMapping("/hello")
public class CustomerController {
@Autowired
private LoadBalancerClient loadBalancerClient;
@RequestMapping("/customer")
public String helloCustomer(String s){
ServiceInstance choose = loadBalancerClient.choose("EUREKA-SERVICE");
String forObject = new RestTemplate().getForObject("http://" + choose.getHost() + ":" + choose.getPort() + "/hello/world?s=" + s, String.class);
return "调用方:"+forObject;
}
}
运行启动类,发现注册中心注册成功
测试调用服务
调用成功