前面寫的都是匿名訪問的,下邊我們寫個用登陸才能訪問Eureka Server
在pom.xml 添加 spring-boot-starter-security,該依賴爲Eureka Server 提供用戶認證能力。
在application.properties 添加
#開啓驗證
security.basic.enabled=true
#用戶名
security.user.name=user
#密碼
security.user.password=a123
#這個是表示微服務是否註冊Eureka Server
eureka.client.register-with-eureka= false
#表示是否從Eureka Server 獲取註冊信息
eureka.client.fetch-registry= false
#既然加了權限,默認註冊微服Eureka Server路徑,源碼默認也是8761端口
eureka.client.service-url.defaultZone= http://user:password@localhost:8761/eureka
訪問:http://localhost:8080/
輸入剛纔配置的密碼 用戶名:user,密碼:a123
3.我們可以看一下Eureka的元素
新建兩個項目分別是(microservice-consumer-movie-understandting-metadata(電影微服務))(miroservice-discovery-euraka-authenticating
miroservice-provider-user-my-metadata(用戶微服務)),微服Eureka Server(miroservice-discovery-eureka)
在miroservice-provider-user-my-metadata 的application.properties
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/
eureka.instance.prefer-ip-address=true
eureka.instance.metadata-map.my-metadata=myUserMyMedata
spring.application.name=miroservice-provider-user
microservice-consumer-movie-understandting-metadata的application.yml
server:
port: 8081
spring:
jpa:
generate-ddl: true
show-sql: true
hibernate:
ddl-auto: update
database: mysql
datasource:
username: root
password: a123
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/user
application:
name: microservice-consumer-movie-understandting-metadata
這裏用不到數據和jpa,只用server.port,還有application.name
之前在microservice-simple-consumer-movie寫過直接複製,改一下MovieController.java代碼就行。
package com.zjm.contoller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import com.zjm.user.User;
@RestController
public class MovieController {
@Autowired
public RestTemplate restTemplate;
@Autowired
private DiscoveryClient discoveryClient;
@GetMapping("/user/{id}")
public User findById(@PathVariable Long id){
return this.restTemplate.getForObject("http://localhost:8000/"+id, User.class);
}
@GetMapping(value="/user-instance",produces="application/json")
public List<ServiceInstance> showInfo(){
return this.discoveryClient.getInstances("miroservice-provider-user");
}
}
這裏produces(指的是服務端)是媒體類型將數據轉換成json
consumes是客戶端媒體類型,是客戶端數據轉成json 或者是xml
下面我們訪問
http://localhost:8081/user-instance
這裏我在服務端轉過了格式json,默認訪問是xml
在訪問一下微服務Eureka Server
http://localhost:8761/eureka/apps
This XML file does not appear to have any style information associated with it. The document tree is shown below.
<applications>
<versions__delta>1</versions__delta>
<apps__hashcode>STARTING_1_UP_1_</apps__hashcode>
<application>
<name>
MICROSERVICE-CONSUMER-MOVIE-UNDERSTANDTING-METADATA
</name>
<instance>
<instanceId>
lenovo-PC:microservice-consumer-movie-understandting-metadata:8081
</instanceId>
<hostName>lenovo-PC</hostName>
<app>
MICROSERVICE-CONSUMER-MOVIE-UNDERSTANDTING-METADATA
</app>
<ipAddr>192.168.1.107</ipAddr>
<status>STARTING</status>
<overriddenstatus>UNKNOWN</overriddenstatus>
<port enabled="true">8081</port>
<securePort enabled="false">443</securePort>
<countryId>1</countryId>
<dataCenterInfo class="com.netflix.appinfo.InstanceInfo$DefaultDataCenterInfo">
<name>MyOwn</name>
</dataCenterInfo>
<leaseInfo>
<renewalIntervalInSecs>30</renewalIntervalInSecs>
<durationInSecs>90</durationInSecs>
<registrationTimestamp>1514466983510</registrationTimestamp>
<lastRenewalTimestamp>1514468836341</lastRenewalTimestamp>
<evictionTimestamp>0</evictionTimestamp>
<serviceUpTimestamp>0</serviceUpTimestamp>
</leaseInfo>
<metadata class="java.util.Collections$EmptyMap"/>
<homePageUrl>http://lenovo-PC:8081/</homePageUrl>
<statusPageUrl>http://lenovo-PC:8081/info</statusPageUrl>
<healthCheckUrl>http://lenovo-PC:8081/health</healthCheckUrl>
<vipAddress>
microservice-consumer-movie-understandting-metadata
</vipAddress>
<secureVipAddress>
microservice-consumer-movie-understandting-metadata
</secureVipAddress>
<isCoordinatingDiscoveryServer>false</isCoordinatingDiscoveryServer>
<lastUpdatedTimestamp>1514466983510</lastUpdatedTimestamp>
<lastDirtyTimestamp>1514466942954</lastDirtyTimestamp>
<actionType>ADDED</actionType>
</instance>
</application>
<application>
<name>MIROSERVICE-PROVIDER-USER</name>
<instance>
<instanceId>lenovo-PC:miroservice-provider-user</instanceId>
<hostName>192.168.1.107</hostName>
<app>MIROSERVICE-PROVIDER-USER</app>
<ipAddr>192.168.1.107</ipAddr>
<status>UP</status>
<overriddenstatus>UNKNOWN</overriddenstatus>
<port enabled="true">8080</port>
<securePort enabled="false">443</securePort>
<countryId>1</countryId>
<dataCenterInfo class="com.netflix.appinfo.InstanceInfo$DefaultDataCenterInfo">
<name>MyOwn</name>
</dataCenterInfo>
<leaseInfo>
<renewalIntervalInSecs>30</renewalIntervalInSecs>
<durationInSecs>90</durationInSecs>
<registrationTimestamp>1514468565114</registrationTimestamp>
<lastRenewalTimestamp>1514468834947</lastRenewalTimestamp>
<evictionTimestamp>0</evictionTimestamp>
<serviceUpTimestamp>1514466880508</serviceUpTimestamp>
</leaseInfo>
<metadata>
<management.port>8080</management.port>
<my-metadata>myUserMyMedata</my-metadata>
</metadata>
<homePageUrl>http://192.168.1.107:8080/</homePageUrl>
<statusPageUrl>http://192.168.1.107:8080/info</statusPageUrl>
<healthCheckUrl>http://192.168.1.107:8080/health</healthCheckUrl>
<vipAddress>miroservice-provider-user</vipAddress>
<secureVipAddress>miroservice-provider-user</secureVipAddress>
<isCoordinatingDiscoveryServer>false</isCoordinatingDiscoveryServer>
<lastUpdatedTimestamp>1514468565114</lastUpdatedTimestamp>
<lastDirtyTimestamp>1514468563931</lastDirtyTimestamp>
<actionType>ADDED</actionType>
</instance>
</application>
</applications>
使用DiscoveryClient的API獲得用戶微服各種信息,其中包含了標準元素和自定義元素,例如:IP,端口,提供各個服務間的調用,也可以用客戶端查詢到,但是不會改變客戶端的行爲。
大功告成。