创建父工程spring-cloud
,引入依赖
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<packaging>pom</packaging>
<modules>
<module>cloud-eureka-server</module>
<module>cloud-service-provider</module>
<module>cloud-service-consumer</module>
</modules>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.cloud</groupId>
<artifactId>spring-cloud</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-cloud</name>
<description>spring-cloud project for Spring Boot</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<!--SpringCloud 版本-->
<!-- spring-cloud Finchley.SR1 版本 ==> springBoot 2.0.4 没有报错
<spring-cloud.version>Finchley.SR1</spring-cloud.version>
spring-cloud Hoxton.SR1 Hoxton.SR2 版本 ==> springBoot 2.2.4 没有报错
<spring-cloud.version>Hoxton.SR1</spring-cloud.version>
-->
<spring-cloud.version>Finchley.SR1</spring-cloud.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>
Eureka 注册中心
创建子工程Eureka 注册中心 (cloud-eureka-server)
,引入 Eureka
<?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>spring-cloud</artifactId>
<groupId>com.cloud</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>cloud-eureka-server</artifactId>
<dependencies>
<!-- Eureka 服务端 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
</project>
创建注册中心启动类,Application.java
package com.cloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
/**
* Eureka 注册中心
*/
@SpringBootApplication
@EnableEurekaServer
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class,args);
}
}
在配置文件中配置application.yml
,
spring:
application:
name: eureka-server
server:
port: 10086
eureka:
client:
# 本身就是 Eureka 注册中心,不需要获取与注册,只需要提供
# 是否注册到Eureka,如果是单机版不需要注册到其他Eureka
register-with-eureka: false
# 表示是否从Eureka Server获取注册信息,默认为true
fetch-registry: false
service-url:
# 对外提供服务的url
defaultZone: http://127.0.0.1:${server.port}/eureka
instance:
prefer-ip-address: true
ip-address: 127.0.0.1
服务提供者
创建子工程 服务提供者 (cloud-service-provider)
,引入依赖
<?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>spring-cloud</artifactId>
<groupId>com.cloud</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>cloud-service-provider</artifactId>
<dependencies>
<!-- Eureka 客户端 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!-- 由于 Eureka-client 没有Tomcat的依赖,所以Spring容器无法创建一些实例,从而导致项目无法启动 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
创建服务提供者启动类Application
,注意:@EnableEurekaClient 只能注册到 Eureka,而@EnableDiscoveryClient 兼容 Eureka zookeeper,所以使用 @EnableDiscoveryClient
package com.cloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
// @EnableEurekaClient 只能注册到 Eureka,
@EnableDiscoveryClient // 兼容 Eureka zookeeper
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class,args);
}
}
配置文件 application.yml
,
spring:
application:
name: eureka-provider
server:
port: 8761
eureka:
client:
service-url:
# 链接服务注册中心,将服务注册到注册中心
defaultZone: http://127.0.0.1:10086/eureka
创建与一个接口 TestController
,
package com.cloud.service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.Map;
@RestController
@RequestMapping("/provider")
public class TestController {
@RequestMapping("/test")
public Map test(){
Map map = new HashMap();
map.put("name","小二");
return map;
}
}
服务消费者
创建子工程服务消费者 (cloud-service-consumer)
,引入依赖
<?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>spring-cloud</artifactId>
<groupId>com.cloud</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>cloud-service-consumer</artifactId>
<dependencies>
<!-- Eureka 客户端 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!-- 由于 Eureka-client 没有Tomcat的依赖,所以Spring容器无法创建一些实例,从而导致项目无法启动 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- openfeign 远程调用服务 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
</dependencies>
</project>
创建服务消费者启动类 Application.java
,注意:@EnableEurekaClient 只能注册到 Eureka,而@EnableDiscoveryClient 兼容 Eureka zookeeper,所以使用 @EnableDiscoveryClient
,这里由于需要使用 httpClient远程调用来,所以配置spring提供的 RestTemplate
package com.cloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;
@EnableFeignClients
// @EnableEurekaClient 只能注册到 Eureka,
@EnableDiscoveryClient // 兼容 Eureka zookeeper
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class,args);
}
@Bean
public RestTemplate restTemplate(ClientHttpRequestFactory factory){
return new RestTemplate(factory);
}
@Bean
public ClientHttpRequestFactory simpleClientHttpRequestFactory(){
SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
factory.setReadTimeout(180000);//单位为ms
factory.setConnectTimeout(5000);//单位为ms
return factory;
}
}
配置文件 application.yml
,
spring:
application:
name: eureka-consumer
server:
port: 8088
eureka:
client:
service-url:
# 连接服务中心,将服务注册到注册中心
defaultZone: http://127.0.0.1:10086/eureka
在创建一个测试控制器 TestController
,
package com.cloud.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import java.util.List;
import java.util.Map;
@RestController
@RequestMapping("/consumer")
public class TestController {
@Autowired
private RestTemplate restTemplate;
@Autowired
private DiscoveryClient discoveryClient;
@RequestMapping("/test")
public Map test(){
# 从注册中心获取服务提供者
List<ServiceInstance> instances = discoveryClient.getInstances("eureka-provider");
ServiceInstance instance = instances.get(0);
String host = instance.getHost();
int port = instance.getPort();
String url = "http://" + host + ":" + port + "/provider/test";
# 调用服务提供者接口
Map result = restTemplate.getForObject(url, Map.class);
return result;
}
// ===================== feign 远程调用 ======================
@Autowired
private TestClient testClient;
@RequestMapping("/test/feign")
public Map testFeign(){
return testClient.test();
}
}
然后依次启动 注册中心、服务提供者、服务消费者
,然后浏览器访问 http://localhost:8088/consumer/test](http://localhost:8088/consumer/test
将返回服务提供者中定义的数据 {"name":"小二"}
。当前包下创建client.TestClient.java
package com.cloud.client;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import java.util.Map;
@FeignClient(name = "eureka-provider")
public interface TestClient {
@GetMapping("/provider/test")
Map test();
}
网友评论