1. 程式人生 > >spring cloud (三、服務提供者demo_provider)

spring cloud (三、服務提供者demo_provider)

       spring cloud (一、服務註冊demo_eureka)   

      spring cloud (二、服務註冊安全demo_eureka)

      建立一個服務提供者註冊到服務註冊中心,跟前一個案例一樣建立一個spring boot 專案 ;

     pom檔案如下:

<?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>
    <groupId>com.test.provider</groupId>
    <artifactId>provider</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>provider</name>
    <description>provider</description>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
         <version>1.5.3.RELEASE</version>
    <relativePath />
    </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.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
 
    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Dalston.SR3</version>
                <type>pom</type>
                <scope>import
</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> <repositories> <repository> <id>spring-snapshots</id> <name>Spring Snapshots</name> <url>https://
repo.spring.io/snapshot</url> <snapshots> <enabled>true</enabled> </snapshots> </repository> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://
repo.spring.io/milestone</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> </project>

       配置檔案如下

#應用(服務)名稱
spring.application.name=eureka-provider
#埠號
server.port=8762
#註冊的賬號密碼地址
eureka.client.serviceUrl.defaultZone=http://admin:[email protected]:3333/eureka/

        啟動類

/**
 * 服務提供者
 */
@EnableDiscoveryClient
@SpringBootApplication

public class ProviderApplication {

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

     到這一步其實已經配置完成了,基本的程式碼;但是啟動會報錯,雖然配置了登入的賬號和密碼;但是由於spring cloud 的一些版本是需要修改註冊中心的啟動配置

   


 

     接下來的改動注意,這是是改服務註冊管理那邊的程式碼;

     我們在 spring cloud (一、服務註冊demo_eureka)   中加上一個自己的類,加上如下的註解,然後再重新啟動即可註冊成功

    

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

}

      這個時候我們先啟動服務管理的demo,然後再啟動服務提供者;最後登入服務控制檯檢視;

   

       這個時候我們可以看到服務註冊中心多了一個服務;