注册中心Eureka-Server
简介
注册中心用来注册服务的。这个是服务端,需要单独启动一个服务。然后在springboot的Web工程中依赖Eureka-client端来进行注册。这个Server端有自己的页面可以访问。
常用8761来做默认端口。
POM
注册中心服务端:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
完整:
<?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.beyond.eureka1</groupId>
<artifactId>eureka-server1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>eureka-server1</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Finchley.SR2</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<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>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
入口类
@EnableEurekaServer来标识启动Eureka的服务
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class EurekaServer1Application {
public static void main(String[] args) {
SpringApplication.run(EurekaServer1Application.class, args);
}
}
配置文件
application.properties
#设置tomcat服务端口号
server.port=8761
#设置服务名称
spring.application.name=eureka-service
#设置注册中心的host
eureka.instance.hostname=localhost
#注册中心不需要注册自己
eureka.client.register-with-eureka=false
#注册中心不需要去发现服务
eureka.client.fetch-registry=false
#设置服务注册中心的URL
eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka
或者:
application.yml
server:
port: 8761
eureka:
instance:
hostname: localhost
client:
registerWithEureka: false
fetchRegistry: false
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
启动: 类似springboot
访问
http://ip:port
示例:http://localhost:8761/
界面:
服务提供者(Eureka-Client)
简介
服务提供者包括两方面,提供的服务 和 注册提供的这些服务。所有暴露的接口都作为暴露的接口。服务注册使用Eureka-Client。
用来将现有暴露的接口注册到Eureka中,Eureka-Client有注册和发现服务的功能,这里只是用注册。在服务的消费者就需要发现服务和消费服务了。Eureka-Client只能发现服务,然后再使用Ribbon来做服务的调用,以及多个服务的负载均衡。
POM
新增eureka-client
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
完整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.example</groupId>
<artifactId>boot1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>demo1</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.6.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.SR2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
入口类
入库类添加@EnableDiscoveryClient,标识: 启动服务发现客户端 Eureka-Client.
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
// 启动服务发现客户端 Eureka-Client
@EnableDiscoveryClient
@SpringBootApplication
public class Boot1Application {
public static void main(String[] args) {
SpringApplication.run(Boot1Application.class, args);
}
}
配置文件
添加配置:
#设置服务名
spring.application.name=boot1
#设置服务注册中心的URL,本服务要向该服务注册中心注册自己
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka
完整:
#设置项目端口
server.port=9091
#配置项目名称
server.servlet.context-path=/boot1
#配置日志文件
#设置日志级别 只能设置到debug级别,默认是info级别,error=true 这句话没啥用
#debug=true
#设置日志文件 : 相对路径和绝对路径都可以
logging.file=./boot1.log
#按照包名设置日志级别
logging.level.com.example.demo1=debug
#设置日志格式化
logging.pattern.console=%date [%thread] %-5level %logger{50}:%L - %msg%n
# 设置日志配置文件:properties中设置有限,可以单独设置
#logging.config=classpath:logging-config.xml
#设置服务名
spring.application.name=boot1
#设置服务注册中心的URL,本服务要向该服务注册中心注册自己
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka
启动
启动入库类即可。
验证
在Eureka中看注册的服务。
服务接口
访问:http://localhost:9091/boot1/fun1
返回:
{
"id": "id1",
"name": "name1"
}
Controller:
@RestController
public class Test1Controller {
private final Logger log = LoggerFactory.getLogger(Test1Controller.class);
@RequestMapping("/fun1")
public Test1Model1 fun1(){
log.debug("fun1 .....");
return new Test1Model1("id1","name1");
}
}
问题
SpringBoot2.0和SpringCloud版本匹配问题导致的NoSuchMethodError
spring boot 2.0.0由于版本不匹配导致的NoSuchMethodError问题解析。我刚开始用的是1.5的springboot和Finchley 的springcloud。人工进行版本匹配比较烦,可以使用springboot自动生成。网址:https://start.spring.io/。
错误信息:
11:12:41.831 [main] ERROR org.springframework.boot.SpringApplication - Application run failed
java.lang.NoSuchMethodError: org.springframework.boot.builder.SpringApplicationBuilder.<init>([Ljava/lang/Object;)V
at org.springframework.cloud.bootstrap.BootstrapApplicationListener.bootstrapServiceContext(BootstrapApplicationListener.java:125)
at org.springframework.cloud.bootstrap.BootstrapApplicationListener.onApplicationEvent(BootstrapApplicationListener.java:89)
at org.springframework.cloud.bootstrap.BootstrapApplicationListener.onApplicationEvent(BootstrapApplicationListener.java:62)
at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:172)
解析:
Spring Cloud | Spring Boot |
Finchley | 兼容Spring Boot 2.0.x,不兼容Spring Boot 1.5.x |
Dalston和Edgware | 兼容Spring Boot 1.5.x,不兼容Spring Boot 2.0.x |
Camden | 兼容Spring Boot 1.4.x,也兼容Spring Boot 1.5.x |
Brixton | 兼容Spring Boot 1.3.x,也兼容Spring Boot 1.4.x |
Angel | 兼容Spring Boot 1.2.x |
服务消费者(Eureka-Client/Ribbon)
介绍
eureka-client 用来注册和发现服务。
ribbon 进行服务的调用和负载均衡
POM
增量:
<!-- 引入eureka 客户端依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!-- 引入ribbon 依赖 ,用来实现负载均衡,我们这里只是使用,先不作其他介绍 。这里比service服务提供者,多了ribbon的依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency>
完整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.example</groupId>
<artifactId>boot1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>demo1</name>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.6.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- 引入eureka 客户端依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!-- 引入ribbon 依赖 ,用来实现负载均衡,我们这里只是使用,先不作其他介绍 。这里比service服务提供者,多了ribbon的依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.SR2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
入口类
添加@EnableDiscoveryClient发现服务和注册自己的服务。
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@EnableDiscoveryClient //这里也要用到@EnableDiscoveryClient, 让服务使用eureka服务器, 实现服务注册和发现
@SpringBootApplication
public class BootApplication {
//@Bean 应用在方法上,用来将方法返回值设为为bean
//@LoadBalanced实现负载均衡
@Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(BootApplication.class, args);
}
}
调用
- 注入:RestTemplate
- 使用restTemplate调用,参数包括:URL,返回值类型
- URL格式:http://注册在Eureka中的服务名/应用名/URI
- 注册服务名在spring.application.name配置
- “应用名/URI”这个得看服务提供者的URI,对应服务提供者如下:
- 返回的结果可以直接转换,可以但拉出一个jar也可以。
Controller:
@RestController
public class Test1Controller {
//这里注入的restTemplate就是在com.sam.ConsumerApp中通过@Bean配置的实例
@Autowired
RestTemplate restTemplate;
@RequestMapping("/hello")
public String helloConsumer() {
//调用hello-service服务,注意这里用的是服务名,而不是具体的ip+port
Test1Model1 m = restTemplate.getForObject("http://boot1/boot1/fun1", Test1Model1.class);
System.out.println(m.toString());
return "hello consumer finish !!!" + m.toString();
}
}
启动
同springboot。
验证
http://localhost:9092/boot2/hello
返回:hello consumer finish !!!Test1Model1 [id=id1, name=name1]
end