天天看點

springcloud的一些使用(非常詳細)一.快速搭建并啟動一個eureka伺服器二.向eureka注冊服務三.如何給eureka伺服器添加http認證

一.快速搭建并啟動一個eureka伺服器

首先使用idea建立一個項目

springcloud的一些使用(非常詳細)一.快速搭建并啟動一個eureka伺服器二.向eureka注冊服務三.如何給eureka伺服器添加http認證
springcloud的一些使用(非常詳細)一.快速搭建并啟動一個eureka伺服器二.向eureka注冊服務三.如何給eureka伺服器添加http認證
springcloud的一些使用(非常詳細)一.快速搭建并啟動一個eureka伺服器二.向eureka注冊服務三.如何給eureka伺服器添加http認證
springcloud的一些使用(非常詳細)一.快速搭建并啟動一個eureka伺服器二.向eureka注冊服務三.如何給eureka伺服器添加http認證

點選next,finish完成

然後在項目中找到springboot的配置檔案application.properties檔案,配置如下内容

springcloud的一些使用(非常詳細)一.快速搭建并啟動一個eureka伺服器二.向eureka注冊服務三.如何給eureka伺服器添加http認證
server.port=8976

eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false

eureka.client.service-url.defaultZone=http://localhost:8976/eureka
           

找到啟動類并添加注解@EnableEurekaServer

package eureka.server.eurekaserver;

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

@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {

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

}
           

運作改類

springcloud的一些使用(非常詳細)一.快速搭建并啟動一個eureka伺服器二.向eureka注冊服務三.如何給eureka伺服器添加http認證

在浏覽器中輸入http://localhost:8976即可看到服務注冊情況

springcloud的一些使用(非常詳細)一.快速搭建并啟動一個eureka伺服器二.向eureka注冊服務三.如何給eureka伺服器添加http認證

以上我們完成了簡單的eureka服務搭建,那接下來我們怎麼注冊服務呢

二.向eureka注冊服務

建立一個eureka-provider項目

springcloud的一些使用(非常詳細)一.快速搭建并啟動一個eureka伺服器二.向eureka注冊服務三.如何給eureka伺服器添加http認證
springcloud的一些使用(非常詳細)一.快速搭建并啟動一個eureka伺服器二.向eureka注冊服務三.如何給eureka伺服器添加http認證
springcloud的一些使用(非常詳細)一.快速搭建并啟動一個eureka伺服器二.向eureka注冊服務三.如何給eureka伺服器添加http認證

建立兩個包

springcloud的一些使用(非常詳細)一.快速搭建并啟動一個eureka伺服器二.向eureka注冊服務三.如何給eureka伺服器添加http認證

建立一個Service接口

package eureka.provider.eurekaprovider.service;

public interface MyService {
    String findSomeThing();
}
           

建立一個實作類

package eureka.provider.eurekaprovider.service.impl;

import eureka.provider.eurekaprovider.service.MyService;
import org.springframework.stereotype.Service;

@Service
public class MyServiceImpl implements MyService {


    @Override
    public String findSomeThing() {
        return "hello eureka,nice to meet you";
    }
}
           

建立一個controller以備之後調用

package eureka.provider.eurekaprovider.controller;

import eureka.provider.eurekaprovider.service.MyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MyServiceController {

    @Autowired
    private MyService myService;

    @RequestMapping("/findSomething/{a}/{b}")
    public String findSomething(@PathVariable("a")Integer a,@PathVariable("b")Integer b){
        System.out.println(a+b);
        return myService.findSomeThing();
    }
}
           

找到配置檔案application.properties

server.port=8189

eureka.client.service-url.defaultZone=http://localhost:8976/eureka
           

上面的eureka.client.service-url.defaultZone配置和之前一步驟中的配置相同,表示注冊到剛剛啟動的eureka伺服器上

找到啟動類,并添加注解@EnableEurekaClient,然後啟動應用

package eureka.provider.eurekaprovider;

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

@SpringBootApplication
@EnableEurekaClient
public class EurekaProviderApplication {

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

}
           

重新整理http://localhost:8976/連接配接,大家可以看到服務已經被注冊上了

springcloud的一些使用(非常詳細)一.快速搭建并啟動一個eureka伺服器二.向eureka注冊服務三.如何給eureka伺服器添加http認證

但是以上有兩個問題,一個就是apllication的名字是unknow,這個可以設定,另外滑鼠放到連接配接處左下角顯示的是主機名和端口,能不能顯示成ip呢,這個也是可以設定的

springcloud的一些使用(非常詳細)一.快速搭建并啟動一個eureka伺服器二.向eureka注冊服務三.如何給eureka伺服器添加http認證

找到配置檔案

server.port=8189

#改變應用名稱
spring.application.name=eureka-provider
#把滑鼠懸浮顯示主機名改成端口
eureka.instance.prefer-ip-address=true

eureka.client.service-url.defaultZone=http://localhost:8976/eureka
           

添加以上兩處配置,啟動再看下結果

springcloud的一些使用(非常詳細)一.快速搭建并啟動一個eureka伺服器二.向eureka注冊服務三.如何給eureka伺服器添加http認證

三.如何給eureka伺服器添加http認證

首先要在eureka-server工程裡添加jar包spring-boot-starter-security

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

然後找到配置檔案添加

server.port=8976

eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false


spring.security.user.name=user
spring.security.user.password=123456


eureka.client.service-url.defaultZone=http://user:[email protected]:8976/eureka
           
springcloud的一些使用(非常詳細)一.快速搭建并啟動一個eureka伺服器二.向eureka注冊服務三.如何給eureka伺服器添加http認證

然後同樣需要在eureka-provider工程裡的配置檔案中修改

springcloud的一些使用(非常詳細)一.快速搭建并啟動一個eureka伺服器二.向eureka注冊服務三.如何給eureka伺服器添加http認證
com.netflix.discovery.shared.transport.TransportException: Cannot execute request on any known server
           

分别啟動這兩個工程,會發現啟動eureka-provider工程時報以上錯誤,搜尋資料發現新版(Spring Cloud 2.0 以上)的security預設啟用了csrf檢驗,要在eurekaServer端配置security的csrf檢驗為false

是以在eureka-server工程裡添加一個配置類

springcloud的一些使用(非常詳細)一.快速搭建并啟動一個eureka伺服器二.向eureka注冊服務三.如何給eureka伺服器添加http認證
package eureka.server.eurekaserver.config;

import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

@EnableWebSecurity
public class WebSecurityConfigurer extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable();
        super.configure(http);
    }
}
           

再次啟動就可以正常通路了

繼續閱讀