搭建eureka集群
- 新建一个boot项目
File-》new-》project
如图选择-》next-》起名字如下
->next -》finish
- 新建3个注册中心
以三个注册中心为例,想多的自己加
项目名字上-》new-》module
-》next-》起名字-》next
选择依赖
Web —spring web这个就别选了,截图截多一个。选后两个就行
Security ----Spring Security
Spring Cloud Discovery----Eureka Server
修改pom.xml,解决java8版本的问题,后面每个服务都要改这个,我的idea默认jdk6,导致启动失败,加了这个就默认为JDK1.8了
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal><!--可以把依赖的包都打包到生成的Jar包中-->
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<!--指定jdk版本-->
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
配置application.yml
注意defaultZone,本例是三个注册中心,中心1配置地址2、3,中心2配置地址3、1,中心3配置地址1、2。注意顺序,
对于注册中心eureka默认优先注册到最前面那个,也就是1注册到2,2注册到3,3注册到1.
当其中一个注册中心死掉后,才会注册到其他地方。如2死掉了,1会注册到第二个地址3上面,结果就成了1注册到3,3注册到1.
eureka:
instance:
hostname: localhost
client:
# eureka.client.registerWithEureka:false 因为自己是为注册中心,不需要自己注册自己 集群模式下改为 ture,注册到其他注册中中心
# 是否将该实例信息注册到其他eureka server上;如果设置为false,那么该server无法被其他server发现,但是仍然可以发现其他server
# fetchRegistry:false来表明自己是一个eureka server.
registerWithEureka: true
#表示是否向eureka注册服务,即在eureka中注册自己,默认为true,此处应该设置为true,只有一个注册中心时为false;
#是否允许该客户端从eureka server上获取注册信息
fetchRegistry: true
serviceUrl:
# 集群模式:
defaultZone: http://admin:admin2@${eureka.instance.hostname}:8762/eureka/,http://admin:admin3@${eureka.instance.hostname}:8763/eureka/
# 单例模式:
#defaultZone: http://admin:admin1${eureka.instance.hostname}:8761/eureka/
#没有密码时,去掉security依赖,配置: defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
server:
#设为false,关闭自我保护,即Eureka server在云心光器件会去统计心跳失败比例在15分钟之内是否低于85%,如果低于85%,EurekaServer
#会将这些事例保护起来,让这些事例不会过期,但是在保护器内如果刚哈这个服务提供者非正常下线了,此时服务消费者会拿到一个无效的服务
#实例,此时调用会失败,对于这个问题需要服务消费者端有一些容错机制,如重试、断路器等;
enable-self-preservation: false
#扫描失效服务的间隔时间(单位是毫秒,摩恩是60*1000),即60s
eviction-interval-timer-in-ms: 10000
server:
#服务端口号
port: 8761
spring:
application:
name: a-server1
# #安全登入用户设置 ####******** 用户名密码,三个项目建议不一样
security:
user:
name: admin
password: admin1
其他两个按这个配置,记得改defaultZone,和password
修改启动类
加两个注解就可以了。
@EnableEurekaServer
@SpringBootApplication
public class AiServer1Application {
public static void main(String[] args) {
SpringApplication.run(AiServer1Application.class, args);
}
}
本来这样就可以启动了,但是我们配置了security安全验证,
而新版(Spring Cloud 2.0 以上)的security默认启用了csrf检验,要在eurekaServer端配置security的csrf检验为false
新建一个类WebSecurityConfig,关闭csrf检验
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable();
super.configure(http);
}
}
其他两个按这个写,到此一个简单的eureka集群就搭建完成了。
启动会报错,不用管,这个是注册到其他注册中心时没扫描到,原因是启动注册中心1时2还没启动,总得有个先后,都启动了就好了。
com.sun.jersey.api.client.ClientHandlerException: java.net.ConnectException: Connection refused: connect
登录http://IP:8761/
登录http://IP:8762/
登录http://IP:8763/
本地启动要输入密码
可以看出,1注册在2,2注册在3,3注册在1.
注册中心完成。
可以尝试关闭其中某个看看注册情况。可能有时差大概30s。
搭建服务提供者client
笔者将上述注册中心布置到了服务器。所以服务提供者就按照服务器配置了,本地的话,把ip改为localhost就行。
新建提供者1
项目名字-》new-》mudule-》next-》起名字-》next
选择依赖(两个)
Web —spring web
Spring Cloud Discovery----Eureka Server
配置application.yml
spring:
application:
name: ai-client1
server:
port: 8769
eureka:
instance:
# 服务提供者地址
hostname: localhost
# 注册中心ip(根据实际,本地写localhost)
hostname1: 47.102.111.111
client:
serviceUrl:
# 集群时,其实只需要配置一个地址,当这个地址的注册中心挂掉后,会自动注册到其他注册中心
# 配置多个注册中心地址的好处是:启动之前,某个注册中心挂掉了,会抛出异常,但是也会正常注册到其他某个注册中心
defaultZone: http://admin:admin1@${eureka.instance.hostname1}:8761/eureka/,http://admin:admin2@${eureka.instance.hostname1}:8762/eureka/,http://admin:admin3@${eureka.instance.hostname1}:8763/eureka/
启动类
@RestController
@EnableEurekaClient
@SpringBootApplication
public class AiClient1Application {
public static void main(String[] args) {
SpringApplication.run(AiClient1Application.class, args);
}
/**
* 假如这个客户端要提供一个getUser的方法
* @return
*/
@GetMapping(value = "/getUser")
@ResponseBody
public Map<String,Object> getUser(@RequestParam Integer id){
Map<String,Object> data = new HashMap<String,Object>();
data.put("id",id);
data.put("userName","admin");
data.put("from","provider-A");
return data;
}
}
新建提供者2
内容跟提供者1一样。
为了测试负载均衡,不要改服务名字,必须保持一致。
spring.application.name=ai-client1
修改一下端口号和输出结果就行,不同输出结果验证是哪个服务处理的请求。
如data.put(“from”,“provider-B1111111111”);
能看出不同就行。
启动提供者,发现3号注册中心会有这两个服务
搭建服务消费者customer
搭建一个消费者customer
新建流程通服务提供者。
application.yml配置如下
eureka:
instance:
# 服务提供者地址
hostname: localhost
# 注册中心ip(根据实际,本地写localhost)
hostname1: 47.102.111.111
client:
serviceUrl: #注册中心的注册地址
# defaultZone: http://localhost:8761/eureka/,http://localhost:8762/eureka/,http://localhost:8763/eureka/
defaultZone: http://skyworth:skyworth1@${eureka.instance.hostname1}:8761/eureka/,http://skyworth:skyworth2@${eureka.instance.hostname1}:8762/eureka/,http://skyworth:skyworth3@${eureka.instance.hostname1}:8763/eureka/
server:
port: 8759 #服务端口号
spring:
application:
name: service-consumer #服务名称--调用的时候根据名称来调用该服务的方法
修改启动类
/**
* Eureka客户端-消费者
*/
@RestController
@EnableEurekaClient
@SpringBootApplication
public class AiConsumer1Application {
@Autowired
RestTemplate restTemplate;
public static void main(String[] args) {
SpringApplication.run(AiConsumer1Application.class, args);
}
/**
* 实例化RestTemplate
* @return
*/
@LoadBalanced
@Bean
public RestTemplate rest() {
return new RestTemplate();
}
/**
* Rest服务端使用RestTemplate发起http请求,然后得到数据返回给前端----gotoUser是为了区分getUser怕小伙伴晕头
* @param id
* @return
*/
@GetMapping(value = "/gotoUser")
@ResponseBody
public Map<String,Object> getUser(@RequestParam Integer id){
Map<String,Object> data = new HashMap<String,Object>();
/**
* 小伙伴发现没有,地址居然是http://service-provider
* 居然不是http://127.0.0.1:8082/
* 因为他向注册中心注册了服务,服务名称service-provider,我们访问service-provider即可
*
* 注意要容错,当发生错误时重新掉这个接口,防止发生某个服务挂掉,请求失败的情况。
*/
data = restTemplate.getForObject("http://ai-client1/getUser?id="+id, Map.class);
return data;
}
}
启动消费者,发现3号注册中心会有这个服务。
访问地址:http:localhost:8759/gotoUser?id=1
多次访问,发现会分别从服务1,服务2 返回结果。(服务1,2 返回结果要设置的不一样,才能看出来的。)
总结几个坑
1.解决java8版本的问题,后面每个服务都要改这个,我的idea默认jdk6,导致启动失败,加了这个就默认为JDK1.8了
2.新版(Spring Cloud 2.0 以上)的security默认启用了csrf检验,要在eurekaServer端配置security的csrf检验为false
3.使用了安全验证,注册地址要加上username:password@,
http://username:password@${eureka.instance.hostname}:8762/eureka/
4.将注册中心部署到服务器之后,如果服务要连接服务时,
要设置eureka.instance.hostname=localhost,如果写成服务器ip,连接会成功,但是消费者会调用失败,因为跳转的时候会跳转到,服务器上的这个服务,而实际的服务在本地。