参考:http://www.ityouknow.com/springcloud/2017/05/12/eureka-provider-constomer.html
D:\springcloud\eureka-service
目标
- 工程导入
- pom.xml
- 配置文件
- 监控actuator
- 启动类
- controller
工程导入
解压D:\springcloud\demo-discovery.zip,导入后更改工程名为eureka-service
pom.xml
<?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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.2.RELEASE</version>
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Hoxton.RELEASE</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- 连接bus -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
<!-- config客户端 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</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>
配置文件bootstrap.yml与云相关的配置
#springboot基本配置
spring:
application:
name: eureka-service
server:
port: 9000
eureka:
instance:
instanceId: ${eureka.instance.ipAddress}:${spring.application.name}:${server.port}
ipAddress: 127.0.0.1
#是否优先使用ip地址
preferIpAddress: true
#用上述三行替代localhost,改为用ip地址访问
#hostname: localhost
client:
#是否在eureka服务器上注册自己的信息以供其他服务发现 (server端为单机时false,server端有负载时为true client端为true)
registerWithEureka: true
#是否获取eureka服务器注册表上的注册信息 eureka-server端为false,其他为true
fetchRegistry: true
#此处为eureka-server服务地址,即指定向谁注册
serviceUrl:
defaultZone: http://127.0.0.1:8090/eureka
监控actuator
step 1:核对pom.xml是否有如下包
<!-- 健康情况、路由情况监控-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
step2 :application.yml的根节点下新增以下配置
management:
endpoint:
health:
showDetails: ALWAYS
gateway:
enabled: true
endpoints:
web:
base-path: /actuator
exposure:
include: "*
step3 测试
http://127.0.0.1:9001/actuator
启动类
@SpringBootApplication
public class ServiceApplication{
public static void main(String[] args) {
SpringApplication.run(ServiceApplication.class, args);
}
}
controller
新建包com.example.demo.controller,并编写如下controller,用于后面的测试
package com.example.demo.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@RequestMapping("/hello")
public String index(@RequestParam String name) {
return "hello "+name+",this is first messge";
}
}
测试#
分别启动eureka-server和eureka-service
之后在eureka-server面板看服务情况
http://127.0.0.1:8090/eurekaAdmin
测试#
先在浏览器里测试http://127.0.0.1:9000/hello?name=wang.qj
到此服务提供者配置就完成了。
网友评论