发表于: 2018-01-05 20:57:38
1 447
今天完成的事
搭建了springcloud的分布式注册中心。
1,注册中心的搭建。
一路等等等之后。
在配置文件里填写。
server.port=8761
eureka.instance.hostname=localhost
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
#属性对应服务注册中心的配置内容,指定服务注册中心的位置。
eureka.client.serviceUrl.defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
其中server.port配置eureka服务器端口号。Eureka的配置属性都在开源项目spring-cloud-netflix-master中定义(spring boot连文档都没有,只能看源码了),在这个项目中有两个类EurekaInstanceConfigBean 和EurekaClientConfigBean,分别含有eureka.instance和eureka.client相关属性的解释和定义。从中可以看到,registerWithEureka表示是否注册自身到eureka服务器,因为当前这个应用就是eureka服务器,没必要注册自身,所以这里是false。fetchRegistry表示是否从eureka服务器获取注册信息,同上,这里不需要。defaultZone就比较重要了,是设置eureka服务器所在的地址,查询服务和注册服务都需要依赖这个地址。
如果要加身份验证要加依赖。
<!-- 身份验证-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
项目结构
package com.ptteng;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* Created by ${MIND-ZR} on 2018/1/5.
*/
@RestController
public class Controller {
@Value("${spring.application.name}")
private String serverName;
/* @Autowired
DiscoveryClient client;*/
@RequestMapping("/callToXiaoAI")
public Car getMessage(){
System.out.println("小爱同学在此!!! !!!!!!!!!!!!!!!!!!!!!!!");
//return "你好! 我是小Bi同学,编号BI1001, 我服务器的名称是:" + serverName + "=====";
Car car=new Car();
car.setColor("黄色");
car.setId(1);
car.setSize("大奔驰");
return car;
}
}
收获
遇到的问题
启动多个端口。
取消即可。
明天的计划
配置完成
评论