【SpringCloud】(五):服務註冊到Eureka Server

00潤物無聲00發表於2017-08-13

  上篇文章我們建立了Eureka Server例項。本篇文章我們把使用者服務和電影服務註冊到Eureka Server上。(服務消費者和服務提供者)

  首先我們以註冊使用者服務為整個講述核心

  註冊文件,SpringCloud官網,我們定位到How to Include Eureka Client,跟著文件一步一步來。


一.基本註冊

1.How to Include Eureka Client

  在使用者服務的POM.xml 中加入依賴


		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-eureka</artifactId>
		</dependency>


2.Registering with Eureka



package com.dynamic.cloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@SpringBootApplication
@EnableEurekaClient
public class MicroserviceSimpleProviderUserApplication {

	public static void main(String[] args) {
		SpringApplication.run(MicroserviceSimpleProviderUserApplication.class, args);
	}
}

3.在application.yml中加入Eureka的地址

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/

  至此基本的服務註冊就已經完成了,我們的服務已經成功的註冊在了Erueka上面。我們啟動Eureka,並啟動使用者服務,可以觀察到,註冊在Eureka上面的服務例項。



  但是上面Application和Status有些問題。應用名稱是未知,狀態是電腦的名字。我們可以通過配置,進行定製,設定應用名稱和將狀態修改為主機+ 埠。


二.配置Application和Status

  在使用者服務的application.yml配置檔案中那個進行配置。



1.配置Application的名稱



2.配置Status


重啟服務,檢視。



三.為Eureka Server進行認證;



1.配置Eureka服務的application.yml



2.在Eureka服務的POM.xml中新增依賴

		    <dependency>
		    	<groupId>org.springframework.boot</groupId>
		    	<artifactId>spring-boot-starter-security</artifactId>
		    </dependency>


3.Eureka增加了認證,那麼服務在註冊的使用,也就需要訪問認證的Eureka。所以需要修改使用者的請求方式,修改使用者服務的Application.yml檔案。




四. 監控和管理生產環境


在使用者服務的POM.xml中加入依賴

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-actuator</artifactId>
		</dependency>



五. Eureka’s Health Checks (健康檢查)


在Eureka專案的application.yml中新增配置;




六. Using the EurekaClient



在使用者微服務的Controller中加入:





ServiceId = Spring.application.name

我們不但將使用者服務註冊到了Eureka Server中,並進行了Application配置,同時對Eureka進行了認證和使用。


同樣的方式把電影微服務註冊到Eureka中

1.修改application.yml配置檔案

2.在啟動類上加入註解@EnableEurekaClient

3.加入相關依賴。


啟動Eureka,使用者服務和電影服務。





程式碼展示:


Eureka

application.yml配置檔案

server:
  port: 8761 #Eurake
eureka:
  client:
    healthcheck: 
      enabled: true
    register-with-eureka: false
    fetch-registry: false
    service-url:
      defaultZone: http://user:pass123@localhost:8761/eureka #把Eureka註冊到那個Eureka
      
security:
  basic:
    enabled: true
  user: 
    name: user #login username
    password: pass123
    
    

POM.xml

<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-discovery-eureka</artifactId>
  <packaging>jar</packaging>

	<parent>
			<groupId>com.dynamic.cloud</groupId>
			<artifactId>microservice-spring-cloud</artifactId>
			<version>0.0.1-SNAPSHOT</version>
			<relativePath/> 
	</parent>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>


<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-eureka-server</artifactId>
    </dependency>
    
    <dependency>
    	<groupId>org.springframework.boot</groupId>
    	<artifactId>spring-boot-starter-security</artifactId>
    </dependency>

</dependencies>


</project>


使用者微服務:

application.yml

server:
  port: 7900
spring:
  jpa:
    generate-ddl: false
    show-sql: true
    hibernate:
      ddl-auto: none
  datasource:
    platform: h2
    schema: classpath:schema.sql  #建表語句
    data: classpath:data.sql #資料
    
  application: 
    name: microservice-provider-user #使用小寫
    
logging:
  level:
    root: INFO
    org.hibernate: INFO
    org.hibernate.type.descriptor.sql.BasicBinder: TRACE
    org.hibernate.type.descriptor.sql.BasicExtractor: TRACE
    com.dynamic: DEBUG
eureka:
  client:
    serviceUrl:
      defaultZone: http://user:pass123@localhost:8761/eureka
  instance:
    prefer-ip-address: true
    instance-id: ${spring.application.name}:${spring.application.instance_id:${server.port}}
   


啟動類
package com.dynamic.cloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@SpringBootApplication
@EnableEurekaClient
public class MicroserviceSimpleProviderUserApplication {

	public static void main(String[] args) {
		SpringApplication.run(MicroserviceSimpleProviderUserApplication.class, args);
	}
}

Controller

package com.dynamic.cloud.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import com.dynamic.cloud.entity.User;
import com.dynamic.cloud.repository.UserRepository;
import com.netflix.appinfo.InstanceInfo;
import com.netflix.discovery.EurekaClient;
import org.springframework.cloud.client.discovery.DiscoveryClient;

@RestController
public class UserController
{

	@Autowired
	private UserRepository userRepository;


	
	@GetMapping("/simple/{id}")
	public User findById(@PathVariable Long id)
	{
		return this.userRepository.findById(id);
	}
	
	@Autowired
	private EurekaClient eurekaClient;
	
	@GetMapping("/eureka-instance")
	public String serviceUrl() {
	    InstanceInfo instance = eurekaClient.getNextServerFromEureka("MICROSERVICE-PROVIDER-USER", false);
	    return instance.getHomePageUrl();
	}
	
	@Autowired
	private DiscoveryClient discoveryClient;
	@GetMapping("/instance-info")
	public ServiceInstance showInfo()
	{
		ServiceInstance localServiceInstance = this.discoveryClient.getLocalServiceInstance();
				return localServiceInstance;
	}
	
}

電影微服務類比使用者微服務,進行修改。

application.yml

server:
  port: 7901

user: 
 userServicePath: http://localhost:7900/simple/
 
spring:
  application:
    name: microservice-comsumer-movie
  
eureka:
  client:
    serviceUrl:
      defaultZone: http://user:pass123@localhost:8761/eureka
  instance: 
    prefer-ip-address: true
    instance-id: ${spring.application.name}:${spring.application.instance_id:${server.port}}

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>

	<artifactId>microservice-comsumer-movie</artifactId>
	<packaging>jar</packaging>

	<parent>
			<groupId>com.dynamic.cloud</groupId>
			<artifactId>microservice-spring-cloud</artifactId>
			<version>0.0.1-SNAPSHOT</version>
			<relativePath/> <!-- lookup parent from repository -->
	</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>
		
				<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-eureka</artifactId>
		</dependency>
		
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-actuator</artifactId>
		</dependency>
	</dependencies>
</project>

啟動類加上註解:@EnableEurekaClient





相關文章