0402-服务注册与发现-Eureka Server使用、将服务注册到Eureka server上
一、Eureka Server使用
1.1、原项目改造【方便后续开发增加父pom】
1》复制改名
2》编写一个父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.lhx.cloud</groupId> <artifactId>microservice-spring-cloud</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>pom</packaging> <name>microservice-spring-cloud</name> <description>Demo project for Spring Boot</description> <modules> <module>microservice-comsumer-movie</module> <module>microservice-provider-user</module> </modules> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.9.RELEASE</version> </parent> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Edgware.SR3</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>
3》修改上两个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> <artifactId>microservice-provider-user</artifactId> <packaging>jar</packaging> <parent> <groupId>com.lhx.cloud</groupId> <artifactId>microservice-spring-cloud</artifactId> <version>0.0.1-SNAPSHOT</version> </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-data-jpa</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <scope>runtime</scope> </dependency> </dependencies> </project>
<?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> <artifactId>microservice-comsumer-movie</artifactId> <packaging>jar</packaging> <parent> <groupId>com.lhx.cloud</groupId> <artifactId>microservice-spring-cloud</artifactId> <version>0.0.1-SNAPSHOT</version> </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> </dependencies> </project>
4》导入项目,更新maven即可。
1.2、eureka server使用
新建maven module项目,microservice-discovery-eureka,单机使用
pom依赖
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> </dependency>
配置文件
#server.port: 指明了应用启动的端口号
#eureka.client.registerWithEureka: 值为false意味着自身仅作为服务器,不作为客户端
#eureka.client.fetchRegistry: 值为false意味着无需注册自身
#eureka.client.serviceUrl.defaultZone: 指明了应用的URL
server:
port: 8761
eureka:
client:
register-with-eureka: false
fetch-registry: false
service-url:
defaultZone: http://localhost:8761/eureka
项目启动类
@SpringBootApplication
@EnableEurekaServer
public class EurekaApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaApplication.class, args);
}
}
现在可以访问http://localhost:8761,基本都是字面含义
git地址:https://github.com/bjlhx15/spring-cloud
使用tag:初始化eureka-server
二、将服务注册到Eureka server上
修改microservice-provider-user服务
1、pom引用
<dependency> <groupId>org.springframework.cloud</groupId> <!--原引用方式,可用--> <!-- <artifactId>spring-cloud-starter-eureka</artifactId> --> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency>
2、注册Eureka
旧版本需要增加@EnableEurekaClient、@EnableDiscoveryClient
新版本按照文档不需要增加,必须是spring-boot项目
3、配置文件配置application.yml
eureka: client: serviceUrl: defaultZone: http://localhost:8761/eureka/ instance: prefer-ip-address: true instance-id: ${spring.application.name}:${spring.application.instance_id:${server.port}}
尽量补全应用程序名
spring:
application:
name: mircoservice-provider-user # 建议小写
此时启动项目即可。
三、Eureka Server增加授权认证
修改microservice-discovery-eureka项目
1、配置属性
eureka.client.serviceUrl.defaultZone=http://user:password@localhost:8761/eureka即可
实践证明不可行。需要增加security配置
2、security配置pom.xml
添加 spring-boot-starter-security,该依赖为Eureka Server 提供用户认证能力。
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency>
3、增加配置文件
security:
basic:
enabled: true #开启验证
user:
name: user #用户名
password: a123 #密码
注意如果此处增加了认证,在注册服务的时候也需要修改注册服务的链接,即第二部分的3设置为
eureka:
client:
serviceUrl:
defaultZone: http://user:a123@localhost:8761/eureka/
四、应用服务增加监控和管理
在user 的pom增加
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
参看地址:http://www.cnblogs.com/bjlhx/p/8719625.html
修改eureka路径
eureka:
instance:
statusPageUrlPath: ${management.context-path}/info
healthCheckUrlPath: ${management.context-path}/health
五、其他
5.1、注册安全应用程序
5.2、eureka的健康检查
在spring-cloud-netfix-eureka 1.1 之后使用eureka.client.healthcheck.enabled=true配置,此配置也是基础acturator的
查看:http://localhost:8761/health
5.3、实例Id
eureka.instance.instanceId=${spring.application.name}:${spring.application.instance_id:${server.port}}
5.4、eurekaClient与discoveryClient
import
import org.springframework.cloud.client.discovery.DiscoveryClient; import com.netflix.discovery.EurekaClient;
代码
@GetMapping("/eureka-instance") public String serviceUrl() { InstanceInfo instance = eurekaClient.getNextServerFromEureka("MIRCOSERVICE-PROVIDER-USER", false); return instance.getHomePageUrl(); } @GetMapping("/eureka-info") public ServiceInstance showInfo() { ServiceInstance serviceInstance = discoveryClient.getLocalServiceInstance(); return serviceInstance; } @GetMapping("/eureka-url") public Object serviceUrl2() { List<ServiceInstance> list = discoveryClient.getInstances("MIRCOSERVICE-PROVIDER-USER"); if (list != null && list.size() > 0 ) { return list.get(0).getUri(); } return null; }
5.5、http 使用
默认情况下,EurekaClient使用Jersey进行HTTP通信。如果您希望避免来自Jersey的依赖关系,则可以将它从依赖关系中排除。 Spring Cloud将自动配置基于Spring RestTemplate的传输客户端。
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> <exclusions> <exclusion> <groupId>com.sun.jersey</groupId> <artifactId>jersey-client</artifactId> </exclusion> <exclusion> <groupId>com.sun.jersey</groupId> <artifactId>jersey-core</artifactId> </exclusion> <exclusion> <groupId>com.sun.jersey.contribs</groupId> <artifactId>jersey-apache-client4</artifactId> </exclusion> </exclusions> </dependency>
以上类似的配置可以配置到microservice-comsumer-movie上即可
代码地址:https://github.com/bjlhx15/spring-cloud
以上项目启动 先启动发现服务,启动后在分别启动user和movie