给微服注册到需要认证的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/
这里写图片描述
输入刚才配置的密码 用户名: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,端口,提供各个服务间的调用,也可以用客户端查询到,但是不会改变客户端的行为。
大功告成。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值