2.0.2.RELEASE
Finchley.SR1
springcloud依赖版本管理,引入这个后当前pom或者子pom都不需要再显示的使用版本号。
org.springframework.cloud
spring-cloud-dependencies
${spring-cloud.version}
pom
import
说明:不同的springboot版本需要不同的springcloud版本
1) 引入依赖
org.springframework.cloud
spring-cloud-starter-netflix-eureka-server
2) 编写启动类
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
3) 准备配置文件(application.properties/ application.yml)
# 服务名称
spring.application.name=eureka-server
# 端口号
server.port=9999
# eureka配置
eureka.client.service-url.defaultZone=http://localhost:9999/eureka/
# 是否从其他的服务中心同步服务列表
eureka.client.fetch-registry=false
# 是否把自己作为服务注册到其他服务注册中心
eureka.client.register-with-eureka=false
# 编码设置
spring.http.encoding.force=true
spring.http.encoding.charset=UTF-8
spring.http.encoding.enabled=true
server.tomcat.uri-encoding=UTF-8
4)监控页面
监控页面参数说明参考:https://blog.csdn.net/qq_25112523/article/details/83028529
1)准备依赖
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
2)编写启动类
@SpringBootApplication
@EnableEurekaClient
public class EurekaProvider1Application {
public static void main(String[] args) {
SpringApplication.run(EurekaProvider1Application.class, args);
}
}
3)准备配置文件(application.properties/ application.yml)
eureka.client.serviceUrl.defaultZone=http://127.0.0.1:9999/eureka/
server.port=7709
spring.application.name=service-provider1
spring.http.encoding.force=true
spring.http.encoding.charset=UTF-8
spring.http.encoding.enabled=true
server.tomcat.uri-encoding=UTF-8
4)编写测试controller
@GetMapping("/infor")
public Map getInfor(){
count++;
System.out.println(this.port+"被第"+count+"次调用");
Map re = new HashMap<>();
re.put("message","消费者调用成功");
return re;
}
1)准备依赖
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
org.springframework.cloud
spring-cloud-starter-ribbon
2)编写启动类、实例化RestTemplate
@SpringBootApplication
@EnableEurekaClient
public class EurekaConsumer1Application {
public static void main(String[] args) {
SpringApplication.run(EurekaConsumer1Application.class, args);
}
/**
* 将该bean放入spring容器中管理;
* 开始负载均衡
* @return
*/
@Bean
@LoadBalanced
RestTemplate restTemplate(){
return new RestTemplate();
}
}
3)准备配置文件(application.properties/ application.yml)
server.port=8081
spring.application.name=service-consumer-ribbon
eureka.client.serviceUrl.defaultZone=http://localhost:9999/eureka/
spring.http.encoding.force=true
spring.http.encoding.charset=UTF-8
spring.http.encoding.enabled=true
server.tomcat.uri-encoding=UTF-8
4)编写测试controller
@RestController
public class TestController {
@Autowired
private RestTemplate restTemplate;
@GetMapping("/cmsInfor")
public String getInfor(){
// service-provider1 服务端名称 infor服务端url
String message = this.restTemplate.getForObject("http://service-provider1/infor",String.class);
System.out.println(message);
return message;
}
}
问题1:eureka server 启动报错 No qualifying bean of type ‘javax.servlet.Filter’ available
将版本
springboot 2.2.1.RELEASE
springcloud Finchley.SR1
改为
springboot 2.0.2.RELEASE
springcloud Finchley.SR1
问题2:springcloud依赖下载慢
在pom引入
spring-snapshots
Spring Snapshots
https://repo.spring.io/snapshot
true
spring-milestones
Spring Milestones
https://repo.spring.io/milestone
false
spring-snapshots
Spring Snapshots
https://repo.spring.io/snapshot
true
spring-milestones
Spring Milestones
https://repo.spring.io/milestone
false