给微服注册到需要认证的Eureka Server及Eureka元素描述

前面写的都是匿名访问的,下边我们写个用登陆才能访问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/
给微服注册到需要认证的Eureka Server及Eureka元素描述_第1张图片
输入刚才配置的密码 用户名:user,密码:a123
给微服注册到需要认证的Eureka Server及Eureka元素描述_第2张图片
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 showInfo(){
        return this.discoveryClient.getInstances("miroservice-provider-user");
    }
}

这里produces(指的是服务端)是媒体类型将数据转换成json
consumes是客户端媒体类型,是客户端数据转成json 或者是xml
下面我们访问
http://localhost:8081/user-instance
给微服注册到需要认证的Eureka Server及Eureka元素描述_第3张图片
这里我在服务端转过了格式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>1versions__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-PChostName>
<app>
MICROSERVICE-CONSUMER-MOVIE-UNDERSTANDTING-METADATA
app>
<ipAddr>192.168.1.107ipAddr>
<status>STARTINGstatus>
<overriddenstatus>UNKNOWNoverriddenstatus>
<port enabled="true">8081port>
<securePort enabled="false">443securePort>
<countryId>1countryId>
<dataCenterInfo class="com.netflix.appinfo.InstanceInfo$DefaultDataCenterInfo">
<name>MyOwnname>
dataCenterInfo>
<leaseInfo>
<renewalIntervalInSecs>30renewalIntervalInSecs>
<durationInSecs>90durationInSecs>
<registrationTimestamp>1514466983510registrationTimestamp>
<lastRenewalTimestamp>1514468836341lastRenewalTimestamp>
<evictionTimestamp>0evictionTimestamp>
<serviceUpTimestamp>0serviceUpTimestamp>
leaseInfo>
<metadata class="java.util.Collections$EmptyMap"/>
<homePageUrl>http://lenovo-PC:8081/homePageUrl>
<statusPageUrl>http://lenovo-PC:8081/infostatusPageUrl>
<healthCheckUrl>http://lenovo-PC:8081/healthhealthCheckUrl>
<vipAddress>
microservice-consumer-movie-understandting-metadata
vipAddress>
<secureVipAddress>
microservice-consumer-movie-understandting-metadata
secureVipAddress>
<isCoordinatingDiscoveryServer>falseisCoordinatingDiscoveryServer>
<lastUpdatedTimestamp>1514466983510lastUpdatedTimestamp>
<lastDirtyTimestamp>1514466942954lastDirtyTimestamp>
<actionType>ADDEDactionType>
instance>
application>
<application>
<name>MIROSERVICE-PROVIDER-USERname>
<instance>
<instanceId>lenovo-PC:miroservice-provider-userinstanceId>
<hostName>192.168.1.107hostName>
<app>MIROSERVICE-PROVIDER-USERapp>
<ipAddr>192.168.1.107ipAddr>
<status>UPstatus>
<overriddenstatus>UNKNOWNoverriddenstatus>
<port enabled="true">8080port>
<securePort enabled="false">443securePort>
<countryId>1countryId>
<dataCenterInfo class="com.netflix.appinfo.InstanceInfo$DefaultDataCenterInfo">
<name>MyOwnname>
dataCenterInfo>
<leaseInfo>
<renewalIntervalInSecs>30renewalIntervalInSecs>
<durationInSecs>90durationInSecs>
<registrationTimestamp>1514468565114registrationTimestamp>
<lastRenewalTimestamp>1514468834947lastRenewalTimestamp>
<evictionTimestamp>0evictionTimestamp>
<serviceUpTimestamp>1514466880508serviceUpTimestamp>
leaseInfo>
<metadata>
<management.port>8080management.port>
<my-metadata>myUserMyMedatamy-metadata>
metadata>
<homePageUrl>http://192.168.1.107:8080/homePageUrl>
<statusPageUrl>http://192.168.1.107:8080/infostatusPageUrl>
<healthCheckUrl>http://192.168.1.107:8080/healthhealthCheckUrl>
<vipAddress>miroservice-provider-uservipAddress>
<secureVipAddress>miroservice-provider-usersecureVipAddress>
<isCoordinatingDiscoveryServer>falseisCoordinatingDiscoveryServer>
<lastUpdatedTimestamp>1514468565114lastUpdatedTimestamp>
<lastDirtyTimestamp>1514468563931lastDirtyTimestamp>
<actionType>ADDEDactionType>
instance>
application>
applications>

使用DiscoveryClient的API获得用户微服各种信息,其中包含了标准元素和自定义元素,例如:IP,端口,提供各个服务间的调用,也可以用客户端查询到,但是不会改变客户端的行为。
大功告成。

你可能感兴趣的:(spring,cloud微服务)